richarddmorey / BayesFactor

BayesFactor R package for Bayesian data analysis with common statistical models.
https://richarddmorey.github.io/BayesFactor/
131 stars 48 forks source link

posterior sampling produces error #96

Closed SanVerhavert closed 7 years ago

SanVerhavert commented 7 years ago

When estimating the posterior from a model with an interaction term containing a categorical variable without including that categorical variable as a main effect, the posterior function produces the following error: "Error in 1:(ncol(chains) - 2 - nGs) : argument of length 0"

The following code is a reproducible example with Xcat as the categorical variable. BF1a and BF1c produce the error.

Data_sub <- data.frame( Ypred = c( 10, 14, 19, 22, 15, 17, 15, 17, 18, 18, 18,
                                   19, 20, 30, 18, 19, 21, 26, 28, 27, 40, 16 ),
                        Xcont = c( 84, 42, 20, 42, 16, 20, 58, 6, 135, 58, 201,
                                   22, 20, 147, 10, 20, 30, 35, 9, 22, 9, 44 ),
                        Xcat = c( 1, 2, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 2,
                                  1, 3, 1, 2, 2, 2, 3 ) )

Data_sub$Xcat <- factor( Data_sub$Xcat )

BF1a <- lmBF( Ypred ~ Xcont:Xcat, data = Data_sub )
BF1b <- lmBF( Ypred ~ Xcont + Xcat + Xcont:Xcat, data = Data_sub )
BF1c <- lmBF( Ypred ~ Xcont + Xcont:Xcat, data = Data_sub )
BF1d <- lmBF( Ypred ~ Xcat + Xcont:Xcat, data = Data_sub )

posterior( BF1b, iterations = 10000 )
posterior( BF1d, iterations = 10000 )

posterior( BF1a, iterations = 10000 )
posterior( BF1c, iterations = 10000 )

BayesFactor version 0.9.12-2