-
Notifications
You must be signed in to change notification settings - Fork 6
/
pm-lecture-note.Rmd
195 lines (158 loc) · 5.11 KB
/
pm-lecture-note.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
---
layout: page
title: RStudio Trainer Program
subtitle: "Tidyverse: Predictive Model - Lecture Note"
author:
name: xwMOOC
url: https://www.facebook.com/groups/tidyverse/
affiliation: Tidyverse Korea
date: "`r Sys.Date()`"
output:
html_document:
toc: yes
toc_float: true
highlight: tango
code_folding: show
number_section: true
self_contained: true
editor_options:
chunk_output_type: console
---
``` {r, include=FALSE}
# source("tools/chunk-options.R")
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE,
comment="", digits = 3, tidy = FALSE, prompt = FALSE, fig.align = 'center')
```
# Lecture Note {#RStudio-lecture-note}
- Identify the target variable
- Perform EDA
- Visualize relationships
# One Country - Data {#RStudio-lecture-note-one}
## Dataframe {#RStudio-lecture-note-df}
```{r gapminder}
library(tidyverse)
library(gapminder)
korea_tbl <- gapminder %>%
filter(str_detect(country, "Korea, Rep.")) %>%
dplyr::select(year, pop, gdpPercap, lifeExp) %>%
mutate(pop = pop / 10^6)
DT::datatable(korea_tbl)
```
## Visualize a relation {#gapminder-visualize-relation}
```{r gapminder-one-visualize}
korea_tbl %>%
ggplot(aes(x=gdpPercap, y=lifeExp)) +
geom_point() +
geom_line() +
labs(x="GDP per capita",
y="Life Expectancy",
title="Relationship between GDP per Capita and Life Expectancy")
```
## Visualize many relations {#gapminder-visualize-relations}
```{r gapminder-one-visualize-many}
korea_tbl %>%
gather(variable, value, -lifeExp) %>%
ggplot(aes(x=value, y=lifeExp)) +
geom_point() +
geom_line() +
facet_wrap(~variable)
```
```{r gapminder-one-visualize-many-decoration}
korea_tbl %>%
gather(variable, value, -lifeExp) %>%
ggplot(aes(x=value, y=lifeExp)) +
geom_point() +
geom_line() +
facet_wrap(~variable, scale = "free") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x="",
y="Life Expectancy",
title="Relationship between Life Expectancy and main factors")
```
hint: [stackoverflow, "Rotating and spacing axis labels in ggplot2"](https://stackoverflow.com/questions/1330989/rotating-and-spacing-axis-labels-in-ggplot2)
# One Country - Model {#RStudio-lecture-note-one-lm}
```{r gapminder-lm}
korea_lm <- lm(lifeExp ~ year, data=korea_tbl)
summary(korea_lm)
```
## Introduce `broom` {#RStudio-lecture-note-one-lm-broom}
```{r gapminder-lm-summary}
library(broom)
glance(korea_lm)
tidy(korea_lm)
augment(korea_lm) %>%
select(.resid) %>%
summary(.)
```
## One Country - Model with multiple variables {#RStudio-lecture-note-one-lm-variables}
```{r gapminder-lm-variable}
korea_multiple_lm <- lm(lifeExp ~ ., data=korea_tbl)
glance(korea_multiple_lm)
tidy(korea_multiple_lm)
```
## One Country - Best Model Selection {#RStudio-lecture-note-one-lm-best}
```{r gapminder-lm2-best}
korea_full_lm <- lm(lifeExp ~ ., data = korea_tbl)
korea_step <- MASS::stepAIC(korea_full_lm, trace = FALSE)
korea_step
```
# Many Simple Models {#RStudio-lecture-note-many-simple}
```{r gapminder-lm-many-models}
gap_nest <- gapminder %>%
select(-continent) %>%
group_by(country) %>%
nest()
## Hello model
(gap_many_tbl <- gap_nest %>%
mutate(model = map(data, ~lm(lifeExp ~ year, data=.))) %>%
mutate(model_glance = map(model, broom::glance),
rsquare = map_dbl(model_glance, ~.$r.squared)) %>%
arrange(rsquare))
```
## Let's visualize best and worst {#RStudio-lecture-note-worst-best}
```{r gapminder-lm-many-models-best-worst}
worst_country <- gap_many_tbl %>%
pull(country) %>%
head(3) %>%
as.character()
best_country <- gap_many_tbl %>%
pull(country) %>%
tail(3) %>%
as.character()
countries <- c(worst_country, best_country)
gapminder %>%
filter(country %in% countries) %>%
mutate(country = as.character(country)) %>%
select(country, year, lifeExp) %>%
mutate(country = fct_relevel(country, countries)) %>%
mutate(worst_best = if_else(country %in% best_country, "Best", "Worst")) %>%
ggplot(aes(x=year, y=lifeExp, color=worst_best)) +
geom_point() +
geom_line() +
facet_wrap(~country, scale = "free") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x="",
y="Life Expectancy",
title="Relationship between Life Expectancy and main factors")
```
## One more thing {#RStudio-lecture-note-worst-best-one-more}
```{r gapminder-lm-many-models-best-worst-one-more}
gap_many_tbl %>%
select(country, data, rsquare) %>%
filter(country %in% countries) %>%
unnest(data) %>%
ungroup() %>%
mutate(country = as.character(country)) %>%
select(country, year, lifeExp, rsquare) %>%
mutate(country = fct_relevel(country, countries)) %>%
mutate(country = glue::glue("{country}, {round(rsquare, 3)}")) %>%
ggplot(aes(x=year, y=lifeExp)) +
geom_point() +
geom_line() +
facet_wrap(~country, scale = "free") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x="",
y="Life Expectancy",
title="Relationship between Life Expectancy and main factors",
color="")
```