-
Notifications
You must be signed in to change notification settings - Fork 1
/
height_weight.Rmd
183 lines (127 loc) · 3.78 KB
/
height_weight.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
# 身高体重 {#height-weight}
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(tidybayes)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
## 线性模型
从最简单的线性模式开始
$$
y_n = \alpha + \beta x_n + \epsilon_n \quad \text{where}\quad
\epsilon_n \sim \operatorname{normal}(0,\sigma).
$$
等价于
$$
y_n - (\alpha + \beta X_n) \sim \operatorname{normal}(0,\sigma),
$$
进一步等价
$$
y_n \sim \operatorname{normal}(\alpha + \beta X_n, \, \sigma).
$$
```{r bayes-simuate, eval=FALSE}
alpha_real <- 10
beta_real <- 3
sigma_real <- 2
df <- tibble(
x = runif(30, 1, 8),
y = rnorm(30, alpha_real + beta_real * x, sd = sigma_real)
)
```
```{r, eval=FALSE}
stan_program <- "
data {
int<lower=0> N;
vector[N] x;
vector[N] y;
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
y ~ normal(alpha + beta * x, sigma);
}
generated quantities {
vector[N] y_rep;
for (n in 1:N)
y_rep[n] = normal_rng(alpha + beta * x[n], sigma);
}
"
stan_data <- df %>%
tidybayes::compose_data(
N = nrow(.),
x = x,
y = y
)
fit_normal <- stan(model_code = stan_program, data = stan_data)
```
```{r include=FALSE}
# 运行stan代码,导致渲染bookdown报错,不知道为什么,先用这边笨办法凑合吧
#
#save(fit_normal,
# stan_data,
# alpha_real,
# beta_real,
# sigma_real,
# file = here::here("stan", "stan_data_normal.Rdata")
# )
load(here::here("stan", "stan_data_normal.Rdata"))
```
### 模型输出
```{r}
fit_normal
```
### 模型评估
```{r}
rstan::traceplot(fit_normal, pars = c("alpha", "beta", "sigma"))
```
```{r, eval=FALSE}
rstan::extract(fit_normal, par = c("alpha", "beta"))
rstan::extract(fit_normal, par = "alpha")$alpha
rstan::extract(fit_normal, par = "beta")$beta
```
```{r, eval=FALSE}
fit_normal %>%
tidybayes::gather_draws(alpha, beta) %>%
ggplot(aes(x = .value, y = as_factor(.variable)) ) +
ggdist::stat_halfeye() +
geom_vline(xintercept = c(alpha_real, beta_real))
```
事实上,`bayesplot`宏包提供了大量模型评估函数,大爱!!
```{r, message=FALSE, results=FALSE}
true_alpha_beta <- c(alpha_real, beta_real, sigma_real)
posterior_alpha_beta <-
as.matrix(fit_normal, pars = c('alpha','beta', 'sigma'))
bayesplot::mcmc_recover_hist(posterior_alpha_beta, true = true_alpha_beta)
```
```{r}
y_rep <- as.matrix(fit_normal, pars = "y_rep")
bayesplot::ppc_dens_overlay(y = stan_data$y, yrep = y_rep[1:200, ])
```
```{r bayes-09}
y_rep <- as.matrix(fit_normal, pars = "y_rep")
bayesplot::ppc_intervals(y = stan_data$y, yrep = y_rep, x = stan_data$x)
```
## bayesian workflow
## 参考资料
- https://mc-stan.org/
- https://github.com/jgabry/bayes-workflow-book
- https://github.com/XiangyunHuang/masr/
- https://github.com/ASKurz/Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed/
- 《Regression and Other Stories》, Andrew Gelman, Cambridge University Press. 2020
- 《A Student's Guide to Bayesian Statistics》, Ben Lambert, 2018
- 《Statistical Rethinking: A Bayesian Course with Examples in R and STAN》 ( 2nd Edition), by Richard McElreath, 2020
- 《Bayesian Data Analysis》, Third Edition, 2013
- 《Doing Bayesian Data Analysis: A Tutorial with R, JAGS, and Stan》 (2nd Edition) John Kruschke, 2014
- 《Bayesian Models for Astrophysical Data: Using R, JAGS, Python, and Stan》, Joseph M. Hilbe, Cambridge University Press, 2017
```{r bayes-20, echo = F}
# remove the objects
# ls() %>% stringr::str_flatten(collapse = ", ")
rm(fit_normal, y_rep, stan_data, alpha_real, beta_real, sigma_real,posterior_alpha_beta, true_alpha_beta)
```
```{r bayes-21, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```