-
Notifications
You must be signed in to change notification settings - Fork 220
/
bayesian_tidybayes.Rmd
215 lines (168 loc) · 4.1 KB
/
bayesian_tidybayes.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 抽样数据的规整与可视化 {#bayesian-tidybayes}
```{r, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.showtext = TRUE
)
```
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(tidybayes)
library(ggdist)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
在贝叶斯抽样样本量比较大,我们需要规整和可视化,就需要借助一些函数。这里简单介绍[tidybayes](https://github.com/mjskay/tidybayes)宏包和它的姊妹宏包
[ggdist](https://mjskay.github.io/ggdist/),更多的技术参数见官方手册。
## 企鹅案例
问题简化,我们只挑选Gentoo类企鹅
```{r}
library(palmerpenguins)
gentoo <- penguins %>%
drop_na() %>%
filter(species == "Gentoo")
gentoo
```
先看下两个变量的关系
```{r}
gentoo %>%
ggplot(aes(x = bill_length_mm, bill_depth_mm)) +
geom_point()
```
## Stan模型
假设我们建立最简单的线性模型,其中预测因子bill_length_mm,被解释变量是 bill_depth_mm
$$
\begin{align}
y_n &\sim \operatorname{normal}(\mu_n, \,\, \sigma)\\
\mu_n &= \alpha + \beta x_n
\end{align}
$$
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int<lower=0> N;
vector[N] y;
vector[N] x;
int<lower=0> M;
vector[M] new_x;
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
y ~ normal(alpha + beta * x, sigma);
alpha ~ normal(0, 10);
beta ~ normal(0, 10);
sigma ~ exponential(1);
}
generated quantities {
vector[M] y_fit;
vector[M] y_rep;
for (n in 1:M) {
y_fit[n] = alpha + beta * new_x[n];
y_rep[n] = normal_rng(alpha + beta * new_x[n], sigma);
}
}
"
library(modelr)
newdata <- gentoo %>%
data_grid(
bill_length_mm = seq_range(bill_length_mm, 100)
)
# or
# newdata <- data.frame(
# bill_length_mm = seq(min(gentoo$bill_length_mm), max(gentoo$bill_length_mm), length.out = 100)
# )
stan_data <- list(
N = nrow(gentoo),
x = gentoo$bill_length_mm,
y = gentoo$bill_depth_mm,
M = nrow(newdata),
new_x = newdata$bill_length_mm
)
fit <- stan(model_code = stan_program, data = stan_data)
```
## 抽样
```{r}
draws <- fit %>%
tidybayes::gather_draws(alpha, beta, sigma)
draws
```
## 统计汇总
```{r}
draws %>%
ggdist::mean_qi(.width = c(0.65, 0.89) )
```
## 可视化
- `geom_slabinterval() / stat_slabinterval()` family
```{r}
draws %>%
ggplot(aes(x = .value, y = .variable)) +
ggdist::stat_interval()
```
```{r}
draws %>%
ggplot(aes(x = .value, y = .variable)) +
ggdist::stat_slab()
```
```{r}
draws %>%
ggplot(aes(x = .value, y = .variable)) +
ggdist::stat_slabinterval()
```
```{r}
draws %>%
filter(.variable %in% c("beta", "sigma")) %>%
ggplot(aes(x = .value, y = .variable)) +
ggdist::stat_slabinterval() +
facet_grid(~ .variable, labeller = "label_both", scales = "free")
```
- `geom_dotsinterval() / stat_dotsinterval()` family
```{r}
draws %>%
filter(.variable %in% c("beta", "sigma")) %>%
ggplot(aes(x = .value, y = .variable)) +
stat_dotsinterval(
quantiles = 200,
justification = -0.1,
slab_color = "black",
slab_fill = "orange",
interval_color = "red"
)
```
- `geom_lineribbon() / stat_lineribbon()` family
```{r}
fit %>%
tidybayes::gather_draws(y_fit[i]) %>%
ggdist::median_qi(.width = c(0.89)) %>%
bind_cols(newdata) %>%
ggplot() +
geom_point(
data = gentoo,
aes(bill_length_mm, bill_depth_mm)
) +
geom_lineribbon(
aes(x = bill_length_mm, y = .value, ymin = .lower, ymax = .upper),
alpha = 0.3,
fill = "gray50"
) +
theme_classic() +
scale_fill_brewer(direction = -1)
```
- 组合
```{r}
penguins %>%
ggplot(aes(y = species, x = bill_length_mm, fill = species)) +
stat_slab(aes(thickness = after_stat(pdf*n)), scale = 0.7) +
stat_dotsinterval(side = "bottom", scale = 0.7, slab_size = NA) +
scale_fill_brewer(palette = "Set2") +
ggtitle("Rain cloud plot")
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```