Search code examples
rmcmc

How to find difference in mean using MCMCregress command?


I am trying to figure out how to find the difference in means for two categorical variables using MCMCregress and to plot the densities.

My code is

library(MCMCpack)
data("crabs") 
out <- MCMCregress(sex~sp , data = data, family=binomial)
summary(out)

I keep getting the error message-

Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, : NA/NaN/Inf in 'y'

What should i do to fix this?


Solution

  • I noticed that the sex variable is a factor. You can simple convert the factor to numeric and your code will work. Here is the code,

    library(MCMCpack)
    data("crabs") 
    out <- MCMCregress(as.numeric(sex)~sp , data = crabs, family=binomial)
    summary(out)
    
    Iterations = 1001:11000
    Thinning interval = 1 
    Number of chains = 1 
    Sample size per chain = 10000 
    
    1. Empirical mean and standard deviation for each variable,
       plus standard error of the mean:
    
                      Mean      SD  Naive SE Time-series SE
    (Intercept)  1.5002783 0.05052 0.0005052      0.0005052
    spO         -0.0003147 0.07202 0.0007202      0.0007202
    sigma2       0.2551607 0.02597 0.0002597      0.0002637
    
    2. Quantiles for each variable:
    
                   2.5%      25%        50%     75%  97.5%
    (Intercept)  1.4016  1.46639  1.5005847 1.53420 1.5996
    spO         -0.1433 -0.04842 -0.0009755 0.04696 0.1420
    sigma2       0.2091  0.23688  0.2534471 0.27180 0.3105