-
Notifications
You must be signed in to change notification settings - Fork 219
/
bayesian_workflow.Rmd
424 lines (286 loc) · 8.68 KB
/
bayesian_workflow.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# 贝叶斯工作流程 {#bayesian-workflow}
```{r, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.showtext = TRUE
)
```
## 贝叶斯工作流程
1. 数据探索和准备
2. 全概率模型
3. 先验预测检查,利用先验模拟响应变量
4. 模型应用到模拟数据,看参数恢复情况
5. 模型应用到真实数据
6. 检查抽样效率和模型收敛情况
7. 模型评估和后验预测检查
8. 信息准则与交叉验证,以及模型选择
## 案例
我们用[ames房屋价格](https://bookdown.org/wangminjie/R4DS/eda-ames-houseprice.html#eda-ames-houseprice),演示贝叶斯数据分析的工作流程
```{r, message = FALSE, warning = FALSE}
library(tidyverse)
library(tidybayes)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
### 1) 数据探索和准备
```{r}
rawdf <- readr::read_rds("./demo_data/ames_houseprice.rds")
rawdf
```
为了简化,我们只关注房屋价格(sale_price)与房屋占地面积(lot_area)和所在地理位置(neighborhood)的关系,这里需要点准备工作
- 房屋价格与房屋占地面积这两个变量**对数化处理** (why ?)
- 地理位置变量转换**因子类型** (why ?)
- 房屋价格与房屋占地面积这两个变量**标准化处理** (why ?)
```{r}
df <- rawdf %>%
select(sale_price, lot_area, neighborhood) %>%
drop_na() %>%
mutate(
across(c(sale_price, lot_area), log),
across(neighborhood, as.factor)
) %>%
mutate(
across(c(sale_price, lot_area), ~ (.x - mean(.x)) /sd(.x) ),
)
head(df)
```
```{r}
df %>%
ggplot(aes(x = lot_area, y = sale_price)) +
geom_point(colour = "blue") +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x")
```
```{r}
df %>%
ggplot(aes(x = lot_area, y = sale_price)) +
geom_point(colour = "blue") +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x", fullrange = TRUE) +
facet_wrap(vars(neighborhood))
```
### 2) 数据模型
$$
\begin{align}
y_i &\sim \operatorname{Normal}(\mu_i, \sigma) \\
\mu_i &= \alpha_{j} + \beta * x_i \\
\alpha_j & \sim \operatorname{Normal}(0, 10)\\
\beta & \sim \operatorname{Normal}(0, 10) \\
\sigma &\sim \exp(1)
\end{align}
$$
如果建立了这样的数学模型,可以马上写出stan代码
```{r, warning=FALSE, message=FALSE}
stan_program <- "
data {
int<lower=1> n;
int<lower=1> n_neighbour;
int<lower=1> neighbour[n];
vector[n] lot;
vector[n] price;
real alpha_sd;
real beta_sd;
int<lower = 0, upper = 1> run_estimation;
}
parameters {
vector[n_neighbour] alpha;
real beta;
real<lower=0> sigma;
}
model {
vector[n] mu;
for (i in 1:n) {
mu[i] = alpha[neighbour[i]] + beta * lot[i];
}
alpha ~ normal(0, alpha_sd);
beta ~ normal(0, beta_sd);
sigma ~ exponential(1);
if(run_estimation == 1) {
target += normal_lpdf(price | mu, sigma);
}
}
generated quantities {
vector[n] log_lik;
vector[n] y_hat;
for (j in 1:n) {
log_lik[j] = normal_lpdf(price | alpha[neighbour[j]] + beta * lot[j], sigma);
y_hat[j] = normal_rng(alpha[neighbour[j]] + beta * lot[j], sigma);
}
}
"
```
### 3) 先验预测检查,利用先验模拟响应变量
有个问题,我们这个先验概率怎么来的呢?猜的,因为没有人知道它究竟是什么分布(如果您是这个领域的专家,就不是猜,而叫**合理假设**)。那到底合不合理,我们需要检验下。这里用到的技术是**先验预测检验**。怎么做?
- 首先,模拟先验概率分布
- 然后,通过先验和模型假定的线性关系,模拟相应的响应变量$y_i$(注意,不是真实的数据)
```{r}
stan_data <- df %>%
tidybayes::compose_data(
n_neighbour = n_distinct(neighborhood),
neighbour = neighborhood,
price = sale_price,
lot = lot_area,
alpha_sd = 10,
beta_sd = 10,
run_estimation = 0
)
model_only_prior_sd_10 <- stan(model_code = stan_program, data = stan_data,
chains = 1, iter = 2100, warmup = 2000)
dt_wide <- model_only_prior_sd_10 %>%
as.data.frame() %>%
select(`alpha[5]`, beta) %>%
rowwise() %>%
mutate(
set = list(tibble(
x = seq(from = -3, to = 3, length.out = 200),
y = `alpha[5]` + beta * x
))
)
ggplot() +
map(
dt_wide$set,
~ geom_line(data = ., aes(x = x, y = y), alpha = 0.2)
)
```
```{r}
stan_data <- df %>%
tidybayes::compose_data(
n_neighbour = n_distinct(neighborhood),
neighbour = neighborhood,
price = sale_price,
lot = lot_area,
alpha_sd = 1,
beta_sd = 1,
run_estimation = 0
)
model_only_prior_sd_1 <- stan(model_code = stan_program, data = stan_data,
chains = 1, iter = 2100, warmup = 2000)
dt_narrow <- model_only_prior_sd_1 %>%
as.data.frame() %>%
select(`alpha[5]`, beta) %>%
rowwise() %>%
mutate(
set = list(tibble(
x = seq(from = -3, to = 3, length.out = 200),
y = `alpha[5]` + beta * x
))
)
ggplot() +
map(
dt_narrow$set,
~ geom_line(data = ., aes(x = x, y = y), alpha = 0.2)
)
```
### 4) 模型应用到模拟数据,看参数恢复情况
```{r}
df_random_draw <- model_only_prior_sd_1 %>%
tidybayes::gather_draws(alpha[i], beta, sigma, y_hat[i], n = 1)
true_parameters <- df_random_draw %>%
filter(.variable %in% c("alpha", "beta", "sigma")) %>%
mutate(parameters = if_else(is.na(i), .variable, str_c(.variable, "_", i)))
y_sim <- df_random_draw %>%
filter(.variable == "y_hat") %>%
pull(.value)
```
模拟的数据`y_sim`,导入模型作为响应变量,
```{r, warning=FALSE, message=FALSE}
stan_data <- df %>%
tidybayes::compose_data(
n_neighbour = n_distinct(neighborhood),
neighbour = neighborhood,
price = y_sim, ## 这里是模拟数据
lot = lot_area,
alpha_sd = 1,
beta_sd = 1,
run_estimation = 1
)
model_on_fake_dat <- stan(model_code = stan_program, data = stan_data)
```
看参数恢复的如何
```{r}
model_on_fake_dat %>%
tidybayes::gather_draws(alpha[i], beta, sigma) %>%
ungroup() %>%
mutate(parameters = if_else(is.na(i), .variable, str_c(.variable, "_", i))) %>%
ggplot(aes(x = .value)) +
geom_density() +
geom_vline(
data = true_parameters,
aes(xintercept = .value),
color = "red"
) +
facet_wrap(vars(parameters), ncol = 5, scales = "free")
```
如果觉得上面的过程很麻烦,可以直接用`bayesplot::mcmc_recover_hist()`
```{r, message=FALSE, results=FALSE}
posterior_alpha_beta <-
as.matrix(model_on_fake_dat, pars = c('alpha', 'beta', 'sigma'))
bayesplot::mcmc_recover_hist(posterior_alpha_beta, true = true_parameters$.value)
```
### 5) 模型应用到真实数据
应用到真实数据
```{r, warning=FALSE, message=FALSE}
stan_data <- df %>%
tidybayes::compose_data(
n_neighbour = n_distinct(neighborhood),
neighbour = neighborhood,
price = sale_price, ## 这里是真实数据
lot = lot_area,
alpha_sd = 1,
beta_sd = 1,
run_estimation = 1
)
model <- stan(model_code = stan_program, data = stan_data)
```
### 6) 检查抽样效率和模型收敛情况
- 检查traceplot
```{r}
rstan::traceplot(model)
```
- 检查neff 和 Rhat
```{r}
print(model,
pars = c("alpha", "beta", "sigma"),
probs = c(0.025, 0.50, 0.975),
digits_summary = 3
)
```
- 检查posterior sample
```{r}
model %>%
tidybayes::gather_draws(alpha[i], beta, sigma) %>%
ungroup() %>%
mutate(parameters = if_else(is.na(i), .variable, str_c(.variable, "_", i))) %>%
ggplot(aes(x = .value, y = parameters)) +
ggdist::stat_halfeye()
```
事实上,`bayesplot`宏包很强大也很好用
```{r}
bayesplot::mcmc_combo(
as.array(model),
combo = c("dens_overlay", "trace"),
pars = c('alpha[1]', 'beta', 'sigma')
)
```
### 7) 模型评估和后验预测检查
```{r}
yrep <- extract(model)[["y_hat"]]
samples <- sample(nrow(yrep), 300)
bayesplot::ppc_dens_overlay(as.vector(df$sale_price), yrep[samples, ])
```
## Conclusion
## 作业
- 前面的模型只有变化的截距(即不同的商圈有不同的截距)斜率是固定的,要求:增加一个变化的斜率
$$
\begin{align}
y_i &\sim \operatorname{Normal}(\mu_i, \sigma) \\
\mu_i &= \alpha_{j} + \beta_{j} * x_i \\
\alpha_j & \sim \operatorname{Normal}(0, 1)\\
\beta_j & \sim \operatorname{Normal}(0, 1) \\
\sigma &\sim \exp(1)
\end{align}
$$
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```