-
Notifications
You must be signed in to change notification settings - Fork 220
/
tidystats_broom.Rmd
306 lines (210 loc) · 6.49 KB
/
tidystats_broom.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
# 模型输出结果的规整 {#tidystats-broom}
```{r, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.showtext = TRUE
)
```
## 案例
还是用第 \@ref(tidyverse-ggplot2-geom) 章的`gapminder`案例
```{r broom-1}
library(tidyverse)
library(gapminder)
gapminder
```
### 可视化探索
画个简单的图
```{r broom-2}
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = lifeExp)) +
geom_point(alpha = 0.2)
```
我们想用**不同的模型**拟合`log(gdpPercap)`与`lifeExp`的关联
```{r broom-3}
library(colorspace)
model_colors <- colorspace::qualitative_hcl(4, palette = "dark 2")
# model_colors <- c("darkorange", "purple", "cyan4")
ggplot(
data = gapminder,
mapping = aes(x = log(gdpPercap), y = lifeExp)
) +
geom_point(alpha = 0.2) +
geom_smooth(
method = "lm",
aes(color = "OLS", fill = "OLS") # one
) +
geom_smooth(
method = "lm", formula = y ~ splines::bs(x, df = 3),
aes(color = "Cubic Spline", fill = "Cubic Spline") # two
) +
geom_smooth(
method = "loess",
aes(color = "LOESS", fill = "LOESS") # three
) +
scale_color_manual(name = "Models", values = model_colors) +
scale_fill_manual(name = "Models", values = model_colors) +
theme(legend.position = "top")
```
### 简单模型
还是回到我们今天的主题。我们建立一个简单的线性模型
```{r broom-4}
out <- lm(
formula = lifeExp ~ gdpPercap + pop + continent,
data = gapminder
)
out
```
```{r broom-5, eval=FALSE}
str(out)
```
```{r broom-6}
summary(out)
```
模型的输出结果是一个复杂的list,图 \@ref(fig:lm-object-schematic)给出了`out`的结构
```{r lm-object-schematic, out.width = '35%', echo = FALSE, fig.cap = '线性模型结果的示意图'}
knitr::include_graphics("images/lm-object-schematic.png")
```
我们发现`out`对象包含了很多元素,比如系数、残差、模型残差自由度等等,用读取列表的方法可以直接读取
```{r broom-7, eval=FALSE}
out$coefficients
out$residuals
out$fitted.values
```
事实上,前面使用的`suammary()`函数只是选取和打印了`out`对象的一小部分信息,同时这些信息的结构不适合用`dplyr`操作和`ggplot2`画图。
## broom
为规整模型结果,这里我们推荐用[David Robinson](http://varianceexplained.org/about/) 开发的`broom`宏包。
```{r broom-8, message = FALSE, warning = FALSE}
library(broom)
```
`broom` 宏包将常用的100多种模型的输出结果规整成数据框
`tibble()`的格式,在模型比较和可视化中就可以方便使用`dplyr`函数了。
`broom` 提供了三个主要的函数:
- `tidy()` 提取模型输出结果的主要信息,比如 `coefficients` 和 `t-statistics`
- `glance()` 把模型视为一个整体,提取如 `F-statistic`,`model deviance` 或者 `r-squared`等信息
- `augment()` 模型输出的信息添加到建模用的数据集中,比如`fitted values` 和 `residuals`
### tidy
```{r broom-9}
tidy(out)
```
```{r broom-10}
out %>%
tidy() %>%
ggplot(mapping = aes(
x = term,
y = estimate
)) +
geom_point() +
coord_flip()
```
可以很方便的获取系数的置信区间
```{r broom-11}
out %>%
tidy(conf.int = TRUE)
```
```{r broom-12}
out %>%
tidy(conf.int = TRUE) %>%
filter(!term %in% c("(Intercept)")) %>%
ggplot(aes(
x = reorder(term, estimate),
y = estimate, ymin = conf.low, ymax = conf.high
)) +
geom_pointrange() +
coord_flip() +
labs(x = "", y = "OLS Estimate")
```
### augment
`augment()`会返回一个数据框,这个数据框是在原始数据框的基础上,增加了模型的拟合值(`.fitted`), 拟合值的标准误(`.se.fit`), 残差(`.resid`)等列。
```{r broom-13}
augment(out)
```
```{r broom-14}
out %>%
augment() %>%
ggplot(mapping = aes(x = lifeExp, y = .fitted)) +
geom_point()
```
### glance
`glance()` 函数也会返回数据框,但这个数据框只有一行,内容实际上是`summary()`输出结果的最底下一行。
```{r broom-15}
glance(out)
```
## 应用
broom的三个主要函数在分组统计建模时,格外方便。
```{r broom-16}
penguins <-
palmerpenguins::penguins %>%
drop_na()
```
```{r broom-17}
penguins %>%
group_nest(species) %>%
mutate(model = purrr::map(data, ~ lm(bill_depth_mm ~ bill_length_mm, data = .))) %>%
mutate(glance = purrr::map(model, ~ broom::glance(.))) %>%
tidyr::unnest(glance)
```
```{r broom-18}
fit_ols <- function(df) {
lm(body_mass_g ~ bill_depth_mm + bill_length_mm, data = df)
}
out_tidy <- penguins %>%
group_nest(species) %>%
mutate(model = purrr::map(data, fit_ols)) %>%
mutate(tidy = purrr::map(model, ~ broom::tidy(.))) %>%
tidyr::unnest(tidy) %>%
dplyr::filter(!term %in% "(Intercept)")
out_tidy
```
```{r broom-19}
out_tidy %>%
ggplot(aes(
x = species, y = estimate,
ymin = estimate - 2 * std.error,
ymax = estimate + 2 * std.error,
color = term
)) +
geom_pointrange(position = position_dodge(width = 0.25)) +
theme(legend.position = "top") +
labs(x = NULL, y = "Estimate", color = "coef")
```
## 练习
假定数据是
```{r broom-19-1}
df <- tibble(
x = runif(30, 2, 10),
y = -2*x + rnorm(30, 0, 5)
)
df
```
用`broom::augment()`和ggplot2做出类似的残差图
```{r broom-19-2, echo=FALSE, out.width='90%', fig.align = "left"}
fitted_lm <- lm(y ~ x, data = df)
#fitted_lm %>% broom::augment()
#fitted_lm %>% broom::augment_columns(df, type = "lm")
# residuals plot adapted from: https://drsimonj.svbtle.com/visualising-residuals
fitted_lm %>%
broom::augment() %>%
select(x, y, predicted = .fitted, residuals = .resid) %>%
ggplot(aes(x = x, y = y)) +
geom_smooth(method = "lm", se = FALSE, color = "gray50") +
geom_segment(aes(xend= x, yend = predicted), alpha = 0.2) +
geom_point(aes(size = abs(residuals), color = abs(residuals))) +
scale_color_continuous(low = "grey", high = "#FFB612", aesthetics = c("fill", "color")) +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "gray"),
panel.background = element_rect(fill = "#f0f0f0", color = NA),
plot.background = element_rect(fill = "#f0f0f0", color = NA),
axis.ticks = element_blank(),
legend.position = "none"
)
```
```{r broom-20, echo = F}
# remove the objects
# rm(list=ls())
rm(out, out_tidy, penguins, model_colors, fit_ols, df)
```
```{r broom-21, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```