Skip to content

Commit 2c3bc24

Browse files
author
ShannonDinan
committed
Shannon
1 parent 4e8cc0a commit 2c3bc24

File tree

9 files changed

+398
-10
lines changed

9 files changed

+398
-10
lines changed

.DS_Store

2 KB
Binary file not shown.

.Rproj.user/B338ED4E/pcs/files-pane.pper

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"ascending": true
66
}
77
],
8-
"path": "~/Library/CloudStorage/Dropbox/RESEARCH/_COLLABORATIONS/_GitHub/ACA"
8+
"path": "~/Library/CloudStorage/Dropbox/_RESEARCH/_COLLABORATIONS/_GitHub/ACA"
99
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"activeTab": 0,
2+
"activeTab": 3,
33
"activeTabSourceWindow0": 0
44
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"left": {
3-
"splitterpos": 311,
4-
"topwindowstate": "MINIMIZE",
5-
"panelheight": 830,
6-
"windowheight": 868
3+
"splitterpos": 401,
4+
"topwindowstate": "NORMAL",
5+
"panelheight": 980,
6+
"windowheight": 1018
77
},
88
"right": {
9-
"splitterpos": 386,
9+
"splitterpos": 456,
1010
"topwindowstate": "NORMAL",
11-
"panelheight": 830,
12-
"windowheight": 868
11+
"panelheight": 980,
12+
"windowheight": 1018
1313
}
1414
}

.Rproj.user/B338ED4E/sources/prop/INDEX

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
~%2FLibrary%2FCloudStorage%2FDropbox%2FMac%2FDesktop%2FavgSupport_redisAll.R="7B9A896F"
2+
~%2FLibrary%2FCloudStorage%2FDropbox%2FMac%2FDesktop%2FredisModels.R="805210DC"
13
~%2FLibrary%2FCloudStorage%2FDropbox%2FRESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2FbinaryTests.R="BADB86D1"
24
~%2FLibrary%2FCloudStorage%2FDropbox%2FRESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FB7B_plots.R="0E7B6A27"
35
~%2FLibrary%2FCloudStorage%2FDropbox%2FRESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2Fanesrake_fail.R="8C60D8CD"
@@ -33,11 +35,17 @@
3335
~%2FLibrary%2FCloudStorage%2FDropbox%2FRESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcodex_test.R="96F134AE"
3436
~%2FLibrary%2FCloudStorage%2FDropbox%2FRESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2FpolrModels.R="5D7B316E"
3537
~%2FLibrary%2FCloudStorage%2FDropbox%2FRESEARCH%2F_COLLABORATIONS%2F_GitHub%2FsurveyAnalysis%2Fcode%2Fcleaning.R="8DDB6E97"
38+
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2Faca_wrangling_W26.R="92CC70AA"
3639
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FavgSupport_Taxation.R="C0DF1AED"
3740
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FavgSupport_experimentGE.R="456E54CB"
41+
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FavgSupport_redisAll.R="88B82E40"
3842
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2Fcleaning.R="292DDC0A"
3943
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2Fcleaning_jan26.R="C717B950"
44+
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FdescStats.R="2E34B61A"
45+
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FredisModels.R="FF225D0D"
4046
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FvarList.R="D2BDFC09"
4147
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2F_GitHub%2FACA%2Fcode%2FvarList_jan26.R="9317804F"
48+
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2Fz_INACTIVE%2FAxelDery_ShannonDinan%2FCPP%20Article%20(2023)%2Fanalyses.R="D69F9B9E"
49+
~%2FLibrary%2FCloudStorage%2FDropbox%2F_RESEARCH%2F_COLLABORATIONS%2Fz_INACTIVE%2FAxelDery_ShannonDinan%2FCPP%20Article%20(2023)%2FanalysesCoT.R="A7F7D1A7"
4250
~%2FLibrary%2FCloudStorage%2FDropbox%2FsampleLLM%2FNtests.R="51CACD11"
4351
~%2FLibrary%2FCloudStorage%2FDropbox%2Ftest%2FtestFiles.R="E4BFB0F1"

.Rproj.user/shared/notebooks/paths

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/Users/shannondinan/Library/CloudStorage/Dropbox/Mac/Desktop/avgSupport_redisAll.R="79510E0B"
2+
/Users/shannondinan/Library/CloudStorage/Dropbox/Mac/Desktop/redisModels.R="500527C2"
13
/Users/shannondinan/Library/CloudStorage/Dropbox/_RESEARCH/_COLLABORATIONS/_GitHub/ACA/code/aca_cleaning_W26.R="6D90A987"
24
/Users/shannondinan/Library/CloudStorage/Dropbox/_RESEARCH/_COLLABORATIONS/_GitHub/ACA/code/aca_wrangling_W26.R="8AFD7265"
35
/Users/shannondinan/Library/CloudStorage/Dropbox/_RESEARCH/_COLLABORATIONS/_GitHub/ACA/code/avgSupp_codingIdea_Claude.R="DCA0FD7D"

code/avgSupport_redisAll.R

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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

Comments
 (0)