|
| 1 | +# =================================================== |
| 2 | +# Plots Redistribution questions |
| 3 | +# =================================================== |
| 4 | +# Version: Feb 17th 2026 |
| 5 | + |
| 6 | +# -- 1. Load packages |
| 7 | +library(tidyverse) |
| 8 | +library(modelsummary) |
| 9 | +library(broom) |
| 10 | +library(ggplot2) |
| 11 | + |
| 12 | +library(tibble) |
| 13 | +library(purrr) |
| 14 | + |
| 15 | +# -- 2. Load data |
| 16 | +df <- read.csv("data/clean_df_valid.csv") |
| 17 | + |
| 18 | +# === 1. Setup === |
| 19 | +# Dependent variables |
| 20 | +dvs <- c( |
| 21 | + "redis_effort_num", |
| 22 | + "redis_intelligence_num", |
| 23 | + "redis_no_cheat_system_num", |
| 24 | + "redis_opportunity_num", |
| 25 | + "redis_reasons_poor_num", |
| 26 | + "redis_reasons_rich_num", |
| 27 | + "redis_social_benefits_num", |
| 28 | + "redis_welfare_num" |
| 29 | +) |
| 30 | + |
| 31 | +# Labels for plotting |
| 32 | +dv_labels <- c( |
| 33 | + redis_intelligence_num = "Rewarded for \n intelligence and skill", |
| 34 | + redis_opportunity_num = "Equal opportunity \n to get ahead", |
| 35 | + redis_reasons_rich_num = "Non-violating outcomes \n Rich", |
| 36 | + redis_reasons_poor_num = "Non-violating outcomes \n Poor", |
| 37 | + redis_effort_num = "Fairness of the \n income distribution", |
| 38 | + redis_social_benefits_num = "Social benefits \n not a choice", |
| 39 | + redis_welfare_num = "Welfare doesn't go to \n the underserving", |
| 40 | + redis_no_cheat_system_num = "Trust not to cheat \n system" |
| 41 | +) |
| 42 | + |
| 43 | +# Independent variables (covariates) |
| 44 | +ivs <- c( |
| 45 | +# "ses_male_bin", # Male |
| 46 | +# "age18_34_bin", # Age (18–34) |
| 47 | +# "incomeHigh_bin", # Income (High) |
| 48 | +# "educBHS", # BHS |
| 49 | +# "children_bin", # Children |
| 50 | +# "employ_fulltime_bin", # Employed full time |
| 51 | +# "ideo_right_num", # Right ideology |
| 52 | +# "trust_social_bin", # Trust in society |
| 53 | + # "ses_region_cat", # Regions |
| 54 | + "ses_income3Cat" # Quebec region |
| 55 | +) |
| 56 | +df$ses_income3Cat |
| 57 | +# === 2. Fit models === |
| 58 | +# Models *with* covariates |
| 59 | +ols_models_cov <- map(dvs, function(dv) { |
| 60 | + frm <- as.formula(paste(dv, "~", paste(ivs, collapse = " + "))) |
| 61 | + lm(frm, data = df) |
| 62 | +}) |
| 63 | +names(ols_models_cov) <- dv_labels |
| 64 | + |
| 65 | +# Models *without* covariates (intercept-only) |
| 66 | +ols_models_nocov <- map(dvs, function(dv) { |
| 67 | + frm <- as.formula(paste(dv, "~ 1")) |
| 68 | + lm(frm, data = df) |
| 69 | +}) |
| 70 | +names(ols_models_nocov) <- dv_labels |
| 71 | + |
| 72 | +# === 3. Extract predicted means and CIs === |
| 73 | +get_predictions <- function(models, model_type) { |
| 74 | + map2_dfr(models, names(models), function(model, label) { |
| 75 | + preds <- predict(model, se.fit = TRUE) |
| 76 | + fit <- mean(preds$fit) |
| 77 | + se <- sqrt(mean(preds$se.fit^2)) |
| 78 | + |
| 79 | + tibble( |
| 80 | + treatment = label, |
| 81 | + estimate = fit, |
| 82 | + conf.low = fit - 1.96 * se, |
| 83 | + conf.high = fit + 1.96 * se, |
| 84 | + model = model_type |
| 85 | + ) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +pred_cov <- get_predictions(ols_models_cov, "With Covariates") |
| 90 | +pred_nocov <- get_predictions(ols_models_nocov, "Without Covariates") |
| 91 | + |
| 92 | +# Combine both |
| 93 | +pred_all <- bind_rows(pred_cov, pred_nocov) |
| 94 | +pred_all$treatment <- factor(pred_all$treatment, levels = rev(dv_labels)) # ordered |
| 95 | + |
| 96 | +# Order |
| 97 | +pred_all$treatment <- factor(pred_all$treatment, levels = dv_labels) |
| 98 | +# === 4. Plot === |
| 99 | +plot <- ggplot(pred_all, aes(x = treatment, y = estimate, color = model, shape = model)) + |
| 100 | + geom_point(position = position_dodge(width = 0.5), size = 3) + |
| 101 | + geom_errorbar( |
| 102 | + aes(ymin = conf.low, ymax = conf.high), |
| 103 | + position = position_dodge(width = 0.5), |
| 104 | + width = 0.15 |
| 105 | + ) + |
| 106 | + scale_color_manual( |
| 107 | + values = c( |
| 108 | + "Without Covariates" = "black", |
| 109 | + "With Covariates" = "grey50" |
| 110 | + ) |
| 111 | + ) + |
| 112 | + scale_shape_manual( |
| 113 | + values = c( |
| 114 | + "Without Covariates" = 16, # solid circle |
| 115 | + "With Covariates" = 17 # solid triangle |
| 116 | + ) |
| 117 | + ) + |
| 118 | + scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)) + |
| 119 | + labs( |
| 120 | + # title = "Average Support for Redistribution", |
| 121 | + # subtitle = "OLS predicted means with and without covariates (95% CIs)", |
| 122 | + x = " ", |
| 123 | + y = "Predicted Support \n (0–1 scale)", |
| 124 | + |
| 125 | + color = "Model Type", |
| 126 | + shape = "Model Type" #, |
| 127 | + #caption = "Note: Covariate-adjusted models include: age, gender, education, employment, children, homeownership, ideology, and trust." |
| 128 | + ) + |
| 129 | + theme_minimal(base_size = 13) + |
| 130 | + theme( |
| 131 | + plot.caption.position = "plot", # ensures caption aligns with the plot area |
| 132 | + plot.caption = element_text(hjust = 0) # left-justify the caption |
| 133 | + ) |
| 134 | + |
| 135 | +plot |
| 136 | + |
| 137 | +# -- 8. Save the coefficient plot |
| 138 | +ggsave( |
| 139 | + filename = "graphs/avgSupport_redistribution.png", |
| 140 | + plot = plot, |
| 141 | + width = 10, |
| 142 | + height = 8, |
| 143 | + dpi = 300 |
| 144 | +) |
| 145 | + |
| 146 | +########################### |
| 147 | +########################### |
| 148 | +######################### |
| 149 | + |
| 150 | + |
| 151 | +# --- 1. Variables --- |
| 152 | +dvs <- names(dv_labels) |
| 153 | + |
| 154 | +ivs <- c("ses_income3Cat") # 3-category income factor |
| 155 | + |
| 156 | +# Ensure the income variable is a factor |
| 157 | +# Ensure ses_income3Cat is a factor and has all levels you want |
| 158 | +df$ses_income3Cat <- factor(df$ses_income3Cat, |
| 159 | + levels = c("Low", "Medium", "High")) |
| 160 | + |
| 161 | +# Remove rows where DV or IV is NA for the current DV |
| 162 | +# AFTER: capture full levels first, then clean, then restore levels |
| 163 | +get_income_predictions <- function(dv, data, income_var = "ses_income3Cat") { |
| 164 | + |
| 165 | + # Capture the full set of levels BEFORE dropping NAs |
| 166 | + full_levels <- levels(data[[income_var]]) |
| 167 | + |
| 168 | + data_clean <- data %>% |
| 169 | + select(all_of(c(dv, income_var))) %>% |
| 170 | + drop_na() %>% |
| 171 | + mutate(!!income_var := factor(.data[[income_var]], levels = full_levels)) # restore all levels |
| 172 | + |
| 173 | + formula <- as.formula(paste(dv, "~", income_var)) |
| 174 | + model <- lm(formula, data = data_clean) |
| 175 | + |
| 176 | + newdata <- setNames( |
| 177 | + data.frame(factor(full_levels, levels = full_levels)), |
| 178 | + income_var |
| 179 | + ) |
| 180 | + |
| 181 | + preds <- predict(model, newdata = newdata, se.fit = TRUE) |
| 182 | + |
| 183 | + tibble( |
| 184 | + dv = dv_labels[dv], |
| 185 | + income = newdata[[income_var]], |
| 186 | + estimate = preds$fit, |
| 187 | + conf.low = preds$fit - 1.96 * preds$se.fit, |
| 188 | + conf.high = preds$fit + 1.96 * preds$se.fit |
| 189 | + ) |
| 190 | +} |
| 191 | + |
| 192 | +pred_all <- map_dfr(dvs, ~ get_income_predictions(.x, df)) |
| 193 | + |
| 194 | +# Make dv a factor for faceting |
| 195 | +pred_all$dv <- factor(pred_all$dv, levels = dv_labels) |
| 196 | + |
| 197 | +# Plot |
| 198 | +ggplot(pred_all, aes(x = income, y = estimate, color = income)) + |
| 199 | + geom_point(size = 3) + |
| 200 | + geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2) + |
| 201 | + facet_wrap(~ dv, scales = "free_y") + |
| 202 | + labs( |
| 203 | + x = "Income Category", |
| 204 | + y = "Predicted Support (0–1 scale)", |
| 205 | + title = "Predicted Redistribution Support by Income Category", |
| 206 | + caption = "OLS models with income as predictor; 95% confidence intervals shown" |
| 207 | + ) + |
| 208 | + theme_minimal(base_size = 13) + |
| 209 | + theme( |
| 210 | + legend.position = "none", |
| 211 | + strip.text = element_text(size = 12) |
| 212 | + ) |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +# Check observation counts for each DV × income category combination |
| 219 | +library(purrr) |
| 220 | + |
| 221 | +walk(dvs, function(dv) { |
| 222 | + cat("\n---", dv, "---\n") |
| 223 | + df %>% |
| 224 | + select(all_of(c(dv, "ses_income3Cat"))) %>% |
| 225 | + drop_na() %>% |
| 226 | + count(ses_income3Cat, .drop = FALSE) %>% |
| 227 | + print() |
| 228 | +}) |
| 229 | + |
| 230 | +table(df$ses_income3Cat) |
0 commit comments