Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 27, 2019 00:25
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/5fb449c4bb91f4e100c6ae9a5a913fc1 to your computer and use it in GitHub Desktop.
Save ryanburge/5fb449c4bb91f4e100c6ae9a5a913fc1 to your computer and use it in GitHub Desktop.
Atheist Therms
## Feeling Therms #####
anes12 <- read_dta("anes_timeseries_2012_Stata12.dta")
anes <- anes %>%
mutate(bagain = car::recode(V161263, "1=1; else=0")) %>%
mutate(prot = car::recode(anes$V161247a, "1=1; else =0")) %>%
mutate(baprot = bagain + prot) %>%
mutate(baprot = car::recode(baprot, "2=1; else =0"))
r_fun <- function(df, var, newvar){
var <- enquo(var)
df <- df %>%
mutate(newvar = car::recode(!! var, "5=1; 4=2; 3=3; 2=4; 1=5; else = NA"))
}
anes %>% r_fun(V162357, "black1")
library(haven)
anes12 <- read_dta("anes_timeseries_2012_Stata12.dta")
anes12 <- anes12 %>%
mutate(xfund = car::recode(ftgr_xfund, "-9:-1 = NA")) %>%
mutate(cath = car::recode(ftgr_catholics, "-9:-1 = NA")) %>%
mutate(fem = car::recode(ftgr_feminists, "-9:-1 = NA")) %>%
mutate(fedgov = car::recode(ftgr_fedgov, "-9:-1 = NA")) %>%
mutate(liberals = car::recode(ftgr_liberals, "-9:-1 = NA")) %>%
mutate(middle = car::recode(ftgr_middle, "-9:-1 = NA")) %>%
mutate(unions = car::recode(ftgr_unions, "-9:-1 = NA")) %>%
mutate(poor = car::recode(ftgr_poor, "-9:-1 = NA")) %>%
mutate(military = car::recode(ftgr_military, "-9:-1 = NA")) %>%
mutate(bigbus = car::recode(ftgr_bigbus, "-9:-1 = NA")) %>%
mutate(welfare = car::recode(ftgr_welfare, "-9:-1 = NA")) %>%
mutate(cons = car::recode(ftgr_cons, "-9:-1 = NA")) %>%
mutate(working = car::recode(ftgr_working, "-9:-1 = NA")) %>%
mutate(scotus = car::recode(ftgr_ussc, "-9:-1 = NA")) %>%
mutate(gay = car::recode(ftgr_gay, "-9:-1 = NA")) %>%
mutate(congress = car::recode(ftgr_congress, "-9:-1 = NA")) %>%
mutate(rich = car::recode(ftgr_rich, "-9:-1 = NA")) %>%
mutate(muslims = car::recode(ftgr_muslims, "-9:-1 = NA")) %>%
mutate(xian = car::recode(ftgr_xian, "-9:-1 = NA")) %>%
mutate(atheists = car::recode(ftgr_atheists, "-9:-1 = NA")) %>%
mutate(mormons = car::recode(ftgr_mormons, "-9:-1 = NA")) %>%
mutate(tea = car::recode(ftgr_tea, "-9:-1 = NA"))
mean_fun <- function(df, var, var1){
var <- enquo(var)
df %>%
mean_ci(!! var) %>%
mutate(group = var1)
}
ttt1 <- anes12 %>% mean_fun(xfund, "Christian Fundamentalists")
ttt2 <-anes12 %>% mean_fun(cath, "Catholics")
ttt3 <-anes12 %>% mean_fun(fem, "Feminists")
ttt4 <-anes12 %>% mean_fun(fedgov, "Federal Government")
ttt5 <-anes12 %>% mean_fun(liberals, "Liberals")
ttt6 <-anes12 %>% mean_fun(middle, "Middle Class")
ttt7 <-anes12 %>% mean_fun(unions, "Unions")
ttt8 <-anes12 %>% mean_fun(poor, "Poor People")
ttt9 <-anes12 %>% mean_fun(military , "The Military")
ttt10 <-anes12 %>% mean_fun(bigbus, "Big Business")
ttt11 <-anes12 %>% mean_fun(welfare, "People on Welfare")
ttt12 <-anes12 %>% mean_fun(cons, "Conservatives")
ttt13 <-anes12 %>% mean_fun(working, "Working Class People")
ttt14 <-anes12 %>% mean_fun(scotus, "Supreme Court")
ttt15 <-anes12 %>% mean_fun(gay, "Gay Men and Lesbians")
ttt16 <-anes12 %>% mean_fun(congress, "Congress")
ttt17 <-anes12 %>% mean_fun(rich, "Rich People")
ttt18 <-anes12 %>% mean_fun(muslims, "Muslims")
ttt19 <-anes12 %>% mean_fun(xian, "Christians")
ttt20 <-anes12 %>% mean_fun(atheists, "Atheists")
ttt21 <-anes12 %>% mean_fun(mormons, "Mormons")
ttt22 <-anes12 %>% mean_fun(tea, "Tea Party")
graph <- bind_df("ttt") %>%
mutate(mean2= round(mean, 1)) %>%
mutate(g2 = paste(group, mean2, sep = " "))
graph$color <- c("Rep", "Rep", "Dem", "Rep", "Dem", "Neutral", "Dem", "Neutral", "Rep", "Dem", "Rep", "Dem", "Dem", "Rep", "Rep", "Dem", "Neutral", "Dem", "Dem", "Dem", "Dem", "Rep")
graph %>%
ggplot(., aes(x= mean, y =1)) +
geom_hline(yintercept = 1) +
theme_gg("Abel") +
geom_point() +
coord_flip() +
geom_hline(yintercept = 1, linetype = "dashed") +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
theme(legend.position = "none") +
scale_color_manual(values = c("dodgerblue3", "darkorchid", "firebrick3")) +
geom_text_repel(aes(mean,1,label=g2, color = color), size = 4, family = "font", nudge_x = .05, direction = "both", show.legend = FALSE, force = 3) +
labs(x = "Mean Thermometer Score", title = "Thermometer Score of Various Groups", caption = "@ryanburge\nData: ANES 2012") +
ggsave("E://feeling_therm_atheist.png", type = "cairo-png", height = 10)
mean_fun <- function(df, var, var1){
var <- enquo(var)
df %>%
mutate(pid3 = as.numeric(pid_x)) %>%
mutate(pid3 = car::recode(pid3, "1:3= 'Democrats'; 4 = 'Independents'; 5:7 = 'Republican'; else = NA")) %>%
group_by(pid3) %>%
mean_ci(!! var) %>%
mutate(group = var1)
}
ttt1 <- anes12 %>% mean_fun(xfund, "Christian Fundamentalists") %>% mutate(color = "Rep")
ttt2 <-anes12 %>% mean_fun(cath, "Catholics")%>% mutate(color = "Dem")
ttt3 <-anes12 %>% mean_fun(fem, "Feminists") %>% mutate(color = "Dem")
ttt4 <-anes12 %>% mean_fun(fedgov, "Federal Government") %>% mutate(color = "Neutral")
ttt5 <-anes12 %>% mean_fun(liberals, "Liberals")%>% mutate(color = "Dem")
ttt6 <-anes12 %>% mean_fun(middle, "Middle Class")%>% mutate(color = "Dem")
ttt7 <-anes12 %>% mean_fun(unions, "Unions")%>% mutate(color = "Dem")
ttt8 <-anes12 %>% mean_fun(poor, "Poor People")%>% mutate(color = "Dem")
ttt9 <-anes12 %>% mean_fun(military , "The Military") %>% mutate(color = "Rep")
ttt10 <-anes12 %>% mean_fun(bigbus, "Big Business") %>% mutate(color = "Rep")
ttt11 <-anes12 %>% mean_fun(welfare, "People on Welfare")%>% mutate(color = "Dem")
ttt12 <-anes12 %>% mean_fun(cons, "Conservatives") %>% mutate(color = "Rep")
ttt13 <-anes12 %>% mean_fun(working, "Working Class People")%>% mutate(color = "Dem")
ttt14 <-anes12 %>% mean_fun(scotus, "Supreme Court")%>% mutate(color = "Neutral")
ttt15 <-anes12 %>% mean_fun(gay, "Gay Men and Lesbians")%>% mutate(color = "Dem")
ttt16 <-anes12 %>% mean_fun(congress, "Congress")%>% mutate(color = "Neutral")
ttt17 <-anes12 %>% mean_fun(rich, "Rich People") %>% mutate(color = "Rep")
ttt18 <-anes12 %>% mean_fun(muslims, "Muslims")%>% mutate(color = "Dem")
ttt19 <-anes12 %>% mean_fun(xian, "Christians") %>% mutate(color = "Rep")
ttt20 <-anes12 %>% mean_fun(atheists, "Atheists")%>% mutate(color = "Dem")
ttt21 <-anes12 %>% mean_fun(mormons, "Mormons") %>% mutate(color = "Rep")
ttt22 <-anes12 %>% mean_fun(tea, "Tea Party")%>% mutate(color = "Rep")
therms <- bind_df("ttt") %>%
filter(pid3 != "NA") %>%
mutate(mean2= round(mean, 1)) %>%
mutate(g2 = paste(group, mean2, sep = " "))
therms %>%
ggplot(., aes(x= mean, y =1)) +
geom_hline(yintercept = 1) +
theme_gg("Abel") +
geom_point() +
coord_flip() +
facet_grid(~ pid3) +
geom_hline(yintercept = 1, linetype = "dashed") +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
theme(legend.position = "none") +
scale_color_manual(values = c("dodgerblue3", "darkorchid", "firebrick3")) +
# theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.minor.x = element_blank()) +
geom_text_repel(aes(mean,1,label=g2, color = color), size = 4, family = "font", nudge_x = .05, direction = "both", show.legend = FALSE, force = 3) +
labs(x = "Mean Thermometer Score", title = "Thermometer Score of Various Groups by Party ID", caption = "@ryanburge\nData: ANES 2012") +
ggsave("E://feeling_therm_atheist_pid3.png", type = "cairo-png", height = 10, width = 9)
## PID3 and Reltrad Therm Scores ####
anes12 <- anes12 %>%
mutate(male = car::recode(iwrobspre_gender, "1=1; else =0")) %>%
mutate(race = case_when(iwrdesc_pre_race_white ==1 ~ 1,
iwrdesc_pre_race_black ==1 ~ 2,
iwrdesc_pre_hisp ==1 ~ 3)) %>%
mutate(white = car::recode(race, "1=1; else =0")) %>%
mutate(educ = car::recode(profile_educdegree, "1:8=1; 9=2; 10=3; 11=4; 12=5; 13:14=6")) %>%
mutate(att = car::recode(relig_churchoft, "5=1; 4=2; 3=3; 2=4; 5=1; else=NA")) %>%
mutate(bible = car::recode(relig_wordgod, "1=1; 2=2; 3=3; else=NA")) %>%
mutate(literal = car::recode(bible, "1=1; else=0")) %>%
mutate(mainline = car::recode(relig_7cat_x, "1=1; else=0")) %>%
mutate(evangelical = car::recode(relig_7cat_x, "2=1; else =0")) %>%
mutate(bprot = car::recode(relig_7cat_x, "3=1; else =0")) %>%
mutate(catholic = car::recode(relig_7cat_x, "4=1; else =0")) %>%
mutate(christian = car::recode(relig_7cat_x, "5=1; else =0")) %>%
mutate(jewish = car::recode(relig_7cat_x, "6=1; else =0")) %>%
mutate(other = car::recode(relig_7cat_x, "7=1; else =0")) %>%
mutate(none = car::recode(relig_7cat_x, "8=1; else =0")) %>%
mutate(age = dem_age_r_x) %>%
mutate(pid3 = as.numeric(pid_x)) %>%
mutate(pid3 = car::recode(pid3, "1:3= 'Democrat'; 4 = 'Independents'; 5:7 = 'Republican'; else = NA"))
anes12 <- anes12 %>%
mutate(xfund = car::recode(ftgr_xfund, "-9:-1 = NA")) %>%
mutate(cath = car::recode(ftgr_catholics, "-9:-1 = NA")) %>%
mutate(fem = car::recode(ftgr_feminists, "-9:-1 = NA")) %>%
mutate(fedgov = car::recode(ftgr_fedgov, "-9:-1 = NA")) %>%
mutate(liberals = car::recode(ftgr_liberals, "-9:-1 = NA")) %>%
mutate(middle = car::recode(ftgr_middle, "-9:-1 = NA")) %>%
mutate(unions = car::recode(ftgr_unions, "-9:-1 = NA")) %>%
mutate(poor = car::recode(ftgr_poor, "-9:-1 = NA")) %>%
mutate(military = car::recode(ftgr_military, "-9:-1 = NA")) %>%
mutate(bigbus = car::recode(ftgr_bigbus, "-9:-1 = NA")) %>%
mutate(welfare = car::recode(ftgr_welfare, "-9:-1 = NA")) %>%
mutate(cons = car::recode(ftgr_cons, "-9:-1 = NA")) %>%
mutate(working = car::recode(ftgr_working, "-9:-1 = NA")) %>%
mutate(scotus = car::recode(ftgr_ussc, "-9:-1 = NA")) %>%
mutate(gay = car::recode(ftgr_gay, "-9:-1 = NA")) %>%
mutate(congress = car::recode(ftgr_congress, "-9:-1 = NA")) %>%
mutate(rich = car::recode(ftgr_rich, "-9:-1 = NA")) %>%
mutate(muslims = car::recode(ftgr_muslims, "-9:-1 = NA")) %>%
mutate(xian = car::recode(ftgr_xian, "-9:-1 = NA")) %>%
mutate(atheists = car::recode(ftgr_atheists, "-9:-1 = NA")) %>%
mutate(mormons = car::recode(ftgr_mormons, "-9:-1 = NA")) %>%
mutate(tea = car::recode(ftgr_tea, "-9:-1 = NA"))
ggg1 <- anes12 %>%
filter(pid3 != "NA") %>%
filter(evangelical == 1) %>%
group_by(pid3) %>%
mean_ci(atheists) %>%
mutate(group = "Evangelicals")
ggg2 <- anes12 %>%
filter(pid3 != "NA") %>%
filter(mainline == 1) %>%
group_by(pid3) %>%
mean_ci(atheists) %>%
mutate(group = "Mainline")
ggg3 <- anes12 %>%
filter(pid3 != "NA") %>%
filter(catholic == 1) %>%
group_by(pid3) %>%
mean_ci(atheists) %>%
mutate(group = "Catholic")
ggg4 <- anes12 %>%
filter(pid3 != "NA") %>%
filter(none == 1) %>%
group_by(pid3) %>%
mean_ci(atheists) %>%
mutate(group = "Nones")
ggg5 <- anes12 %>%
filter(pid3 != "NA") %>%
filter(christian == 1) %>%
group_by(pid3) %>%
mean_ci(atheists) %>%
mutate(group = "Christians")
graph <- bind_df("ggg")
graph %>%
filter(pid3 != "Independents") %>%
ggplot(., aes(y=mean, x= fct_reorder(group, mean), color = pid3)) +
geom_point(position=position_dodge(width=0.5), size =4) +
geom_errorbar(aes(ymin = lower, ymax=upper), position=position_dodge(0.5), size = 1, width =.05) +
scale_color_manual(values = c("dodgerblue3", "firebrick3")) +
labs(x = "Religious Group", y = "Mean Thermometer Score for Atheists", title = "Average Score for Atheists by Religious Group and Party ID", caption = "@ryanburge\nData: ANES 2012") +
theme_gg("Abel") +
theme(legend.position = c(.25, .75)) +
ggsave("E://therm_atheists_pid3_relig.png", type = "cairo-png")
## Interaction ####
anes12 <- anes12 %>%
mutate(pid2 = as.numeric(pid_x)) %>%
mutate(pid2 = car::recode(pid2, "1:3= 'Democrat'; 5:7 = 'Republican'; else = NA"))
reg1 <- lm(atheists ~ age*pid2 + male + educ + white + att, data = anes12)
gg2 <- interact_plot(reg1, pred = age, modx = pid2, int.width = .76, interval = TRUE)
gg2 +
theme_gg("Abel") +
labs(x = "Age of Respondent", y = "Mean Thermometer Score for Atheists", title = "The Interaction of Age and Party ID on Feelings about Atheists", caption = "@ryanburge\nData: ANES 2012") +
scale_color_manual(values = c("dodgerblue3", "firebrick3")) +
scale_fill_manual(values = c("dodgerblue3", "firebrick3")) +
theme(legend.position = c(.25, .25)) +
theme(plot.title = element_text(size = 16)) +
ggsave("E://interact_ath_therm.png", type = "cairo-png")
dem <- anes12 %>%
filter(pid_x == 1 | pid_x ==2 | pid_x == 3) %>%
mutate(age = age/90) %>%
mutate(educ = educ/6) %>%
mutate(att = att/4)
rep <- anes12 %>%
filter(pid_x == 5 | pid_x ==6 | pid_x == 7) %>%
mutate(age = age/90) %>%
mutate(educ = educ/6) %>%
mutate(att = att/4)
ind <- anes12 %>%
filter(pid_x == 4) %>%
mutate(age = age/90) %>%
mutate(educ = educ/6) %>%
mutate(att = att/4)
reg1 <- lm(atheists ~ age + white + educ + att + literal + male, data = dem)
reg2 <- lm(atheists ~ age + white + educ + att + literal + male, data = rep)
reg3 <- lm(atheists ~ age + white + educ + att + literal + male, data = ind)
coef_names <- c("Age" = "age", "White" = "white", "Education" = "educ", "Male" = "male", "Literalism" = "literal", "Church Attendance" = "att", "Constant" = "(Intercept)")
coef_names <- coef_names[1:6]
plot <- plot_summs(reg1, reg2, reg3, coefs = coef_names, point.shape = FALSE, model.names = c("Democrat", "Republican", "Independent"), color.class = "Qual2", rescale.distributions = TRUE)
plot +
labs(x ="<-- Lower Thermometer Scores : Higher Thermometer Scores -->", y = "", subtitle = "", title = "Predicting Thermometer Scores for Atheists", caption = "@ryanburge\nData: ANES 2012") +
theme_gg("Abel") +
theme(legend.position = c(.75,.25)) +
ggsave("E://therm_plot_summs.png", width = 10, height = 6, type = "cairo-png")
stargazer(reg1, reg2, reg3, type = "text", title = "Predicting Atheist Thermometer Scores", column.labels = c("Democrat", "Republican", "Independent"), dep.var.caption = "Atheist Thermometer", dep.var.labels = "",
covariate.labels = c("Age", "White", "Education", "Church Attendance", "Literalism", "Male"),
star.cutoffs = c(0.05), out = "E://ath_therm.htm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment