5. Bayes Factors
mpg qsec wt
Mazda RX4 21.0 16.46 2.620
Mazda RX4 Wag 21.0 17.02 2.875
Datsun 710 22.8 18.61 2.320
Hornet 4 Drive 21.4 19.44 3.215
Hornet Sportabout 18.7 17.02 3.440
Valiant 18.1 20.22 3.460
mtcars |>
ggplot(aes(x=mpg, y=qsec)) +
geom_point() +
geom_smooth(method="lm", formula = "y ~ x") +
theme_bw()
Call:
lm(formula = qsec ~ mpg, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-2.8161 -1.0287 0.0954 0.8623 4.7149
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 15.35477 1.02978 14.911 2.05e-15 ***
mpg 0.12414 0.04916 2.525 0.0171 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.65 on 30 degrees of freedom
Multiple R-squared: 0.1753, Adjusted R-squared: 0.1478
F-statistic: 6.377 on 1 and 30 DF, p-value: 0.01708
mpg
is a significant predictor of qsec
(\(\beta\) = 0.12, p < .05)mtcars |>
ggplot(aes(x=wt, y=qsec)) +
geom_point() +
geom_smooth(method="lm", formula = "y ~ x") +
theme_bw()
wt
will improve the model?
Call:
lm(formula = qsec ~ mpg + wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-2.1092 -0.9617 -0.2343 0.8399 4.2769
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.92949 3.53431 1.961 0.0596 .
mpg 0.32039 0.09138 3.506 0.0015 **
wt 1.39324 0.56286 2.475 0.0194 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.524 on 29 degrees of freedom
Multiple R-squared: 0.3191, Adjusted R-squared: 0.2722
F-statistic: 6.797 on 2 and 29 DF, p-value: 0.003796
bic1
and bic0
bayestestR
Bayes Factor (\(BF_{10}\)) | Interpretation |
---|---|
> 100 | Extreme evidence for H1 |
30 – 100 | Very strong evidence for H1 |
10 – 30 | Strong evidence for H1 |
3 – 10 | Moderate evidence for H1 |
1 – 3 | Anecdotal evidence for H1 |
1 | No evidence |
1/3 – 1 | Anecdotal evidence for H0 |
1/3 – 1/10 | Moderate evidence for null H0 |
1/10 – 1/30 | Strong evidence for null H0 |
1/30 – 1/100 | Very strong evidence for H0 |
< 1/100 | Extreme evidence for H0 |
effectsize
package provides many automated interpretation functionsexp()
qsec ~ wt
vs. a constant model“I try to analyze the linear relationship between the sepal length and the sepal width of the iris flowers (iris
built in dataset). Should I control for Species?”
Pizzas can help us get a intuitive feeling for BFs
Welch Two Sample t-test
data: mtcars$mpg by mtcars$am
t = -3.7671, df = 18.332, p-value = 0.001374
alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
95 percent confidence interval:
-11.280194 -3.209684
sample estimates:
mean in group 0 mean in group 1
17.14737 24.39231
BayesFactor
package (by Richard Morey) where BFs are computed using a technique called Gaussian Quadrature 1 possible for simple models (not important)BayesFactor
package also implements correlation testsdrat
and qsec
# Compute the beta distribution
data$y <- dbeta(xspace, 1.5, 1.5)
# Rescale its x-axis
data$x2 <- (data$x - 0.5) * 2
data |>
ggplot(aes(y=y)) +
geom_line(aes(x=x), color="blue", linewidth=3) +
geom_line(aes(x=x2), color="orange", linewidth=2, alpha=0.9) +
coord_cartesian(xlim=c(-1.2, 1.2)) +
theme_bw()
BayesFactor
use?correlationBF
to find outNoninformative priors are assumed for the population means and variances of the two population; a shifted, scaled beta(1/rscale,1/rscale) prior distribution is assumed for \(rho\) […]
For the rscale argument, several named values are recognized: “medium.narrow”, “medium” [default], “wide”, and “ultrawide”. These correspond to r scale values of \(1/\sqrt{27}\), 1/3, \(1/\sqrt{3}\), and \(1\), respectively.
# Compute the beta distribution following the formula
# And store in the same dataframe
xspace <- seq(0, 1, by=0.01) # Original beta is ]0, 1[
data <- data.frame() # Inititalize empty dataframe
for(scale in names(rscales)) {
rscale <- rscales[[scale]]
dat <- data.frame(
y = dbeta(xspace, 1/rscale, 1/rscale),
x = (xspace - 0.5) * 2, # Rescale its x-axis
scale = scale
)
data <- rbind(data, dat)
}
Sepal.Length
and Sepal.Width
from the iris
dataset.Thank you!