-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_odds_ratio.r
135 lines (126 loc) · 3.29 KB
/
04_odds_ratio.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
## ------------------------------------------------------------------------
##
## Script name: 04_odds_ratio.r
## Purpose: Visualize odds ratios
## Author: Yanwen Wang
## Date Created: 2024-12-10
## Email: [email protected]
##
## ------------------------------------------------------------------------
##
## Notes:
##
## ------------------------------------------------------------------------
# 1 Load data -------------------------------------------------------------
odds_ratio_pooled <- read_feather("Outputs/odds_ratio.arrow")
odds_ratio_by_hukou <- read_feather("Outputs_by_hukou/odds_ratio.arrow")
# Recode education and urban status
odds_ratio_pooled <- odds_ratio_pooled %>%
mutate(
Education = factor(edu,
levels = c(
"Primary or less",
"Middle",
"High",
"College or above",
"Aggregated"
)
)
)
odds_ratio_by_hukou <- odds_ratio_by_hukou %>%
mutate(
Education = factor(edu,
levels = c(
"Primary or less",
"Middle",
"High",
"College or above",
"Aggregated"
)
)
) %>%
mutate(urban = ifelse(urban == 1, "Rural", "Urban"))
# 2 Plot trends -----------------------------------------------------------
# Pooled
odds_ratio_pooled_plt <- odds_ratio_pooled %>%
ggplot(aes(
x = cohort, y = estimate,
color = Education, group = Education, linetype = Education
)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2) +
geom_hline(yintercept = 0, linetype = "dashed", color = "grey") +
scale_y_continuous(breaks = seq(-10, 10, by = 1)) +
scale_color_manual(
values = c(
"Primary or less" = "#fd7f6f",
"Middle" = "#7eb0d5",
"High" = "#b2e061",
"College or above" = "#bd7ebe",
"Aggregated" = "grey"
),
name = "Education"
) +
scale_linetype_manual(
values = c(
"Primary or less" = "solid",
"Middle" = "solid",
"High" = "solid",
"College or above" = "solid",
"Aggregated" = "dashed"
),
name = "Education"
) +
labs(
x = "Cohort",
y = "Log-odd ratios"
) +
theme(legend.position = "none") +
facet_grid(~term)
# By hukou
odds_ratio_by_hukou_plt <- odds_ratio_by_hukou %>%
ggplot(aes(
x = cohort, y = estimate,
color = Education, group = Education, linetype = Education
)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2) +
geom_hline(yintercept = 0, linetype = "dashed", color = "grey") +
scale_y_continuous(breaks = seq(-10, 10, by = 1)) +
scale_color_manual(
values = c(
"Primary or less" = "#fd7f6f",
"Middle" = "#7eb0d5",
"High" = "#b2e061",
"College or above" = "#bd7ebe",
"Aggregated" = "grey"
),
name = "Education"
) +
scale_linetype_manual(
values = c(
"Primary or less" = "solid",
"Middle" = "solid",
"High" = "solid",
"College or above" = "solid",
"Aggregated" = "dashed"
),
name = "Education"
) +
labs(
x = "Cohort",
y = "Log-odd ratios"
) +
theme(legend.position = "bottom") +
facet_grid(~term+urban)
odds_ratio_plt <- odds_ratio_pooled_plt / odds_ratio_by_hukou_plt
# Save the plot
ggsave(
"Graphs/odds_ratio.png",
odds_ratio_plt,
width = 8,
height = 12,
dpi = 300
)