-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch25_2_script.R
59 lines (45 loc) · 1.36 KB
/
ch25_2_script.R
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
library(gapminder)
library(tidyverse)
library(modelr)
by_country <- gapminder %>%
group_by(country, continent) %>%
nest()
by_country
# Funcion para fitear modelos
country_model <- function(df) {
lm(lifeExp ~ year, data = df)
}
by_country <- by_country %>%
mutate(model = map(data, country_model))
# Agregar residuos y hacer unnesting
by_country <- by_country %>%
mutate(
resids = map2(data, model, add_residuals)
)
resids <- unnest(by_country, resids)
# Luego de hacer unnesting se puede graficar
ggplot(resids, aes(x = year, y = resid)) +
geom_line(aes(group = country), alpha = 1/3) +
geom_smooth(se = FALSE)
ggplot(resids, aes(x = year, y = resid)) +
geom_line(aes(group = country), alpha = 1/3) +
geom_smooth(se = FALSE) +
facet_wrap(~continent)
# Metricas de calidad de los modelos
glance_models <-
by_country %>%
mutate(glance = map(model, broom::glance)) %>%
unnest(glance, .drop = TRUE)
# Mirando modelos con bajo ajuste
glance_models %>%
arrange(r.squared)
glance_models %>%
ggplot(aes(continent, r.squared)) +
geom_jitter()
# Extrayendo paises con particularmente bajo R cuadrado
bad_fit <- glance_models %>% filter(r.squared < 0.25)
gapminder %>%
semi_join(bad_fit, by = "country") %>%
ggplot(aes(year, lifeExp, color = country)) +
geom_line()
# Aca se observa el efecto del genocidio de Rwanda y la epidemia del VIH