File: priors.Rmd

package info (click to toggle)
r-cran-bayesfactor 0.9.12-4.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,512 kB
  • sloc: cpp: 1,555; sh: 16; makefile: 7
file content (131 lines) | stat: -rw-r--r-- 4,026 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!--
%\VignetteEngine{knitr}
%\VignetteIndexEntry{Prior checks}
\usepackage[utf8]{inputenc}
-->

![alt text](extra/logo.png)

------

```{r echo=FALSE,message=FALSE,results='hide'}

```

Prior checks
===========

```{r echo=FALSE,message=FALSE,results='hide'}
options(markdown.HTML.stylesheet = 'extra/manual.css')
library(knitr)
opts_chunk$set(dpi = 200, out.width = "67%") 
library(BayesFactor)
options(BFprogress = FALSE)
bfversion = BFInfo()
session = sessionInfo()[[1]]
rversion = paste(session$version.string," on ",session$platform,sep="")
set.seed(2)
```

The BayesFactor has a number of prior settings that should provide for a consistent Bayes factor. In this document, Bayes factors are checked for consistency.

Independent-samples t test and ANOVA
------
The independent samples $t$ test and ANOVA functions should provide the same answers with the default prior settings.

```{r}
# Create data
x <- rnorm(20)
x[1:10] = x[1:10] + .2
grp = factor(rep(1:2,each=10))

dat = data.frame(x=x,grp=grp)

t.test(x ~ grp, data=dat)
```

If the prior settings are consistent, then all three of these numbers should be the same.

```{r}
as.vector(ttestBF(formula = x ~ grp, data=dat))
as.vector(anovaBF(x~grp, data=dat))
as.vector(generalTestBF(x~grp, data=dat))
```

Regression and ANOVA
------
In a paired design with an additive random factor and and a fixed effect with two levels, the Bayes factors should be the same, regardless of whether we treat the fixed factor as a factor or as a dummy-coded covariate.

```{r}
# create some data
id = rnorm(10)
eff = c(-1,1)*1
effCross = outer(id,eff,'+')+rnorm(length(id)*2)
dat = data.frame(x=as.vector(effCross),id=factor(1:10), grp=factor(rep(1:2,each=length(id))))
dat$forReg = as.numeric(dat$grp)-1.5
idOnly = lmBF(x~id, data=dat, whichRandom="id")

summary(aov(x~grp+Error(id/grp),data=dat))

```

If the prior settings are consistent, these two numbers should be almost the same (within MC estimation error).
```{r}
as.vector(lmBF(x ~ grp+id, data=dat, whichRandom="id")/idOnly)
as.vector(lmBF(x ~ forReg+id, data=dat, whichRandom="id")/idOnly)
```

Independent t test and paired t test
-------
Given the effect size $\hat{\delta}=t\sqrt{N_{eff}}$, where the effective sample size $N_{eff}$ is the sample size in the one-sample case, and
\[
N_{eff} = \frac{N_1N_2}{N_1+N_2}
\]
in the two-sample case, the Bayes factors should be the same for the one-sample and two sample case, given the same observed effect size, save for the difference from the degrees of freedom that affects the shape of the noncentral $t$ likelihood. The difference from the degrees of freedom should get smaller for a given $t$ as $N_{eff}\rightarrow\infty$.

```{r}
# create some data
tstat = 3
NTwoSample = 500
effSampleSize = (NTwoSample^2)/(2*NTwoSample)
effSize = tstat/sqrt(effSampleSize)

# One sample
x0 = rnorm(effSampleSize)
x0 = (x0 - mean(x0))/sd(x0) + effSize

t.test(x0)

# Two sample
x1 = rnorm(NTwoSample)
x1 = (x1 - mean(x1))/sd(x1)
x2 = x1 + effSize

t.test(x2,x1)

```

These (log) Bayes factors should be approximately the same. 
```{r}
log(as.vector(ttestBF(x0)))
log(as.vector(ttestBF(x=x1,y=x2)))
```


Paired samples and ANOVA
------
A paired sample $t$ test and a linear mixed effects model should broadly agree. The two are based on different models &mdash; the paired t test has the participant effects substracted out, while the linear mixed effects model has a prior on the participant effects &mdash; but we'd expect them to lead to the same conclusions. 

These two Bayes factors should be lead to similar conclusions.

```{r}
# using the data previously defined
t.test(x~grp,data=dat,paired=TRUE)

as.vector(lmBF(x ~ grp+id, data=dat, whichRandom="id")/idOnly)
as.vector(ttestBF(x=dat$x[dat$grp==1],y=dat$x[dat$grp==2],paired=TRUE))
```

-------

*This document was compiled with version `r bfversion` of BayesFactor (`r rversion`).*