Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created January 30, 2024 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/5d38887039b51bf62cd4c1c291be9322 to your computer and use it in GitHub Desktop.
Save ryanburge/5d38887039b51bf62cd4c1c291be9322 to your computer and use it in GitHub Desktop.
graph <- cces %>%
filter(year == 2008 | year == 2022) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
group_by(age, year) %>%
mean_ci(single, wt = weight)
graph %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean, color = factor(year), group = factor(year))) +
geom_point(stroke = .5, shape = 21, alpha = .45) +
geom_labelsmooth(aes(label = year), method = "loess", formula = y ~ x, family = "font",
linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .75) +
scale_color_calc() +
theme_rb() +
y_pct() +
labs(x = "Age", y = "", title = "Share Reporting That They Have Never Been Married by Age",
caption = "@ryanburge\nData: Cooperative Election 2008-2022")
save("age_marital_new.png", wd = 8, ht = 6)
graph <- cces %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(year == 2008 | year == 2022) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
group_by(age, year, gender) %>%
mean_ci(single, wt = weight) %>% filter(gender != "NA")
graph %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean, color = gender, group = gender)) +
geom_point(stroke = .5, shape = 21, alpha = .45, show.legend = FALSE) +
geom_smooth(se = FALSE) +
scale_color_calc() +
facet_wrap(~ year) +
theme_rb(legend = TRUE) +
y_pct() +
theme(strip.text = element_text(size = 20))+
labs(x = "Age", y = "", title = "Share Reporting That They Have Never Been Married by Age",
caption = "@ryanburge\nData: Cooperative Election 2008-2022")
save("age_marital_gender.png", wd = 8, ht = 6)
graph <- cces %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conservative")) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(year >= 2020) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
group_by(age, id3, gender) %>%
mean_ci(single, wt = weight) %>% filter(gender != "NA") %>% filter(id3 != "NA")
graph %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean, color = gender, group = gender)) +
geom_point(stroke = .5, shape = 21, alpha = .45, show.legend = FALSE) +
geom_smooth(se = FALSE) +
scale_color_calc() +
facet_wrap(~ id3) +
theme_rb(legend = TRUE) +
y_pct() +
theme(strip.text = element_text(size = 20))+
labs(x = "Age", y = "", title = "Share Reporting That They Have Never Been Married by Age",
caption = "@ryanburge\nData: Cooperative Election 2020-2022")
save("age_marital_gender_id3.png", wd = 8, ht = 6)
graph <- cces %>%
mutate(rel = frcode(religion == 1 | religion == 2 | religion == 3 | religion == 4 ~ "Christian",
religion == 5 | religion == 6 | religion == 7 | religion == 8 ~ "Other Faith",
religion == 9 | religion == 10 | religion == 11 ~ "Nones")) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(year >= 2020) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
group_by(age, rel, gender) %>%
mean_ci(single, wt = weight) %>% filter(gender != "NA") %>% filter(rel != "NA")
graph %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean, color = gender, group = gender)) +
geom_point(stroke = .5, shape = 21, alpha = .45, show.legend = FALSE) +
geom_smooth(se = FALSE) +
scale_color_calc() +
facet_wrap(~ rel) +
theme_rb(legend = TRUE) +
y_pct() +
theme(strip.text = element_text(size = 20))+
labs(x = "Age", y = "", title = "Share Reporting That They Have Never Been Married by Age",
caption = "@ryanburge\nData: Cooperative Election 2020-2022")
save("age_marital_gender_rel.png", wd = 8, ht = 6)
graph <- cces %>%
filter(year >= 2020) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
mutate(single = case_when(havekids == 1 ~ 1,
havekids == 2 ~ 0)) %>%
group_by(age, gender) %>%
mean_ci(single, wt = weight)
graph %>%
filter(age <= 75) %>%
filter(gender != "NA") %>%
ggplot(., aes(x = age, y = mean, color = gender, group = gender)) +
geom_point(stroke = .5, shape = 21, alpha = .45) +
geom_labelsmooth(aes(label = gender), method = "loess", formula = y ~ x, family = "font",
linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .15) +
scale_color_calc() +
theme_rb() +
y_pct() +
labs(x = "Age", y = "", title = "Share Reporting That They Are a Parent/Guardian of a Child Under the Age of 18",
caption = "@ryanburge\nData: Cooperative Election 2020-2022")
save("age_havekids_new.png", wd = 8, ht = 6)
graph <- cces %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode( birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(both = nokids + single) %>%
mutate(both = case_when(both == 2 ~ 1,
TRUE ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
group_by(year, gender, cohorts) %>%
mean_ci(both, wt = weight) %>%
filter(mean > 0) %>%
filter(cohorts != "NA") %>%
filter(gender != "NA")
graph %>%
ggplot(., aes(x = year, y = mean, color = gender, group = gender)) +
geom_point(stroke = .5, shape = 21, alpha = .5) +
geom_smooth(se = FALSE) +
theme_rb(legend = TRUE) +
facet_wrap(~ cohorts) +
scale_color_calc() +
y_pct() +
labs(x = "Year", y = "", title = "Share Who Have Never Been Married and\nAre Not Parents to Young Children", caption = "@ryanburge\nData: Cooperative Election 2008-2022")
save("cohorts_nokid_never_married.png", ht = 8, wd = 6)
regg <- cces %>%
filter(year >= 2020) %>%
filter(age >= 35 & age <= 50) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(both = nokids + single) %>%
mutate(both = case_when(both == 2 ~ 1,
TRUE ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
mutate(white = case_when(race == 1 ~ 1, TRUE ~ 0)) %>%
mutate(none = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1,
TRUE ~ 0)) %>%
mutate(liberal = case_when(ideo5 == 1 | ideo5 == 2 ~ 1,
ideo5 <= 5 ~ 0)) %>%
select(educ, age, gender, both, white, income, none, liberal)
women <- regg %>% filter(gender == "Women")
men <- regg %>% filter(gender == "Men")
ww <- glm(both ~ educ + income + none + liberal + white, family = "binomial", data = women)
mm <- glm(both ~ educ + income + none + liberal + white, family = "binomial", data = men)
coef_names <- c("Age" = "age",
"Male" = "male",
"White" = "white",
"Income" = "income",
"Liberal" = "liberal",
"Attendance" = "att",
"Education" = "educ",
"No Religion" = "none")
library(jtools)
gg1 <- plot_summs(ww, mm, robust = "HC3", scale = TRUE, coefs = coef_names, model.names = c("Women", "Men"))
gg1 +
theme_rb() +
scale_fill_calc() +
scale_color_calc() +
theme(legend.position = c(.75, .75)) +
guides(shape = FALSE) +
guides(color = guide_legend(reverse = TRUE)) +
add_text(x = .25, y = 5.25, word = "More Likely to Be Never Married\nand Never a Parent", sz = 6) +
labs(x = "", y = "", title = "What Factors Predict Someone to be Never Married and Never a Parent?", subtitle = "Sample Restricted to 35-50 Year Olds", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("reg_predict_no_marry_no_kids.png")
regg <- cces %>%
filter(income <= 16) %>%
filter(year >= 2020) %>%
filter(age >= 35 & age <= 50) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(both = nokids + single) %>%
mutate(both = case_when(both == 2 ~ 1,
TRUE ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
mutate(white = case_when(race == 1 ~ 1, TRUE ~ 0)) %>%
mutate(none = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1,
TRUE ~ 0)) %>%
mutate(liberal = case_when(ideo5 == 1 | ideo5 == 2 ~ 1,
ideo5 <= 5 ~ 0)) %>%
mutate(ed2 = frcode(educ == 1 | educ == 2 ~ "HS or Less",
educ == 5 | educ == 6 ~ "College Degree")) %>%
select(ed2, age, gender, both, white, income, none, liberal)
regg <- glm(both ~ ed2*gender*income + white + none + liberal, family = "binomial", data = regg)
library(interactions)
int <- interact_plot(regg, pred= income, modx = gender, mod2 = ed2, int.width = .76, interval = TRUE, modx.labels = c("Men", "Women"), mod2.labels = c("HS or Less", "College Grad"))
int +
theme_rb(legend = TRUE) +
scale_fill_calc() +
scale_color_calc() +
scale_x_continuous(breaks = c(4, 8, 12, 16), labels = c("$30K-$40K", "$70K-$80K", "$150K-$200K", "$500K+")) +
scale_y_continuous(labels = percent) +
theme(strip.text = element_text(size = 20)) +
labs(x = "Household Income", y = "", title = "Predicted Share Who Are Never Married + Not Currently a Parent", subtitle = "Sample Restricted to 35-50 Year Olds", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("interact_income_educ_married_kids.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment