-
Notifications
You must be signed in to change notification settings - Fork 230
/
many-models.Rmd
272 lines (188 loc) · 6.27 KB
/
many-models.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
# Many models {#many-models .r4ds-section}
## Introduction {#introduction-17 .r4ds-section}
```{r setup,message=FALSE,cache=FALSE}
library("modelr")
library("tidyverse")
library("gapminder")
```
## gapminder {#gapminder .r4ds-section}
### Exercise 25.2.1 {.unnumbered .exercise data-number="25.2.1"}
<div class="question">
A linear trend seems to be slightly too simple for the overall trend.
Can you do better with a quadratic polynomial?
How can you interpret the coefficients of the quadratic?
Hint you might want to transform year so that it has mean zero.)
</div>
<div class="answer">
The following code replicates the analysis in the chapter but replaces the function `country_model()` with a regression that includes the year squared.
```{r, eval = FALSE}
lifeExp ~ poly(year, 2)
```
```{r}
country_model <- function(df) {
lm(lifeExp ~ poly(year - median(year), 2), data = df)
}
by_country <- gapminder %>%
group_by(country, continent) %>%
nest()
by_country <- by_country %>%
mutate(model = map(data, country_model))
```
```{r}
by_country <- by_country %>%
mutate(
resids = map2(data, model, add_residuals)
)
by_country
```
```{r}
unnest(by_country, resids) %>%
ggplot(aes(year, resid)) +
geom_line(aes(group = country), alpha = 1 / 3) +
geom_smooth(se = FALSE)
```
```{r}
by_country %>%
mutate(glance = map(model, broom::glance)) %>%
unnest(glance, .drop = TRUE) %>%
ggplot(aes(continent, r.squared)) +
geom_jitter(width = 0.5)
```
</div>
### Exercise 25.2.2 {.unnumbered .exercise data-number="25.2.2"}
<div class="question">
Explore other methods for visualizing the distribution of $R^2$ per continent.
You might want to try the ggbeeswarm package, which provides similar methods for avoiding overlaps as jitter, but uses deterministic methods.
</div>
<div class="answer">
See exercise 7.5.1.1.6 for more on ggbeeswarm
```{r}
library("ggbeeswarm")
by_country %>%
mutate(glance = map(model, broom::glance)) %>%
unnest(glance, .drop = TRUE) %>%
ggplot(aes(continent, r.squared)) +
geom_beeswarm()
```
</div>
### Exercise 25.2.3 {.unnumbered .exercise data-number="25.2.3"}
<div class="question">
To create the last plot (showing the data for the countries with the worst model fits),
we needed two steps:
we created a data frame with one row per country
and then semi-joined it to the original dataset.
It’s possible to avoid this join if we use `unnest()` instead of `unnest(.drop = TRUE)`.
How?
</div>
<div class="answer">
```{r}
gapminder %>%
group_by(country, continent) %>%
nest() %>%
mutate(model = map(data, ~lm(lifeExp ~ year, .))) %>%
mutate(glance = map(model, broom::glance)) %>%
unnest(glance) %>%
unnest(data) %>%
filter(r.squared < 0.25) %>%
ggplot(aes(year, lifeExp)) +
geom_line(aes(color = country))
```
</div>
## List-columns {#list-columns-1 .r4ds-section}
`r no_exercises()`
## Creating list-columns {#creating-list-columns .r4ds-section}
### Exercise 25.4.1 {.unnumbered .exercise data-number="25.4.1"}
<div class="question">
List all the functions that you can think of that take a atomic vector and return a list.
</div>
<div class="answer">
Many functions in the stringr package take a character vector as input and return a list.
```{r}
str_split(sentences[1:3], " ")
str_match_all(c("abc", "aa", "aabaa", "abbbc"), "a+")
```
The `map()` function takes a vector and always returns a list.
```{r}
map(1:3, runif)
```
</div>
### Exercise 25.4.2 {.unnumbered .exercise data-number="25.4.2"}
<div class="question">
Brainstorm useful summary functions that, like `quantile()`, return multiple values.
</div>
<div class="answer">
Some examples of summary functions that return multiple values are the following.
```{r}
range(mtcars$mpg)
fivenum(mtcars$mpg)
boxplot.stats(mtcars$mpg)
```
</div>
### Exercise 25.4.3 {.unnumbered .exercise data-number="25.4.3"}
<div class="question">
What’s missing in the following data frame?
How does `quantile()` return that missing piece?
Why isn’t that helpful here?
```{r}
mtcars %>%
group_by(cyl) %>%
summarise(q = list(quantile(mpg))) %>%
unnest()
```
</div>
<div class="answer">
The particular quantiles of the values are missing, e.g. `0%`, `25%`, `50%`, `75%`, `100%`. `quantile()` returns these in the names of the vector.
```{r}
quantile(mtcars$mpg)
```
Since the `unnest` function drops the names of the vector, they aren't useful here.
</div>
### Exercise 25.4.4 {.unnumbered .exercise data-number="25.4.4"}
<div class="question">
What does this code do?
Why might might it be useful?
```r
mtcars %>%
group_by(cyl) %>%
summarise_each(funs(list))
```
</div>
<div class="answer">
```{r}
mtcars %>%
group_by(cyl) %>%
summarise_each(funs(list))
```
It creates a data frame in which each row corresponds to a value of `cyl`,
and each observation for each column (other than `cyl`) is a vector of all the values of that column for that value of `cyl`.
It seems like it should be useful to have all the observations of each variable for each group, but off the top of my head, I can't think of a specific use for this.
But, it seems that it may do many things that `dplyr::do` does.
</div>
## Simplifying list-columns {#simplifying-list-columns .r4ds-section}
### Exercise 25.5.1 {.unnumbered .exercise data-number="25.5.1"}
<div class="question">
Why might the `lengths()` function be useful for creating atomic vector columns from list-columns?
</div>
<div class="answer">
The `lengths()` function returns the lengths of each element in a list.
It could be useful for testing whether all elements in a list-column are the same length.
You could get the maximum length to determine how many atomic vector columns to create.
It is also a replacement for something like `map_int(x, length)` or `sapply(x, length)`.
</div>
### Exercise 25.5.2 {.unnumbered .exercise data-number="25.5.2"}
<div class="question">
List the most common types of vector found in a data frame.
What makes lists different?
</div>
<div class="answer">
The common types of vectors in data frames are:
- `logical`
- `numeric`
- `integer`
- `character`
- `factor`
All of the common types of vectors in data frames are atomic.
Lists are not atomic since they can contain other lists and other vectors.
</div>
## Making tidy data with broom {#making-tidy-data-with-broom .r4ds-section}
`r no_exercises()`