-
Notifications
You must be signed in to change notification settings - Fork 218
/
eda_career_decision.Rmd
420 lines (278 loc) · 8.87 KB
/
eda_career_decision.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# 探索性数据分析-大学生职业决策 {#eda-career-decision}
```{r, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.showtext = TRUE
)
```
## 预备知识
```{r eda-career-decision-1, message=FALSE, warning=FALSE}
library(tidyverse)
example <-
tibble::tribble(
~name, ~english, ~chinese, ~math, ~sport, ~psy, ~edu,
"A", 133, 100, 102, 56, 89, 89,
"B", 120, 120, 86, 88, 45, 75,
"C", 98, 109, 114, 87, NA, 84,
"D", 120, 78, 106, 68, 86, 69,
"E", 110, 99, 134, 98, 75, 70,
"F", NA, 132, 130, NA, 68, 88
)
example
```
### 缺失值检查
我们需要判断每一列的缺失值
```{r eda-career-decision-2}
example %>%
summarise(
na_in_english = sum(is.na(english)),
na_in_chinese = sum(is.na(chinese)),
na_in_math = sum(is.na(math)),
na_in_sport = sum(is.na(sport)),
na_in_psy = sum(is.na(math)), # tpyo here
na_in_edu = sum(is.na(edu))
)
```
我们发现,这种写法比较笨,而且容易出错,比如`na_in_psy = sum(is.na(math))` 就写错了。那么有没有`既偷懒又安全`的方法呢?有的。但代价是需要学会`across()`函数,大家可以在Console中输入`?dplyr::across`查看帮助文档,或者看第 \@ref(tidyverse-colwise) 章。
```{r eda-career-decision-3}
example %>%
summarise(
across(everything(), mean)
)
example %>%
summarise(
across(everything(), function(x) sum(is.na(x)) )
)
```
### 数据预处理
- 直接**丢弃**缺失值所在的行
```{r eda-career-decision-4}
example %>% drop_na()
```
- 用**均值**代替缺失值
```{r eda-career-decision-5, eval=FALSE, include=FALSE}
example %>%
mutate(
english_new = if_else(is.na(english), mean(english, na.rm = T), english)
)
```
```{r eda-career-decision-6}
d <- example %>%
mutate(
across(where(is.numeric), ~ if_else(is.na(.), mean(., na.rm = T), .))
)
d
```
- 计算总分/均值
```{r eda-career-decision-7}
d %>%
rowwise() %>%
mutate(
total = sum(c_across(-name))
)
d %>%
rowwise() %>%
mutate(
mean = mean(c_across(-name))
)
```
- **数据标准化**处理
```{r eda-career-decision-8}
standard <- function(x) {
(x - mean(x)) / sd(x)
}
```
```{r eda-career-decision-9}
d %>%
mutate(
across(where(is.numeric), standard)
)
```
## 开始
### 文件管理中需要注意的地方
感谢康钦虹同学提供的数据,但这里有几点需要注意的地方:
| 事项 | 问题 | 解决办法 |
|---------- |--------------------------- |-----------------------------------------------|
| 文件名 | excel的文件名是中文 | 用英文,比如 `data.xlsx` |
| 列名 | 列名中有-号,大小写不统一 | 规范列名,或用`janitor::clean_names()`偷懒 |
| 预处理 | 直接在原始数据中新增 | 不要在原始数据上改动,统计工作可以在R里实现 |
| 文件管理 | 没有层级 | 新建`data`文件夹装数据,与`code.Rmd`并列 |
```{r eda-career-decision-10, message=FALSE, warning=FALSE}
data <- readxl::read_excel("demo_data/career-decision.xlsx", skip = 1) %>%
janitor::clean_names()
#glimpse(data)
```
```{r eda-career-decision-11}
d <- data %>% select(1:61)
#glimpse(d)
```
### 缺失值检查
```{r eda-career-decision-12}
d %>%
summarise(
across(everything(), ~sum(is.na(.)))
)
```
没有缺失值,挺好
### 数据预处理
采用利克特式 5 点计分... (这方面你们懂得比我多)
```{r eda-career-decision-13}
d <- d %>%
rowwise() %>%
mutate(
environment_exploration = sum(c_across(z1:z5)),
self_exploration = sum(c_across(z6:z9)),
objective_system_exploration = sum(c_across(z10:z15)),
info_quantity_exploration = sum(c_across(z16:z18)),
self_evaluation = sum(c_across(j1:j6)),
information_collection = sum(c_across(j7:j15)),
target_select = sum(c_across(j16:j24)),
formulate = sum(c_across(j25:j32)),
problem_solving = sum(c_across(j33:j39)),
career_exploration = sum(c_across(z1:z18)),
career_decision_making = sum(c_across(j1:j39))
) %>%
select(-starts_with("z"), -starts_with("j")) %>%
ungroup() %>%
mutate(pid = 1:n(), .before = sex) %>%
mutate(
across(c(pid, sex, majoy, grade, from), as_factor)
)
#glimpse(d)
```
### 标准化
```{r eda-career-decision-14}
standard <- function(x) {
(x - mean(x)) / sd(x)
}
d <- d %>%
mutate(
across(where(is.numeric), standard)
)
d
```
## 探索
### 想探索的问题
- 不同性别(或者年级,生源地,专业)下,各指标分值的差异性
- 两个变量的相关分析和回归分析
- 更多(欢迎大家提出了喔)
### 男生女生在职业探索上有所不同?
以性别为例。因为性别变量是男女,仅仅2组,所以检查男女**在各自指标上的均值差异**,可以用T检验。
```{r eda-career-decision-15}
d %>%
group_by(sex) %>%
summarise(
across(where(is.numeric), mean)
)
```
你可以给这个图颜色弄得更好看点?
```{r eda-career-decision-16, fig.width=4, fig.height=3.5, fig.align="center"}
library(ggridges)
d %>%
ggplot(aes(x = career_exploration, y = sex, fill = sex)) +
geom_density_ridges()
```
```{r eda-career-decision-17}
t_test_eq <- t.test(career_exploration ~ sex, data = d, var.equal = TRUE) %>%
broom::tidy()
t_test_eq
```
```{r eda-career-decision-18}
t_test_uneq <- t.test(career_exploration ~ sex, data = d, var.equal = FALSE) %>%
broom::tidy()
t_test_uneq
```
当然,也可以用第 \@ref(tidystats-infer) 章介绍的统计推断的方法
```{r eda-career-decision-19}
library(infer)
obs_diff <- d %>%
specify(formula = career_exploration ~ sex) %>%
calculate("diff in means", order = c("1", "2"))
obs_diff
```
```{r eda-career-decision-20}
null_dist <- d %>%
specify(formula = career_exploration ~ sex) %>%
hypothesize(null = "independence") %>%
generate(reps = 5000, type = "permute") %>%
calculate(stat = "diff in means", order = c("1", "2"))
null_dist
```
```{r eda-career-decision-21}
null_dist %>%
visualize() +
shade_p_value(obs_stat = obs_diff, direction = "two_sided")
```
```{r eda-career-decision-22}
null_dist %>%
get_p_value(obs_stat = obs_diff, direction = "two_sided") %>%
#get_p_value(obs_stat = obs_diff, direction = "less") %>%
mutate(p_value_clean = scales::pvalue(p_value))
```
也可以用tidyverse的方法一次性的搞定**所有指标**
```{r eda-career-decision-23}
d %>%
pivot_longer(
cols = -c(pid, sex, majoy, grade, from),
names_to = "index",
values_to = "value"
) %>%
group_by(index) %>%
summarise(
broom::tidy( t.test(value ~ sex, data = cur_data()))
) %>%
select(index, estimate, statistic, p.value) %>%
arrange(p.value)
```
### 来自不同地方的学生在职业探索上有所不同?
以生源地为例。因为生源地有3类,所以可以使用方差分析。
```{r eda-career-decision-24}
aov(career_exploration ~ from, data = d) %>%
TukeyHSD(which = "from") %>%
broom::tidy()
```
```{r eda-career-decision-25, eval=FALSE, include=FALSE}
lm(career_exploration ~ from, data = d) %>%
broom::tidy()
```
```{r eda-career-decision-26, fig.width=4, fig.height=3.5, fig.align="center"}
library(ggridges)
d %>%
ggplot(aes(x = career_exploration, y = from, fill = from)) +
geom_density_ridges()
```
也可以一次性的搞定**所有指标**
```{r eda-career-decision-27}
d %>%
pivot_longer(
cols = -c(pid, sex, majoy, grade, from),
names_to = "index",
values_to = "value"
) %>%
group_by(index) %>%
summarise(
broom::tidy( aov(value ~ from, data = cur_data()))
) %>%
select(index, term, statistic, p.value) %>%
filter(term != "Residuals") %>%
arrange(p.value)
```
### 职业探索和决策之间有关联?
可以用第 \@ref(tidystats-lm) 章线性模型来探索
```{r eda-career-decision-28}
lm(career_decision_making ~ career_exploration, data = d)
```
不要因为我讲课讲的很垃圾,就错过了R的美,瑕不掩瑜啦。要相信自己,你们是川师研究生中最聪明的。
```{r eda-career-decision-29, echo=FALSE, fig.align='center', out.width='90%'}
knitr::include_graphics("images/support.jpg")
```
```{r eda-career-decision-30, echo = F}
# remove the objects
# rm(list=ls())
rm(d, data, example, null_dist, obs_diff, standard, t_test_eq, t_test_uneq)
```
```{r eda-career-decision-31, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```