|
|
|
|
|
|
|
|
rm(list = ls(all = TRUE)) |
|
|
|
|
|
|
|
|
setwd("~/Downloads/hbg_replication") |
|
|
|
|
|
|
|
|
set.seed(123) |
|
|
|
|
|
|
|
|
n_iter <- 10000 |
|
|
|
|
|
|
|
|
library(sandwich) |
|
|
library(car) |
|
|
|
|
|
|
|
|
source("scripts/helper_functions.R") |
|
|
|
|
|
|
|
|
|
|
|
tpnw <- read.csv("data/tpnw_data.csv", row.names = 1, |
|
|
stringsAsFactors = FALSE) |
|
|
|
|
|
|
|
|
aware <- read.csv("data/tpnw_aware.csv", row.names = 1, |
|
|
stringsAsFactors = FALSE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
join_tpnw <- "join_tpnw" |
|
|
|
|
|
|
|
|
tpnw_atts <- names(tpnw)[startsWith(names(tpnw), "tpnw_atts")] |
|
|
|
|
|
|
|
|
all_outs <- c(join_tpnw, tpnw_atts) |
|
|
|
|
|
|
|
|
|
|
|
treats <- c("group_cue", "security_cue", "norms_cue", "institutions_cue") |
|
|
|
|
|
|
|
|
demos <- c("age", "female", "midwest", "west", "south", "income", "educ") |
|
|
|
|
|
|
|
|
pol_demos <- c("ideo", "pid3") |
|
|
|
|
|
|
|
|
|
|
|
covars <- list(NULL, c(demos, pol_demos)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bal_covars <- c("age", "female", "northeast", "midwest", "west", |
|
|
"south", "income", "educ", "ideo", "pid3") |
|
|
|
|
|
|
|
|
bal_mat <- lapply(0:4, function (i) { |
|
|
|
|
|
apply(tpnw[bal_covars][tpnw$treatment == i,], 2, function (x) { |
|
|
|
|
|
|
|
|
mean_x <- mean(x) |
|
|
|
|
|
|
|
|
sd_x <- sd(replicate(10000, { |
|
|
samp <- x[sample(length(x), replace = TRUE)] |
|
|
return(mean(samp)) |
|
|
})) |
|
|
|
|
|
|
|
|
return(list(mean = mean_x, sd = sd_x)) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
bal_mat <- lapply(bal_mat, function (treat) { |
|
|
do.call("rbind", unlist(treat, recursive = FALSE)) |
|
|
}) |
|
|
|
|
|
|
|
|
bal_mat <- do.call("cbind", bal_mat) |
|
|
|
|
|
|
|
|
|
|
|
bal_tab <- apply(bal_mat, 2, function (x) format(round(x, 3), digits = 3)) |
|
|
|
|
|
|
|
|
mean_rows <- endsWith(rownames(bal_tab), ".mean") |
|
|
|
|
|
|
|
|
se_rows <- endsWith(rownames(bal_tab), ".sd") |
|
|
|
|
|
|
|
|
bal_tab[se_rows,] <- apply(bal_tab[se_rows,], 2, function (x) { |
|
|
paste0("(", x, ")") |
|
|
}) |
|
|
|
|
|
|
|
|
rownames(bal_tab)[se_rows] <- "" |
|
|
|
|
|
|
|
|
rownames(bal_tab)[mean_rows] <- gsub(".mean", "", rownames(bal_tab)[mean_rows]) |
|
|
|
|
|
|
|
|
bal_tab <- paste(paste(paste( |
|
|
capwords(rownames(bal_tab)), apply(bal_tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), |
|
|
sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
bal_tab <- gsub("\\( ", "\\(", bal_tab) |
|
|
|
|
|
|
|
|
sink("output/balance_tab.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{Covariate Balance Across Treatment Arms}\n", |
|
|
"\\centering\\small\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{2.75cm}*{5}", |
|
|
"{S[table-number-alignment = center, table-column-width = 1.75cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{5}{c}{Arm}\\\\\\cmidrule{2-6}\n", |
|
|
"& {Control} & {Group} & {Security} & {Norms} & {Institutions} \\\\\\midrule\n", |
|
|
bal_tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
|
|
|
main_results <- lapply(covars, function (covar) { |
|
|
|
|
|
|
|
|
form <- as.formula(paste(join_tpnw, paste(c(treats, covar), |
|
|
collapse = " + "), sep = " ~ ")) |
|
|
|
|
|
|
|
|
fit <- lm(form, data = tpnw) |
|
|
|
|
|
|
|
|
ses <- sqrt(diag(vcovHC(fit, type = "HC2"))) |
|
|
|
|
|
|
|
|
reg_out <- cbind(fit$coef[2:5], ses[2:5]) |
|
|
|
|
|
|
|
|
colnames(reg_out) <- c("coef", "se") |
|
|
rownames(reg_out) <- treats |
|
|
|
|
|
|
|
|
return(as.data.frame(reg_out)) |
|
|
}) |
|
|
|
|
|
|
|
|
names(main_results) <- c("model_1", "model_2") |
|
|
|
|
|
|
|
|
|
|
|
bf_ps <- lapply(main_results, function (x) { |
|
|
round(p.adjust(pnorm(x[, 1] / x[, 2], lower.tail = TRUE), |
|
|
method = "holm"), 3) |
|
|
}) |
|
|
|
|
|
|
|
|
fdr_ps <- lapply(main_results, function (x) { |
|
|
round(p.adjust(pnorm(x[, 1] / x[, 2], lower.tail = TRUE), |
|
|
method = "fdr"), 3) |
|
|
}) |
|
|
|
|
|
|
|
|
main_model <- lm(join_tpnw ~ group_cue + security_cue + norms_cue + |
|
|
institutions_cue + age + female + midwest + |
|
|
west + south + income + educ + ideo + pid3, tpnw) |
|
|
main_vcov <- vcovHC(main_model, "HC2") |
|
|
|
|
|
|
|
|
|
|
|
diff_sig <- function (eff_1, eff_2) { |
|
|
diff <- main_model$coef[eff_1] - main_model$coef[eff_2] |
|
|
se <- sqrt(main_vcov[eff_1, eff_1] + main_vcov[eff_2, eff_2] - |
|
|
2 * main_vcov[eff_1, eff_2]) |
|
|
p <- 2 * (1 - pnorm(abs(diff) / se)) |
|
|
return (p) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
inst_sec_diff_p <- diff_sig("institutions_cue", "security_cue") |
|
|
|
|
|
|
|
|
|
|
|
inst_grp_diff_p <- diff_sig("institutions_cue", "group_cue") |
|
|
|
|
|
|
|
|
|
|
|
sec_grp_diff_p <- diff_sig("security_cue", "group_cue") |
|
|
|
|
|
|
|
|
|
|
|
sec_norms_diff_p <- diff_sig("security_cue", "norms_cue") |
|
|
|
|
|
|
|
|
|
|
|
inst_norms_diff_p <- diff_sig("institutions_cue", "norms_cue") |
|
|
|
|
|
|
|
|
|
|
|
grp_norms_diff_p <- diff_sig("group_cue", "norms_cue") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
aware_table <- table(aware$awareness, useNA = "ifany") |
|
|
names(aware_table) <- c("Yes, support", "Yes, oppose", |
|
|
"No, support", "No, oppose", "Skipped") |
|
|
|
|
|
|
|
|
aware_results <- lapply(1:4, function (resp) { |
|
|
|
|
|
wt_mean <- with(aware, weighted.mean(awareness == resp, |
|
|
w = weight, na.rm = TRUE)) |
|
|
|
|
|
|
|
|
rw_mean <- with(aware, mean(awareness == resp, na.rm = TRUE)) |
|
|
|
|
|
|
|
|
means <- c(wt_mean, rw_mean) |
|
|
names(means) <- c("weighted_mean", "raw_mean") |
|
|
|
|
|
|
|
|
ses <- replicate(10000, { |
|
|
samp <- aware[sample(nrow(aware), |
|
|
replace = TRUE),] |
|
|
wt_mean <- with(samp, weighted.mean(awareness == resp, |
|
|
w = weight, na.rm = TRUE)) |
|
|
rw_mean <- with(samp, mean(awareness == resp, |
|
|
na.rm = TRUE)) |
|
|
return(c(wt_mean, rw_mean)) |
|
|
}) |
|
|
ses <- apply(ses, 1, sd) |
|
|
names(ses) <- c("weighted_mean", "raw_mean") |
|
|
|
|
|
|
|
|
outs <- rbind(means, ses) |
|
|
rownames(outs) <- paste(names(aware_table)[resp], |
|
|
c("mean", "se"), sep = "_") |
|
|
return(outs) |
|
|
}) |
|
|
|
|
|
|
|
|
names(aware_results) <- c("Yes, support", "Yes, oppose", |
|
|
"No, support", "No, oppose") |
|
|
|
|
|
|
|
|
|
|
|
demo_tab_vars <- c("age", "female", "northeast", "midwest", "west", "south") |
|
|
|
|
|
|
|
|
tpnw_means <- apply(tpnw[demo_tab_vars], 2, mean, na.rm = TRUE) |
|
|
|
|
|
|
|
|
aware_means <- apply(aware[demo_tab_vars], 2, function (x) { |
|
|
weighted.mean(x, na.rm = TRUE, w = aware$weight) |
|
|
}) |
|
|
|
|
|
|
|
|
demo_ses <- replicate(10000, { |
|
|
|
|
|
samp_tpnw <- tpnw[sample(nrow(tpnw), replace = TRUE), demo_tab_vars] |
|
|
|
|
|
|
|
|
samp_aware <- aware[sample(nrow(aware), replace = TRUE), |
|
|
c(demo_tab_vars, "weight")] |
|
|
|
|
|
|
|
|
tpnw_means <- apply(samp_tpnw[demo_tab_vars], 2, mean, na.rm = TRUE) |
|
|
|
|
|
|
|
|
aware_means <- apply(samp_aware[demo_tab_vars], 2, function (x) { |
|
|
weighted.mean(x, na.rm = TRUE, w = samp_aware$weight) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return(list(tpnw = tpnw_means, aware = aware_means)) |
|
|
}, simplify = FALSE) |
|
|
|
|
|
|
|
|
demo_ses <- lapply(c("tpnw", "aware"), function (dataset) { |
|
|
|
|
|
sep_res <- lapply(demo_ses, function (iteration) { |
|
|
return(iteration[[dataset]]) |
|
|
}) |
|
|
|
|
|
|
|
|
sep_res <- do.call("rbind", sep_res) |
|
|
|
|
|
|
|
|
sep_ses <- apply(sep_res, 2, sd) |
|
|
|
|
|
|
|
|
return(sep_ses) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
att_results <- lapply(0:4, function (i) { |
|
|
|
|
|
atts_mean <- apply(tpnw[tpnw$treatment == i, tpnw_atts], 2, function (x) { |
|
|
mean(x, na.rm = TRUE) |
|
|
}) |
|
|
|
|
|
|
|
|
bl_atts_boot <- replicate(10000, { |
|
|
dat <- tpnw[tpnw$treatment == i, tpnw_atts] |
|
|
samp <- dat[sample(nrow(dat), replace = TRUE),] |
|
|
apply(samp, 2, function (x) mean(x, na.rm = TRUE)) |
|
|
}) |
|
|
bl_atts_ses <- apply(bl_atts_boot, 1, sd) |
|
|
|
|
|
|
|
|
return(cbind(atts_mean, bl_atts_ses)) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
att_effs <- lapply(covars, function (covar) { |
|
|
|
|
|
model_res <- lapply(tpnw_atts, function (out) { |
|
|
|
|
|
form <- as.formula(paste(out, |
|
|
paste(c(treats, covar), |
|
|
collapse = " + "), |
|
|
sep = " ~ ")) |
|
|
|
|
|
|
|
|
fit <- lm(form, data = tpnw) |
|
|
|
|
|
|
|
|
ses <- sqrt(diag(vcovHC(fit, type = "HC2"))) |
|
|
|
|
|
|
|
|
reg_out <- cbind(fit$coef[2:5], ses[2:5]) |
|
|
|
|
|
|
|
|
colnames(reg_out) <- c("coef", "se") |
|
|
rownames(reg_out) <- treats |
|
|
|
|
|
|
|
|
return(as.data.frame(reg_out)) |
|
|
}) |
|
|
|
|
|
|
|
|
names(model_res) <- tpnw_atts |
|
|
return(model_res) |
|
|
}) |
|
|
|
|
|
|
|
|
names(att_effs) <- c("model_1", "model_2") |
|
|
|
|
|
|
|
|
|
|
|
pid_results <- lapply(0:4, function (treat) { |
|
|
|
|
|
out <- lapply(-1:1, function (i) { |
|
|
|
|
|
pid_mean <- with(tpnw, |
|
|
mean(join_tpnw[pid3 == i & |
|
|
treatment == treat], |
|
|
na.rm = TRUE)) |
|
|
|
|
|
|
|
|
|
|
|
pid_boot <- replicate(10000, { |
|
|
dat <- tpnw$join_tpnw[tpnw$pid3 == i & |
|
|
tpnw$treatment == treat] |
|
|
samp <- dat[sample(length(dat), |
|
|
replace = TRUE)] |
|
|
mean(samp, na.rm = TRUE) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return(c(mean = pid_mean, se = sd(pid_boot))) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
names(out) <- c("dem", "ind", "rep") |
|
|
return(as.data.frame(out)) |
|
|
}) |
|
|
|
|
|
|
|
|
names(pid_results) <- c("Control", paste(c("Group", "Security", "Norms", |
|
|
"Institutions"), "Cue")) |
|
|
|
|
|
|
|
|
|
|
|
pid_diff_ses <- replicate(10000, { |
|
|
|
|
|
samp <- tpnw[sample(nrow(tpnw), replace = TRUE),] |
|
|
|
|
|
|
|
|
|
|
|
dem_ind_diff <- with(samp[samp$treatment == 0,], |
|
|
mean(join_tpnw[pid3 == -1], |
|
|
na.rm = TRUE) - |
|
|
mean(join_tpnw[pid3 == 0], |
|
|
na.rm = TRUE)) |
|
|
|
|
|
|
|
|
dem_rep_diff <- with(samp[samp$treatment == 0,], |
|
|
mean(join_tpnw[pid3 == -1], |
|
|
na.rm = TRUE) - |
|
|
mean(join_tpnw[pid3 == 1], |
|
|
na.rm = TRUE)) |
|
|
|
|
|
|
|
|
ind_rep_diff <- with(samp[samp$treatment == 0,], |
|
|
mean(join_tpnw[pid3 == 1], |
|
|
na.rm = TRUE) - |
|
|
mean(join_tpnw[pid3 == 0], |
|
|
na.rm = TRUE)) |
|
|
|
|
|
|
|
|
out <- c(dem_ind_diff, dem_rep_diff, ind_rep_diff) |
|
|
names(out) <- c("dem_ind", "dem_rep", "ind_rep") |
|
|
return(out) |
|
|
}) |
|
|
|
|
|
|
|
|
pid_diff_ses <- apply(pid_diff_ses, 1, sd) |
|
|
|
|
|
|
|
|
dem_ind_p <- 2 * (1 - pnorm(abs(pid_results$Control["mean", "dem"] - |
|
|
pid_results$Control["mean", "ind"]) / pid_diff_ses["dem_ind"])) |
|
|
dem_rep_p <- 2 * (1 - pnorm(abs(pid_results$Control["mean", "dem"] - |
|
|
pid_results$Control["mean", "rep"]) / pid_diff_ses["dem_rep"])) |
|
|
ind_rep_p <- 2 * (1 - pnorm(abs(pid_results$Control["mean", "ind"] - |
|
|
pid_results$Control["mean", "rep"]) / pid_diff_ses["ind_rep"])) |
|
|
|
|
|
|
|
|
tpnw$ideo <- recode(tpnw$ideo, "c(-2, -1) = 'liberal'; |
|
|
0 = 'moderate'; |
|
|
c(1, 2) = 'conservative'") |
|
|
ideo_results <- lapply(0:4, function (treat) { |
|
|
|
|
|
out <- lapply(c("liberal", "moderate", "conservative"), function (i) { |
|
|
|
|
|
pid_mean <- with(tpnw, |
|
|
mean(join_tpnw[ideo == i & |
|
|
treatment == treat], |
|
|
na.rm = TRUE)) |
|
|
|
|
|
|
|
|
|
|
|
pid_boot <- replicate(10000, { |
|
|
dat <- tpnw$join_tpnw[tpnw$ideo == i & |
|
|
tpnw$treatment == treat] |
|
|
samp <- dat[sample(length(dat), |
|
|
replace = TRUE)] |
|
|
mean(samp, na.rm = TRUE) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return(c(mean = pid_mean, se = sd(pid_boot))) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
names(out) <- c("liberal", "moderate", "conservative") |
|
|
return(as.data.frame(out)) |
|
|
}) |
|
|
|
|
|
|
|
|
names(ideo_results) <- c("Control", paste(c("Group", "Security", "Norms", |
|
|
"Institutions"), "Cue")) |
|
|
|
|
|
|
|
|
|
|
|
w_main_results <- lapply(covars, function (covar) { |
|
|
|
|
|
|
|
|
form <- as.formula(paste(join_tpnw, paste(c(treats, covar), |
|
|
collapse = " + "), sep = " ~ ")) |
|
|
|
|
|
|
|
|
fit <- lm(form, data = tpnw, weights = anesrake_weight) |
|
|
|
|
|
|
|
|
ses <- sqrt(diag(vcovHC(fit, type = "HC2"))) |
|
|
|
|
|
|
|
|
reg_out <- cbind(fit$coef[2:5], ses[2:5]) |
|
|
|
|
|
|
|
|
colnames(reg_out) <- c("coef", "se") |
|
|
rownames(reg_out) <- treats |
|
|
|
|
|
|
|
|
return(as.data.frame(reg_out)) |
|
|
}) |
|
|
|
|
|
|
|
|
names(w_main_results) <- c("model_1", "model_2") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main_mat <- do.call("rbind", lapply(1:2, function (model) { |
|
|
cbind(main_results[[model]], model) |
|
|
})) |
|
|
|
|
|
|
|
|
z_90 <- qnorm(.95) |
|
|
z_95 <- qnorm(.975) |
|
|
|
|
|
|
|
|
setEPS() |
|
|
postscript("output/fg1.eps", width = 8, height = 5.5) |
|
|
|
|
|
|
|
|
par(mar = c(8, 7, 2, 2)) |
|
|
|
|
|
|
|
|
plot(0, type = "n", axes = FALSE, ann = FALSE, |
|
|
xlim = c(-.3, .05), ylim = c(.8, 4)) |
|
|
|
|
|
|
|
|
abline(v = seq(-.3, .05, .05)[-7], col = "lightgrey", lty = 3) |
|
|
|
|
|
|
|
|
par(new = TRUE) |
|
|
plot(x = main_mat$coef[main_mat$model == 1], y = 1:4 + .05, |
|
|
xlim = c(-.3, .05), ylim = c(.8, 4), pch = 16, col = "steelblue2", |
|
|
xlab = "", ylab = "", axes = FALSE) |
|
|
|
|
|
|
|
|
par(new = TRUE) |
|
|
plot(x = main_mat$coef[main_mat$model == 2], y = 1:4 - .05, |
|
|
xlim = c(-.3, .05), ylim = c(.8, 4), pch = 16, col = "#FF8F37", main = "", |
|
|
xlab = "", ylab = "", axes = FALSE) |
|
|
|
|
|
|
|
|
axis(side = 1, at = round(seq(-.3, 0, .05), 2), labels = FALSE) |
|
|
mtext(side = 1, at = seq(-.3, .1, .1), text = c("-30", "-20", "-10", "0"), |
|
|
cex = .9, line = .75) |
|
|
axis(side = 1, at = round(seq(-.25, .05, .05), 2), tck = -.01, labels = FALSE) |
|
|
|
|
|
|
|
|
axis(side = 2, at = 1:4, labels = FALSE) |
|
|
mtext(side = 2, line = .75, at = 1:4, |
|
|
text = paste(c("Group", "Security", "Norms", "Institutions"), "Cue"), |
|
|
las = 1, padj = .35, cex = .9) |
|
|
|
|
|
|
|
|
mtext(side = 2, line = 2.3, at = 4.2, text = "Treatment", |
|
|
font = 2, las = 1, xpd = TRUE) |
|
|
mtext(side = 1, text = "Estimated Effect Size", line = 2.5, at = -.15, font = 2) |
|
|
|
|
|
|
|
|
abline(v = 0.00, lty = 2) |
|
|
|
|
|
|
|
|
with(main_mat[main_mat$model == 1,], |
|
|
segments(x0 = coef - z_90 * se, y0 = 1:4 + .05, x1 = coef + z_90 * se, |
|
|
y1 = 1:4 + .05, col = "steelblue2", lwd = 3)) |
|
|
with(main_mat[main_mat$model == 2,], |
|
|
segments(x0 = coef - z_90 * se, y0 = 1:4 - .05, x1 = coef + z_90 * se, |
|
|
y1 = 1:4 - .05, col = "#FF8F37", lwd = 3)) |
|
|
|
|
|
|
|
|
with(main_mat[main_mat$model == 1,], |
|
|
segments(x0 = coef - z_95 *se, y0 = 1:4 + .05, x1 = coef + z_95 *se, |
|
|
y1 = 1:4 + .05, col = "steelblue2", lwd = 1)) |
|
|
with(main_mat[main_mat$model == 2,], |
|
|
segments(x0 = coef - z_95 *se, y0 = 1:4 - .05, x1 = coef + z_95 *se, |
|
|
y1 = 1:4 - .05, col = "#FF8F37", lwd = 1)) |
|
|
|
|
|
|
|
|
legend(legend = paste("Model", 1:2), x = -.15, y = -.275, horiz = TRUE, |
|
|
pch = 16, col = c("steelblue2", "#FF8F37"), xjust = .5, xpd = TRUE, |
|
|
text.width = .05, cex = .9) |
|
|
|
|
|
|
|
|
box() |
|
|
|
|
|
|
|
|
dev.off() |
|
|
|
|
|
|
|
|
|
|
|
tab_dat <- do.call("cbind", main_results) |
|
|
|
|
|
|
|
|
ctrl_form <- as.formula(paste(join_tpnw, paste(treats, |
|
|
collapse = " + "), sep = " ~ ")) |
|
|
|
|
|
|
|
|
ctrl_fit <- lm(ctrl_form, data = tpnw) |
|
|
|
|
|
|
|
|
ctrl_mean <- ctrl_fit$coef["(Intercept)"] |
|
|
|
|
|
|
|
|
ctrl_se <- sqrt(diag(vcovHC(ctrl_fit, "HC2")))["(Intercept)"] |
|
|
|
|
|
|
|
|
ctrl_results <- c(format(round(c(ctrl_mean, ctrl_se), 3) * 100, digits = 2), |
|
|
"|", "|") |
|
|
|
|
|
|
|
|
tab_dat <- apply(tab_dat, 2, function (y) format(round(y, 3) * 100, digits = 2)) |
|
|
|
|
|
|
|
|
tab <- rbind(ctrl_results, tab_dat) |
|
|
|
|
|
|
|
|
rownames(tab)[which(rownames(tab) == "1")] <- "control_mean" |
|
|
|
|
|
|
|
|
coef_cols <- grep("coef$", colnames(tab)) |
|
|
|
|
|
|
|
|
se_cols <- grep("se$", colnames(tab)) |
|
|
|
|
|
|
|
|
tab[,se_cols] <- apply(tab[, se_cols], 2, function (y) paste0("(", y, ")")) |
|
|
|
|
|
|
|
|
tab <- paste(paste(paste(capwords(gsub("_", " ", rownames(tab))), |
|
|
apply(tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
|
|
|
|
|
|
sink("output/main_results_tab.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{Estimated Treatment Effects on Support for TPNW}\n", |
|
|
"\\begin{adjustbox}{width = \\textwidth, center}\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\ttable-space-text-post = *,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{3.5cm}*{4}", |
|
|
"{S[table-number-alignment = right, table-column-width=1.25cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{4}{c}{Model}\\\\\\cmidrule{2-5}\n", |
|
|
"& \\multicolumn{2}{c}{{(1)}} & \\multicolumn{2}{c}{{(2)}} \\\\\\midrule\n", |
|
|
tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{adjustbox}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
|
|
|
aware_tab <- rbind(do.call("rbind", aware_results)) |
|
|
|
|
|
|
|
|
aware_tab <- apply(aware_tab, 2, function (y) format(round(y, 3) * 100, |
|
|
digits = 3)) |
|
|
|
|
|
|
|
|
mean_rows <- endsWith(rownames(aware_tab), "mean") |
|
|
|
|
|
|
|
|
se_rows <- endsWith(rownames(aware_tab), "se") |
|
|
|
|
|
|
|
|
aware_tab[se_rows,] <- paste0("(", aware_tab[se_rows,], ")") |
|
|
|
|
|
|
|
|
rownames(aware_tab)[se_rows] <- "" |
|
|
|
|
|
|
|
|
rownames(aware_tab)[mean_rows] <- gsub("_mean", "", |
|
|
rownames(aware_tab)[mean_rows]) |
|
|
|
|
|
|
|
|
|
|
|
aware_tab <- rbind(aware_tab, c("|", "|")) |
|
|
rownames(aware_tab)[nrow(aware_tab)] <- "Skipped" |
|
|
|
|
|
|
|
|
|
|
|
aware_tab[which(rownames(aware_tab) %in% names(aware_table)),] |
|
|
aware_tab <- cbind(aware_tab, "") |
|
|
colnames(aware_tab)[ncol(aware_tab)] <- "N" |
|
|
aware_tab[which(rownames(aware_tab) %in% names(aware_table)), "N"] <- aware_table |
|
|
|
|
|
|
|
|
aware_tab <- paste(paste(paste(capwords(gsub("_", " ", rownames(aware_tab))), |
|
|
apply(aware_tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), |
|
|
sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
|
|
|
|
|
|
sink("output/yougov_tab.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{YouGov Survey Responses}\n", |
|
|
"\\centering\\small\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{3.5cm}*{5}", |
|
|
"{S[table-number-alignment = right, table-column-width=1.25cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{5}{c}{Arm}\\\\\\cmidrule{2-6}\n", |
|
|
"& {Control} & {Group} & {Security} & {Norms} & {Institutions} \\\\\\midrule\n", |
|
|
aware_tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
|
|
|
tab_dat <- do.call("cbind", att_results) |
|
|
|
|
|
|
|
|
tab <- sapply(seq(0, 8, 2), function (i) { |
|
|
matrix(c(t(tab_dat[,1:2 + i])), 14, 1) |
|
|
}) |
|
|
|
|
|
|
|
|
tab <- apply(tab, 2, function (y) format(round(y, 3), digits = 3)) |
|
|
|
|
|
|
|
|
rownames(tab) <- paste(rep(rownames(tab_dat), each = 2), |
|
|
c("mean", "se"), sep = "_") |
|
|
|
|
|
|
|
|
mean_rows <- grep("_mean", rownames(tab)) |
|
|
|
|
|
|
|
|
se_rows <- grep("_se", rownames(tab)) |
|
|
|
|
|
|
|
|
tab[se_rows,] <- apply(tab[se_rows,], 1, function (y) { |
|
|
paste0("(", gsub(" ", "", y), ")") |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
rownames(tab) <- gsub("tpnw_atts|mean$|se$", "", rownames(tab)) |
|
|
|
|
|
|
|
|
rownames(tab) <- gsub("^_|_$", "", rownames(tab)) |
|
|
|
|
|
|
|
|
rownames(tab)[se_rows] <- "" |
|
|
|
|
|
|
|
|
tab <- paste(paste(paste(capwords(gsub("_", " ", rownames(tab))), |
|
|
apply(tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), |
|
|
sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
|
|
|
|
|
|
sink("output/atts_tab.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{Attitudes Toward Nuclear Weapons by Arm}\n", |
|
|
"\\centering\\small\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\ttable-space-text-post = *,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{3.5cm}*{5}", |
|
|
"{S[table-number-alignment = center, table-column-width=1.25cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{5}{c}{Arm}\\\\\\cmidrule{2-6}\n", |
|
|
"& {Control} & {Group} & {Security} & {Norms} & {Institutions} \\\\\\midrule\n", |
|
|
tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pid_tab <- lapply(pid_results, function (x) { |
|
|
matrix(unlist(x), nrow = 6, ncol = 1) |
|
|
}) |
|
|
pid_tab <- do.call("cbind", pid_tab) |
|
|
|
|
|
|
|
|
|
|
|
rownames(pid_tab) <- paste(rep(c("democrat", "independent", "republican"), |
|
|
each = 2), c("mean", "se")) |
|
|
|
|
|
|
|
|
mean_rows <- endsWith(rownames(pid_tab), "mean") |
|
|
|
|
|
|
|
|
se_rows <- endsWith(rownames(pid_tab), "se") |
|
|
|
|
|
|
|
|
colnames(pid_tab) <- c("control", treats) |
|
|
|
|
|
|
|
|
pid_tab[mean_rows, treats] <- pid_tab[mean_rows, treats] - |
|
|
pid_tab[mean_rows, "control"] |
|
|
|
|
|
|
|
|
pid_tab <- apply(pid_tab, 2, function (y) format(round(y, 3) * 100, digits = 3)) |
|
|
|
|
|
|
|
|
pid_tab <- gsub(" ", "", pid_tab) |
|
|
|
|
|
|
|
|
pid_tab[se_rows,] <- paste0("(", pid_tab[se_rows,], ")") |
|
|
|
|
|
|
|
|
rownames(pid_tab)[se_rows] <- "" |
|
|
|
|
|
|
|
|
pid_tab <- paste(paste(paste(capwords(gsub("_", " ", rownames(pid_tab))), |
|
|
apply(pid_tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), |
|
|
sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
|
|
|
|
|
|
sink("output/pid_support.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{Support for Joining TPNW by Party ID}\n", |
|
|
"\\centering\\small\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{3.5cm}*{5}", |
|
|
"{S[table-number-alignment = right, table-column-width=1.25cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{5}{c}{Arm}\\\\\\cmidrule{2-6}\n", |
|
|
"& {Control} & {Group} & {Security} & {Norms} & {Institutions} \\\\\\midrule\n", |
|
|
pid_tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ideo_tab <- lapply(ideo_results, function (x) { |
|
|
matrix(unlist(x), nrow = 6, ncol = 1) |
|
|
}) |
|
|
ideo_tab <- do.call("cbind", ideo_tab) |
|
|
|
|
|
|
|
|
|
|
|
rownames(ideo_tab) <- paste(rep(c("liberal", "moderate", "conservative"), |
|
|
each = 2), c("mean", "se")) |
|
|
|
|
|
|
|
|
ideo_tab <- apply(ideo_tab, 2, function (y) format(round(y, 3) * 100, |
|
|
digits = 3)) |
|
|
|
|
|
|
|
|
mean_rows <- endsWith(rownames(ideo_tab), "mean") |
|
|
|
|
|
|
|
|
se_rows <- endsWith(rownames(ideo_tab), "se") |
|
|
|
|
|
|
|
|
ideo_tab[se_rows,] <- paste0("(", ideo_tab[se_rows,], ")") |
|
|
|
|
|
|
|
|
rownames(ideo_tab)[se_rows] <- "" |
|
|
|
|
|
|
|
|
ideo_tab <- paste(paste(paste(capwords(gsub("_", " ", rownames(ideo_tab))), |
|
|
apply(ideo_tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), |
|
|
sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
|
|
|
|
|
|
sink("output/ideo_support_tab.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{Support for Joining TPNW by Ideology}\n", |
|
|
"\\centering\\small\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{3.5cm}*{5}", |
|
|
"{S[table-number-alignment = right, table-column-width=1.25cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{5}{c}{Arm}\\\\\\cmidrule{2-6}\n", |
|
|
"& {Control} & {Group} & {Security} & {Norms} & {Institutions} \\\\\\midrule\n", |
|
|
ideo_tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
|
|
|
w_tab_dat <- do.call("cbind", w_main_results) |
|
|
|
|
|
|
|
|
w_ctrl_form <- as.formula(paste(join_tpnw, paste(treats, |
|
|
collapse = " + "), sep = " ~ ")) |
|
|
|
|
|
|
|
|
w_ctrl_fit <- lm(w_ctrl_form, data = tpnw, |
|
|
weights = anesrake_weight) |
|
|
|
|
|
|
|
|
w_ctrl_mean <- w_ctrl_fit$coef["(Intercept)"] |
|
|
|
|
|
|
|
|
w_ctrl_se <- sqrt(diag(vcovHC(w_ctrl_fit, "HC2")))["(Intercept)"] |
|
|
|
|
|
|
|
|
|
|
|
w_ctrl_results <- c(format(round(c(w_ctrl_mean, w_ctrl_se), 3) * 100, |
|
|
digits = 2), "|", "|") |
|
|
|
|
|
|
|
|
w_tab_dat <- apply(w_tab_dat, 2, function (y) format(round(y, 3) * 100, |
|
|
digits = 2)) |
|
|
|
|
|
|
|
|
w_tab <- rbind(w_ctrl_results, w_tab_dat) |
|
|
|
|
|
|
|
|
rownames(w_tab)[which(rownames(w_tab) == "1")] <- "control_mean" |
|
|
|
|
|
|
|
|
coef_cols <- grep("coef$", colnames(w_tab)) |
|
|
|
|
|
|
|
|
se_cols <- grep("se$", colnames(w_tab)) |
|
|
|
|
|
|
|
|
w_tab[,se_cols] <- apply(w_tab[, se_cols], 2, function (y) paste0("(", y, ")")) |
|
|
|
|
|
|
|
|
w_tab <- paste(paste(paste(capwords(gsub("_", " ", rownames(w_tab))), |
|
|
apply(w_tab, 1, function (x) { |
|
|
paste(x, collapse = " & ") |
|
|
}), sep = " & "), collapse = " \\\\\n"), "\\\\\n") |
|
|
|
|
|
|
|
|
sink("output/weighted_main_results_tab.tex") |
|
|
cat("\\begin{table}\n", |
|
|
"\\caption{Estimated Treatment Effects on Support for TPNW (Weighted)}\n", |
|
|
"\\begin{adjustbox}{width = \\textwidth, center}\n", |
|
|
"\\sisetup{\n", |
|
|
"\tdetect-all,\n", |
|
|
"\ttable-number-alignment = center,\n", |
|
|
"\ttable-figures-integer = 1,\n", |
|
|
"\ttable-figures-decimal = 3,\n", |
|
|
"\ttable-space-text-post = *,\n", |
|
|
"\tinput-symbols = {()}\n", |
|
|
"}\n", |
|
|
paste0("\\begin{tabular}{@{\\extracolsep{5pt}}L{3.5cm}*{4}", |
|
|
"{S[table-number-alignment = right, table-column-width=1.25cm]}}\n"), |
|
|
"\\toprule\n", |
|
|
"& \\multicolumn{4}{c}{Model}\\\\\\cmidrule{2-5}\n", |
|
|
"& \\multicolumn{2}{c}{{(1)}} & \\multicolumn{2}{c}{{(2)}} \\\\\\midrule\n", |
|
|
w_tab, |
|
|
"\\bottomrule\n", |
|
|
"\\end{tabular}\n", |
|
|
"\\end{adjustbox}\n", |
|
|
"\\end{table}\n") |
|
|
sink() |
|
|
|
|
|
|
|
|
save.image(file = "output/hbg_replication_out.RData") |
|
|
|