forked from perlatex/R_for_Data_Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eda_height.Rmd
301 lines (213 loc) · 7.03 KB
/
eda_height.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
# 探索性数据分析-身高体重 {#eda-height}
```{r, eval=FALSE, include=FALSE}
# 数据模拟代码
library(tidyverse)
boy_mu_height <- 168
boy_mu_weight <- 118
sigma_a <- 5 # std dev in intercepts
sigma_b <- 3 # std dev in slopes
rho <- 0.8 # correlation between intercepts and slopes
mu <- c(boy_mu_height, boy_mu_weight)
sigmas <- c(sigma_a, sigma_b) # standard deviations
rho <- matrix(c(1, rho, # correlation matrix
rho, 1), nrow = 2)
# now matrix multiply to get covariance matrix
sigma <- diag(sigmas) %*% rho %*% diag(sigmas)
# how many cafes would you like?
n_obs <- 1000
set.seed(13) # used to replicate example
df_boys <-
MASS::mvrnorm(n_obs, mu, sigma) %>%
data.frame() %>%
set_names("height", "weight") %>%
as_tibble() %>%
mutate(gender = "male")
girl_mu_height <- 165
girl_mu_weight <- 110
sigma_a <- 5 # std dev in intercepts
sigma_b <- 4 # std dev in slopes
rho <- 0.7 # correlation between intercepts and slopes
mu <- c(girl_mu_height, girl_mu_weight)
sigmas <- c(sigma_a, sigma_b) # standard deviations
rho <- matrix(c(1, rho, # correlation matrix
rho, 1), nrow = 2)
# now matrix multiply to get covariance matrix
sigma <- diag(sigmas) %*% rho %*% diag(sigmas)
# how many cafes would you like?
n_obs <- 1000
df_girls <-
MASS::mvrnorm(n_obs, mu, sigma) %>%
data.frame() %>%
set_names("height", "weight") %>%
as_tibble() %>%
mutate(gender = "female")
df <- bind_rows(df_boys, df_girls)
```
## 案例分析
这是一份身高和体重的数据集
```{r eda-height-2}
library(tidyverse)
d <- read_csv("./demo_data/weight-height.csv")
d
```
```{r eda-height-3}
d %>% summarise(
across(everything(), ~ sum(is.na(.)))
)
```
## 可视化
### 画出不同性别的身高分布
常规答案
```{r eda-height-4}
d %>%
ggplot(aes(x = Height, fill = Gender)) +
geom_density(alpha = 0.5)
```
```{r eda-height-5}
d %>%
ggplot(aes(x = Height, fill = Gender)) +
geom_density(alpha = 0.5) +
facet_wrap(vars(Gender))
```
## 来点高级的
刚才我们看到了分面的操作,全局数据按照某个变量分组后,形成的若干个子集在不同的面板中分别展示出来。
这种方法很适合子集之间对比。事实上,我们看到每个子集的情况后,还很想知道全局的情况,以及子集在全局中的分布、状态或者位置。也就说,想对比子集和全局的情况。
所以我们期望(**子集之间对比,子集与全局对比**)。
具体方法:**用分面的方法高亮展示子集,同时在每个分面上添加全局(灰色背景)**
- 第一步,先把子集用分面的方法,分别画出来
```{r eda-height-6, eval = FALSE}
d %>%
ggplot(aes(x = Height)) +
geom_density() +
facet_wrap(vars(Gender))
```
- 第二步,添加整体的情况作为背景图层。因为第一步用到了分面,也就说会分组,但我们希望整体的背景图层不受分面信息影响,或者叫背景图层不需要分组,而是显示全部。也就说,要保证每个分面面板中的背景图都是一样的,因此,在这个geom_denstiy()图层中,构建不受facet_wrap()影响的数据,即删掉data的分组列。
```{r eda-height-7, eval = FALSE}
d %>%
ggplot(aes(x = Height)) +
geom_density(
data = d %>% select(-Gender)
) +
geom_density() +
facet_wrap(vars(Gender))
```
- 第三步,y轴的调整,我们希望保持密度的形状,同时希望y轴不用比例值而是用具体的count个数,这样整体和局部能放在一个标度下,
```{r eda-height-8, eval = FALSE}
d %>%
ggplot(aes(x = Height, y = after_stat(count))) +
geom_density(
data = d %>% select(-Gender)
) +
geom_density() +
facet_wrap(vars(Gender))
```
- 第四步, 配色。
[配色网站](https://coolors.co/50514f-f25f5c-ffe066-247ba0-70c1b3)选颜色
"Male", "Female" 是Gender已经存在的分组。另外,我们在背景图层,新增了一个组"all people",这样,整个图就有三个分组(三个color组),那么,我们可以在scale_fill_manual中统一设置和指定。
```{r eda-height-9, eval = FALSE}
density_colors <- c(
"Male" = "#247BA0",
"Female" = "#F25F5C",
"all people" = "grey85"
)
```
```{r eda-height-10, eval = FALSE}
d %>%
ggplot(aes(x = Height, y = after_stat(count))) +
geom_density(
data = df %>% select(-Gender),
aes(fill = "all people", color = "all people")
) +
geom_density(aes(color = Gender, fill = Gender)) +
facet_wrap(vars(Gender)) +
scale_fill_manual(name = NULL, values = density_colors) +
scale_color_manual(name = NULL, values = density_colors) +
theme_minimal() +
theme(legend.position = "bottom")
```
### 完整代码
```{r eda-height-11}
density_colors <- c(
"Male" = "#247BA0",
"Female" = "#F25F5C",
"all people" = "grey80"
)
scales::show_col(density_colors)
```
```{r eda-height-12}
d %>%
ggplot(aes(x = Height, y = after_stat(count))) +
geom_density(
data = d %>% dplyr::select(-Gender),
aes(fill = "all people", color = "all people")
) +
geom_density(aes(color = Gender, fill = Gender)) +
facet_wrap(vars(Gender)) +
scale_fill_manual(name = NULL, values = density_colors) +
scale_color_manual(name = NULL, values = density_colors) +
theme_minimal() +
theme(legend.position = "bottom")
```
或者,用不同的主题风格
```{r eda-height-13}
density_colors <- c(
"Male" = "#56B4E9",
"Female" = "#EF8A17",
"all participants" = "grey85"
)
d %>%
ggplot(aes(x = Height, y = after_stat(count))) +
geom_density(
data = function(x) dplyr::select(x, -Gender),
aes(fill = "all participants", color = "all participants")
) +
geom_density(aes(fill = Gender, color = Gender)) +
facet_wrap(vars(Gender)) +
scale_color_manual(name = NULL, values = density_colors) +
scale_fill_manual(name = NULL, values = density_colors) +
cowplot::theme_minimal_hgrid(16) +
theme(legend.position = "bottom", legend.justification = "center")
```
### 画出不同性别的体重分布
```{r eda-height-14}
d %>%
ggplot(aes(x = Weight, fill = Gender)) +
geom_density(alpha = 0.5)
```
## 建模
### 身高与体重的散点图
```{r eda-height-15}
d %>%
ggplot(aes(x = Height, y = Weight, color = Gender)) +
geom_point()
```
### 建立身高与体重的线性模型
```{r eda-height-16}
fit <- lm(Weight ~ 1 + Height, data = d)
summary(fit)
```
```{r eda-height-17}
broom::tidy(fit)
```
### 建立不同性别下的身高与体重的线性模型
```{r eda-height-18}
d %>%
group_by(Gender) %>%
group_modify(
~ broom::tidy(lm(Weight ~ 1 + Height, data = .))
)
```
```{r eda-height-19}
d %>%
ggplot(aes(x = Height, y = Weight, group = Gender)) +
geom_point(aes(color = Gender)) +
geom_smooth(method = lm)
```
```{r eda-height-20, echo = F}
# remove the objects
# rm(list=ls())
rm(d, fit, density_colors)
```
```{r eda-height-21, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```