BayesFactor Examples Michael Friendly 04 Dec 2015 The BayesFactor package enables the computation of Bayes factors in standard designs, such as one- and two- sample designs, ANOVA designs, and regression. Some examples taken from http://bayesfactorpcl.r-forge.r-project.org/ Load the package require(bayesfactor, quietly=true) ************ Welcome to BayesFactor 0.9.12-2. If you have questions, please contact Richard Morey (richarddmorey@gmail.com). Type BFManual() to open the manual. ************ Independent groups t-test The ttestbf function is used to obtain Bayes factors corresponding to tests of a single sample's mean, or tests that two independent samples have the same mean. The chickwts data set has six groups, but we reduce it to two for the example. data(chickwts) Restrict to two groups chicks = chickwts[chickwts$feed %in% c("horsebean","linseed"),] Drop unused factor levels chicks$feed = factor(chicks$feed) Plot data plot(weight ~ feed, data = chicks, main = "Chick weights")
Traditional t test t.test(weight ~ feed, data = chicks, var.eq=true) Two Sample t-test data: weight by feed t = -2.934, df = 20, p-value = 0.008205 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -100.17618-16.92382 sample estimates: mean in group horsebean mean in group linseed 160.20 218.75 Compute the corresponding Bayes factor using ttestbf. This is expressed as the ratio of the posterior odds of M 1 relative to M 0. bf = ttestbf(formula = weight ~ feed, data = chicks) bf Bayes factor analysis -------------- [1] Alt., r=0.707 : 5.975741 ±0% Against denominator: Null, mu1-mu2 = 0 --- Bayes factor type: BFindepSample, JZS We can sample from the posterior distribution for the numerator model. There is one chain for each parameter. chains <- posterior(bf, iterations = 10000) plot(chains[,1:2], trace=false)
Two-way ANOVA The BayesFactor package has two main functions that allow the comparison of models with factors as predictors (ANOVA): anovabf, which computes several model estimates at once, and lmbf, which computes one comparison at a time. The ToothGrowth data is a 3x2 fixed-effect ANOVA. data(toothgrowth) # plot the data library(ggplot2) ggplot(toothgrowth, aes(x=dose, y=len)) + geom_point(position=position_jitter(width=0.1)) + geom_smooth(aes(group=1), size=1.5) + facet_grid(.~ supp) + theme_bw() geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. Treat dose as a factor ToothGrowth$dose = factor(toothgrowth$dose) levels(toothgrowth$dose) = c("low", "Medium", "High") summary(tooth.aov <- aov(len ~ supp*dose, data=toothgrowth)) Df Sum Sq Mean Sq F value Pr(>F) supp 1 205.4 205.4 15.572 0.000231 *** dose 2 2426.4 1213.2 92.000 < 2e-16 *** supp:dose 2 108.3 54.2 4.107 0.021860 * Residuals 54 712.1 13.2 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
There appears to be a large effect of the dosage, a small effect of the supplement type, and perhaps a hint of an interaction. We could test model simplification via stepaic (Note that using k=log(n) gives BIC) MASS:::stepAIC(tooth.aov, k=log(nrow(toothgrowth))) Start: AIC=173 len ~ supp * dose Df Sum of Sq RSS AIC <none> 712.11 173.00 - supp:dose 2 108.32 820.43 173.31 Call: aov(formula = len ~ supp * dose, data = ToothGrowth) Terms: supp dose supp:dose Residuals Sum of Squares 205.350 2426.434 108.319 712.106 Deg. of Freedom 1 2 2 54 Residual standard error: 3.631411 Estimated effects may be unbalanced The anovabf function will compute the Bayes factors of all models against the intercept-only model; by default, it will choose the subset of all models in which which an interaction can only be included if all constituent effects or interactions are included (argument whichmodels is set to withmain, indicating that interactions can only enter in with their main effects). First, we show the default behavior. bf = anovabf(len ~ supp*dose, data=toothgrowth) bf Bayes factor analysis -------------- [1] supp : 1.198757 ±0.01% [2] dose : 4.983636e+12 ±0% [3] supp + dose : 2.942959e+14 ±1.57% [4] supp + dose + supp:dose : 7.421358e+14 ±1.1% Against denominator: Intercept only --- Bayes factor type: BFlinearModel, JZS Plot bayes factors against the intercept-only model plot(bf)
In the BayesFactor package "/" divides two Bayes factor objects to create new model comparisons The model with the main effect of supp and the supp:dose interaction is preferred quite strongly over the dose-only model. plot(bf[3:4] / bf[2]) Top-down analysis Using whichmodels="top" is like backward elimination, starting from the full model. bf = anovabf(len ~ supp*dose, data=toothgrowth, whichmodels="top") bf Bayes factor top-down analysis -------------- When effect is omitted from supp + dose + supp:dose, BF is... [1] Omit dose:supp : 0.3593662 ±2.79% [2] Omit dose : 6.422682e-16 ±5.87% [3] Omit supp : 0.01045709 ±3.34% Against denominator: len ~ supp + dose + supp:dose
--- Bayes factor type: BFlinearModel, JZS plot(bf)