-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_regression.R
143 lines (129 loc) · 3.72 KB
/
04_regression.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
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
## ------------------------------------------------------------------------
##
## Script name: 04_regression.R
## Purpose: Multinomial and ordinary regression
## Author: Yanwen Wang
## Date Created: 2024-11-20
## Email: [email protected]
##
## ------------------------------------------------------------------------
##
## Notes:
##
## ------------------------------------------------------------------------
# 1 Multinomial logistic regression ---------------------------------------
# 1.1 Fit models ----------------------------------------------------------
mods <- list(
mod1 <- multinom(
cluster5 ~ gender + age + I(age^2) + edu + hukou + province,
data = childless_df
),
mod2 <- multinom(
cluster5 ~ gender * age + gender * I(age^2) + edu + hukou + province,
data = childless_df
),
mod3 <- multinom(
cluster5 ~ gender * edu + age + I(age^2) + hukou + province,
data = childless_df
),
mod4 <- multinom(
cluster5 ~ gender * hukou + age + I(age^2) + edu + province,
data = childless_df
)
)
# 1.2 Marginal effects ----------------------------------------------------
# Gender
predictions <- ggemmeans(mod1, terms = c("gender"))
gender_plt <- as.data.frame(predictions) %>%
mutate(
x = case_when(
x == 0 ~ "female",
x == 1 ~ "male"
)
) %>%
ggplot(aes(x = x, y = predicted, group = 1)) +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = .1) +
scale_y_continuous(labels = scales::percent) +
facet_grid(response.level ~ ., scales = "free") +
labs(
title = "",
y = "Probability",
x = ""
) +
theme_bw()
# Age or cohort
predictions <- ggemmeans(mod2, terms = c("age[all]", "gender"))
cohort_plt <- as.data.frame(predictions) %>%
mutate(
Gender = case_when(
group == 0 ~ "female",
group == 1 ~ "male"
)
) %>%
mutate(x = 2018 - x) %>%
ggplot(aes(x = x, y = predicted, group = Gender, color = Gender)) +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = .1) +
scale_x_continuous(breaks = seq(1920, 1980, 5)) +
scale_y_continuous(labels = scales::percent) +
facet_grid(response.level ~ ., scales = "free") +
labs(
title = "",
y = "Probability",
x = ""
) +
theme_bw() +
theme(legend.position = "none")
# Education
predictions <- ggemmeans(mod3, terms = c("edu[all]", "gender"))
edu_plt <- as.data.frame(predictions) %>%
mutate(
Gender = case_when(
group == 0 ~ "female",
group == 1 ~ "male"
),
x = case_when(
x == 0 ~ "low edu",
x == 1 ~ "high edu"
),
x = factor(x, levels = c("low edu", "high edu"))
) %>%
ggplot(aes(x = x, y = predicted, group = Gender, color = Gender)) +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = .1) +
scale_y_continuous(labels = scales::percent) +
facet_grid(response.level ~ ., scales = "free") +
labs(
title = "",
y = "Probability",
x = ""
) +
theme_bw()
# Hukou
predictions <- ggemmeans(mod4, terms = c("hukou[all]", "gender"))
hukou_plt <- as.data.frame(predictions) %>%
mutate(Gender = case_when(
group == 0 ~ "female",
group == 1 ~ "male"
)) %>%
mutate(x = case_when(
x == 0 ~ "rural",
x == 1 ~ "urban"
)) %>%
ggplot(aes(x = x, y = predicted, group = Gender, color = Gender)) +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = .1) +
scale_y_continuous(labels = scales::percent) +
facet_grid(response.level ~ ., scales = "free") +
labs(
title = "",
y = "Probability",
x = ""
) +
theme_bw()
# 2 Ordinary least squares regression -------------------------------------
mod <- lm(
complexity ~ gender + birthy + I(birthy^2) + edu + hukou + province,
data = childless_df
)