getwd()
## [1] "/Users/idlhy/Library/CloudStorage/OneDrive-개인/R FILE"
setwd("/Users/idlhy/Library/CloudStorage/OneDrive-개인/바탕 화면/Research/Puberty Delinquency(HRB)")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(haven)
library(readxl)
# early
early_int <- read_excel("./4. sas print files/early_int.xlsx") %>%
rename(Grade = n_year_2, early = Intercept,
e_l = Intercept_L, e_u = Intercept_U) %>%
mutate(Grade = Grade - 2003) %>% # convert time
mutate(early = exp(early), # convert into proportion
e_l = exp(e_l),
e_u = exp(e_u)) %>%
select(-Intercept_SE)
# on time
normal_int <- read_excel("./4. sas print files/normal_int.xlsx") %>%
rename(Grade = n_year_2, normal = Intercept,
n_l = Intercept_L, n_u = Intercept_U) %>%
mutate(Grade = Grade - 2003) %>%
mutate(normal = exp(normal),
n_l = exp(n_l),
n_u = exp(n_u)) %>%
select(-Intercept_SE)
# late
late_int <- read_excel("./4. sas print files/late_int.xlsx") %>%
rename(Grade = n_year_2, late = Intercept,
l_l = Intercept_L, l_u = Intercept_U) %>%
mutate(Grade = Grade - 2003) %>%
mutate(late = exp(late),
l_l = exp(l_l),
l_u = exp(l_u)) %>%
select(-Intercept_SE)
intercept <- early_int %>%
left_join(normal_int, by = c("Grade")) %>%
left_join(late_int, by = c("Grade"))
colors <- c("late" = "#0000CC",
"early" = "#CC0000",
"on time" = "#1B9E77")
linetypes <- c("late" = 5,
"early" = 1,
"on time" = 6)
# 1. Intercept
ggplot(intercept) +
aes(x = Grade) +
geom_line(aes(y = early, color = "early", lty = "early"), size = 1) +
geom_line(aes(y = normal, color = "on time", lty = "on time"), size = 1) +
geom_line(aes(y = late, color = "late", lty = "late"), size = 1) +
geom_ribbon(aes(ymin = e_l, ymax = e_u), color = "#CC0000", alpha = 0.05, lty = "dotted") +
geom_ribbon(aes(ymin = n_l, ymax = n_u), color = "#1B9E77", alpha = 0.05, lty = "dotted") +
geom_ribbon(aes(ymin = l_l, ymax = l_u), color = "#0000CC", alpha = 0.05, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors) +
scale_linetype_manual(values = linetypes) +
labs(x = "Grade"
, y = "Prevalence"
, color = "Puberty Group"
, lty = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

## Only E and L
ggplot(intercept) +
aes(x = Grade) +
geom_line(aes(y = early, color = "early", lty = "early"), size = 1) +
geom_line(aes(y = late, color = "late", lty = "late"), size = 1) +
geom_ribbon(aes(ymin = e_l, ymax = e_u), color = "#CC0000", alpha = 0.1, lty = "dotted") +
geom_ribbon(aes(ymin = l_l, ymax = l_u), color = "#0000CC", alpha = 0.1, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
, limits = c("early", "late")) +
labs(x = "Grade"
, y = "Prevalence"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# 2. Peer Substance Abuse
early_cov <- read_excel("./4. sas print files/early_covariates.xlsx") %>%
rename(Grade = n_year_2) %>%
mutate(Grade = Grade - 2003)
normal_cov <- read_excel("./4. sas print files/normal_covariates.xlsx") %>%
rename(Grade = n_year_2) %>%
mutate(Grade = Grade - 2003)
late_cov <- read_excel("./4. sas print files/late_covariates.xlsx") %>%
rename(Grade = n_year_2) %>%
mutate(Grade = Grade - 2003)
ggplot() +
geom_line(aes(x = early_cov$Grade, y = early_cov$n_peer_all_2,
color = "early", lty = "early"), size = 1) +
geom_line(aes(x = normal_cov$Grade, y = normal_cov$n_peer_all_2,
color = "on time", lty = "on time"), size = 1) +
geom_line(aes(x = late_cov$Grade, y = late_cov$n_peer_all_2,
color = "late", lty = "late"), size = 1) +
geom_hline(yintercept = 0, color = "black") +
geom_ribbon(aes(x = early_cov$Grade,
ymin = early_cov$n_peer_all_2_L, ymax = early_cov$n_peer_all_2_U),
color = "#CC0000", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = normal_cov$Grade,
ymin = normal_cov$n_peer_all_2_L, ymax = normal_cov$n_peer_all_2_U),
color = "#1B9E77", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = late_cov$Grade,
ymin = late_cov$n_peer_all_2_L, ymax = late_cov$n_peer_all_2_U),
color = "#0000CC", alpha = 0.04, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
, limits = c("early", "late", "on time")) +
labs(x = "Grade"
, y = "Coefficient"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

## E and L only
ggplot() +
geom_line(aes(x = early_cov$Grade, y = early_cov$n_peer_all_2,
color = "early", lty = "early"), size = 1) +
geom_line(aes(x = late_cov$Grade, y = late_cov$n_peer_all_2,
color = "late", lty = "late"), size = 1) +
geom_hline(yintercept = 0, color = "black") +
geom_ribbon(aes(x = early_cov$Grade,
ymin = early_cov$n_peer_all_2_L, ymax = early_cov$n_peer_all_2_U),
color = "#CC0000", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = late_cov$Grade,
ymin = late_cov$n_peer_all_2_L, ymax = late_cov$n_peer_all_2_U),
color = "#0000CC", alpha = 0.04, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
,limits = c("early", "late")) +
labs(x = "Grade"
, y = "Coefficient"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# 3. Parent Attachment
ggplot() +
geom_line(aes(x = early_cov$Grade, y = early_cov$n_P_attach_2,
color = "early", lty = "early"), size = 1) +
geom_line(aes(x = normal_cov$Grade, y = normal_cov$n_P_attach_2,
color = "on time", lty = "on time"), size = 1) +
geom_line(aes(x = late_cov$Grade, y = late_cov$n_P_attach_2,
color = "late", lty = "late"), size = 1) +
geom_hline(yintercept = 0, color = "black") +
geom_ribbon(aes(x = early_cov$Grade,
ymin = early_cov$n_P_attach_2_L, ymax = early_cov$n_P_attach_2_U),
color = "#CC0000", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = normal_cov$Grade,
ymin = normal_cov$n_P_attach_2_L, ymax = normal_cov$n_P_attach_2_U),
color = "#1B9E77", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = late_cov$Grade,
ymin = late_cov$n_P_attach_2_L, ymax = late_cov$n_P_attach_2_U),
color = "#0000CC", alpha = 0.04, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
, limits = c("early", "late", "on time")) +
labs(x = "Grade"
, y = "Coefficient"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

## E and L only
ggplot() +
geom_line(aes(x = early_cov$Grade, y = early_cov$n_P_attach_2,
color = "early", lty = "early"), size = 1) +
geom_line(aes(x = late_cov$Grade, y = late_cov$n_P_attach_2,
color = "late", lty = "late"), size = 1) +
geom_hline(yintercept = 0, color = "black") +
geom_ribbon(aes(x = early_cov$Grade,
ymin = early_cov$n_P_attach_2_L, ymax = early_cov$n_P_attach_2_U),
color = "#CC0000", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = late_cov$Grade,
ymin = late_cov$n_P_attach_2_L, ymax = late_cov$n_P_attach_2_U),
color = "#0000CC", alpha = 0.04, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
,limits = c("early", "late")) +
labs(x = "Grade"
, y = "Coefficient"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# 4. Coeducation
ggplot() +
geom_line(aes(x = early_cov$Grade, y = early_cov$n_coedu_1,
color = "early", lty = "early"), size = 1) +
geom_line(aes(x = normal_cov$Grade, y = normal_cov$n_coedu_1,
color = "on time", lty = "on time"), size = 1) +
geom_line(aes(x = late_cov$Grade, y = late_cov$n_coedu_1,
color = "late", lty = "late"), size = 1) +
geom_hline(yintercept = 0, color = "black") +
geom_ribbon(aes(x = early_cov$Grade,
ymin = early_cov$n_coedu_1_L, ymax = early_cov$n_coedu_1_U),
color = "#CC0000", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = normal_cov$Grade,
ymin = normal_cov$n_coedu_1_L, ymax = normal_cov$n_coedu_1_U),
color = "#1B9E77", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = late_cov$Grade,
ymin = late_cov$n_coedu_1_L, ymax = late_cov$n_coedu_1_U),
color = "#0000CC", alpha = 0.04, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
, limits = c("early", "late", "on time")) +
labs(x = "Grade"
, y = "Coefficient"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

## E and L only
ggplot() +
geom_line(aes(x = early_cov$Grade, y = early_cov$n_coedu_1,
color = "early", lty = "early"), size = 1) +
geom_line(aes(x = late_cov$Grade, y = late_cov$n_coedu_1,
color = "late", lty = "late"), size = 1) +
geom_hline(yintercept = 0, color = "black") +
geom_ribbon(aes(x = early_cov$Grade,
ymin = early_cov$n_coedu_1_L, ymax = early_cov$n_coedu_1_U),
color = "#CC0000", alpha = 0.04, lty = "dotted") +
geom_ribbon(aes(x = late_cov$Grade,
ymin = late_cov$n_coedu_1_L, ymax = late_cov$n_coedu_1_U),
color = "#0000CC", alpha = 0.04, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors
,limits = c("early", "late")) +
labs(x = "Grade"
, y = "Coefficient"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# 5. Overall Group - Intercept
# Intercept
T_int <- read_excel("./4. sas print files/total_intercept.xlsx") %>%
rename(Grade = n_year_2) %>%
mutate(Grade = Grade - 2003) %>% # convert time
mutate(total = exp(Intercept), # convert into proportion
t_l = exp(Intercept_L),
t_u = exp(Intercept_U)) %>%
select(-Intercept_SE)
ggplot(T_int) +
aes(x = Grade) +
geom_line(aes(y = total), size = 1) +
geom_line(aes(y = 0), size = 1) +
geom_ribbon(aes(ymin = t_l, ymax = t_u), color = "#CC0000", alpha = 0.04, lty = "dotted") +
theme_bw() +
labs(x = "Grade"
, y = "Prevalence"
, title = "Intercept"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# 6. Overall Group - Covariates
T_cov <- read_excel("./4. sas print files/total_covariate.xlsx") %>%
rename(Grade = n_year_2) %>%
mutate(Grade = Grade - 2003)
# Peer Substance Use
ggplot(T_cov) +
aes(x = Grade) +
geom_line(aes(y = n_peer_all_2), size = 1) +
geom_line(aes(y = 0), size = 1, color = "grey50") +
geom_ribbon(aes(ymin = n_peer_all_2_L, ymax = n_peer_all_2_U), color = "#CC0000", alpha = 0.04, lty = "dotted") +
theme_bw() +
labs(x = "Grade"
, y = "Coefficient"
, title = "Peer Substance Abuse"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# Parent attachment
ggplot(T_cov) +
aes(x = Grade) +
geom_line(aes(y = n_P_attach_2), size = 1) +
geom_line(aes(y = 0), size = 1, color = "grey50") +
geom_ribbon(aes(ymin = n_P_attach_2_L, ymax = n_P_attach_2_U), color = "#CC0000", alpha = 0.04, lty = "dotted") +
theme_bw() +
labs(x = "Grade"
, y = "Coefficient"
, title = "Parent Attachment"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
plot.title = element_text(family = "Times New Roman", size = (16)),
legend.title = element_text(family = "Times New Roman", size = (16)),
legend.text = element_text(family = "Times New Roman", size = (16)),
axis.title = element_text(family = "Times New Roman", size = (16)),
axis.text = element_text(family = "Times New Roman", size = (16)))

# Coeducation
ggplot(T_cov) +
aes(x = Grade) +
geom_line(aes(y = n_coedu_1), size = 1) +
geom_line(aes(y = 0), size = 1, color = "grey50") +
geom_ribbon(aes(ymin = n_coedu_1_L, ymax = n_coedu_1_U), color = "#CC0000", alpha = 0.04, lty = "dotted") +
theme_bw() +
labs(x = "Grade"
, y = "Coefficient"
, title = "Coeducation"
, lty = "Puberty Group"
, color = "Puberty Group") +
theme(legend.position = "bottom",
)
