richarddmorey / BayesFactor

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

ttestBF( ) "not enough observations" error #166

Closed dominic007 closed 1 year ago

dominic007 commented 1 year ago

Following is the code:

ttest= ttestBF(formula=count~spray, data=InsectSprays[InsectSprays$spray %in% c("C","B"),])

InsectSprays is inbuilt R data. I get the following error:

Error in ttest.tstat(t = t, n1 = ns[1], n2 = ns[2], nullInterval = nullInterval,  : 
  not enough observations

I notice that when I change the subsetting operation from c("C","B") to c("C","A") there is an output. strange thing is that both the cases give similar dataframes (with 24 observations each)

richarddmorey commented 1 year ago

Confirrmed; thanks for the reproducible example. It looks like this has to do with the fact that you've not dropped the factor levels, there are six factor levels (some of which have 0 observations). Hence, it cannot do a t test. Try this:

ttest = ttestBF(formula=count~spray, data=InsectSprays[InsectSprays$spray %in% c("C","B"),] |> droplevels())

(note the use of droplevels()). That will remake the factor so that it only has two levels, C and B. Ideally, this would give a different warning, like "Indep. groups t test requires a factor with exactly 2 levels."

richarddmorey commented 1 year ago

For my future reference, the correct error is not being emitted because the line:

https://github.com/richarddmorey/BayesFactor/blob/0868c0a452123aaa1b3c59bb5d5adf421ade6450/pkg/BayesFactor/R/checking.R#L71

(re)makes the factor before checking the number of levels, hence the unused levels will be dropped. This line should be changed so that it only remakes it if it is not already a factor.