-
Notifications
You must be signed in to change notification settings - Fork 1
/
ANOVA.Rmd
158 lines (109 loc) · 2.78 KB
/
ANOVA.Rmd
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 方差分析 {#anova}
Bayesian ANOVA is usually represented as hierarchical model.
```{r}
library(tidyverse)
library(tidybayes)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
theme_set(bayesplot::theme_default())
```
## data
[数据来源](From http://personality-project.org/R/datasets/R.appendix1.data)
```{r}
alert <- tibble(Dosage = factor(rep(c("a", "b", "c"), each = 6)),
Alertness = c(30, 38, 35, 41, 27, 24, 32, 26, 31,
29, 27, 35, 21, 25, 17, 21, 20, 10))
alert
```
```{r}
alert %>%
ggplot(aes(x = Dosage, y = Alertness)) +
stat_summary()
```
## "Frequentist" ANOVA
```{r}
summary(aov(Alertness ~ Dosage, data = alert))
```
```{r}
aov(Alertness ~ Dosage, data = alert) %>%
TukeyHSD(which = "Dosage") %>%
broom::tidy()
```
## Bayesian ANOVA
$$
\begin{aligned}
\textrm{Alertness} & \sim \textrm{normal}(\mu_{j}, \, \sigma) \\
\mu_j &\sim \textrm{normal}(\gamma, \, \tau) \\
\gamma &\sim \textrm{normal}(0, \, 50) \\
\tau &\sim \textrm{gamma}(2, \, 1/8) \\
\sigma &\sim \textrm{student_t}(4, 0, 10)
\end{aligned}
$$
## stan 代码
```{r, warning=FALSE, message=FALSE}
stan_program <- '
data {
int<lower=1> N;
int<lower=2> n_groups;
vector[N] y;
int<lower=1, upper=n_groups> group_id[N];
}
parameters {
vector[n_groups] mu;
real<lower=0> sigma;
real gamma;
real<lower=0> tau;
}
model {
for (n in 1:N){
y[n] ~ normal(mu[group_id[n]], sigma);
}
mu ~ normal(gamma, tau);
gamma ~ normal(0, 50);
tau ~ gamma(2, 0.125);
sigma ~ student_t(4, 0, 10);
}
generated quantities {
real mu_diff_b_a;
real mu_diff_c_a;
real mu_diff_c_b;
mu_diff_b_a = mu[2] - mu[1];
mu_diff_c_a = mu[3] - mu[1];
mu_diff_c_b = mu[3] - mu[2];
}
'
stan_data <- alert %>%
tidybayes::compose_data(
N = nrow(.),
n_groups = n_distinct(Dosage),
group_id = Dosage,
y = Alertness
)
stan_anova <- stan(model_code = stan_program, data = stan_data)
```
```{r}
stan_anova
```
```{r, fig.width = 5, fig.height = 3}
stan_anova %>%
tidybayes::gather_draws(mu[i]) %>%
ungroup() %>%
mutate(i = as_factor(i)) %>%
ggplot(aes(x = .value, y = i)) +
tidybayes::stat_halfeye()
```
```{r, fig.width = 5, fig.height = 3}
stan_anova %>%
tidybayes::gather_draws(mu_diff_b_a, mu_diff_c_a, mu_diff_c_b) %>%
ggplot(aes(x = .value, y = .variable)) +
stat_halfeye(
fill = "skyblue",
point_interval = mode_hdi,
.width = c(0.5, 0.89),
interval_colour = "red",
point_colour = "red"
) +
geom_vline(xintercept = 0, linetype = "dashed", size = 1) +
labs(x = "mu_diff", y = NULL)
```