```{r}
#| message: false
#| warning: false
#| label: run-regressions
# Function to run regressions for a variable
run_regressions <- function(
var,
reg_data = df,
fe_spec = "| district_id + period + wave + enum_id + day_of_week + cohort",
fe_spec_endline = "| district_id + wave + enum_id + day_of_week + cohort",
controls = paste0(var, "_bl + hh_size + census_wealth_index")) {
reg_data_bl <- reg_data
reg_data <- reg_data %>%
filter(period > 0)
# By Treatment groups
group_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
# By Treatment groups - including baseline
group_reg_with_baseline <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) ", fe_spec)),
cluster = "hh_id",
data = reg_data_bl
)
group_reg_no_endline <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) ", fe_spec)),
cluster = "hh_id",
data = reg_data %>% filter(period < 6)
)
# By Treatment groups - balanced_risky
group_reg_balanced <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment_balanced) ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
# By Treatment groups - balanced_risky
group_reg_split <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment_split) ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
income_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + study_income ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
income_and_treatment_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) + study_income ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
income_treatment_interaction_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) * study_income ", fe_spec)),
cluster = "hh_id",
data = reg_data,
collin.tol = 1e-5
)
draw_imputed_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(draw_imputed, ref = 'C') + unpredictable ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
draw_imputed_with_lag_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(draw_imputed, ref = 'C') + i(draw_imputed_lag1, ref = 'C') + unpredictable ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
draw_realized_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(draw_realized, ref = 'C') + unpredictable ", fe_spec)),
cluster = "hh_id",
data = reg_data %>% filter(draw_realized != "")
)
draw_realized_with_lag_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(draw_realized, ref = 'C') + i(draw_realized_lag1, ref = 'C') + unpredictable ", fe_spec)),
cluster = "hh_id",
data = reg_data %>% filter(draw_realized != "" & draw_realized_lag1 != "")
)
# By Treatment groups -> Using balanced panel of people who completed all surveys
balanced_panel_reg <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) ", fe_spec)),
cluster = "hh_id",
data = reg_data %>% filter(completed_all_surveys == 1)
)
endline_only <- feols(as.formula(paste0(var, " ~ ", controls, " + i(treatment) ", fe_spec_endline)),
cluster = "community_id",
data = reg_data %>% filter(period == 6)
)
treated_v_control <- feols(as.formula(paste0(var, " ~ ", controls, " + treated ", fe_spec)),
cluster = "hh_id",
data = reg_data
)
treated_v_control_endline <- feols(as.formula(paste0(var, " ~ ", controls, " + treated ", fe_spec_endline, " + enum_id")),
cluster = "community_id",
data = reg_data %>% filter(period == 6)
)
# Create named list using simple numeric names to avoid HTML encoding
return(setNames(
list(
group_reg, group_reg_balanced, group_reg_split,
income_reg, income_and_treatment_reg, income_treatment_interaction_reg,
draw_imputed_reg, draw_imputed_with_lag_reg, draw_realized_reg,
draw_realized_with_lag_reg, balanced_panel_reg, endline_only,
treated_v_control, treated_v_control_endline, group_reg_with_baseline, group_reg_no_endline
),
c(
var,
paste0(var, " - Balanced Risky"),
paste0(var, " - Split Risky"),
paste0(var, " - Income"),
paste0(var, " - Income + Arm"),
paste0(var, " - Income X Arm"),
paste0(var, " - Imputed Draw"),
paste0(var, " - Imputed Draw with Lag"),
paste0(var, " - Realized Draw"),
paste0(var, " - Realized Draw with Lag"),
paste0(var, " - Balanced Panel"),
paste0(var, " - Endline Only"),
paste0(var, " - Treated vs Control"),
paste0(var, " - Treated vs Control (Endline Only)"),
paste0(var, " - Baseline Included"),
paste0(var, " - Excluding Endline")
)
))
}
allLabels <- c(
"treatment" = "Arm",
"treated" = "Treated",
"treatment::1" = "Stable",
"treatment::2" = "Predictable",
"treatment::3" = "Risky",
"treatment_balanced::1" = "Stable",
"treatment_balanced::2" = "Predictable",
"treatment_balanced::3" = "Risky (Balanced)",
"treatment_split::1" = "Stable",
"treatment_split::2" = "Predictable",
"treatment_split::3" = "Risky Medium",
"treatment_split::4" = "Risky High",
"treatment_split::5" = "Risky Low",
"draw_realized::H" = "High Draw",
"draw_realized::M" = "Medium Draw",
"draw_realized::L" = "Low Draw",
"draw_realized_lag1::H" = "High Draw previous period",
"draw_realized_lag1::M" = "Medium Draw previous period",
"draw_realized_lag1::L" = "Low Draw previous period",
"draw_imputed::H" = "High Draw",
"draw_imputed::M" = "Medium Draw",
"draw_imputed::L" = "Low Draw",
"draw_imputed_lag1::H" = "High Draw previous period",
"draw_imputed_lag1::M" = "Medium Draw previous period",
"draw_imputed_lag1::L" = "Low Draw previous period",
"study_income" = "Study Income",
"study_income:treatment::1" = "Stable × Study Income",
"study_income:treatment::2" = "Predictable × Study Income",
"study_income:treatment::3" = "Risky × Study Income"
# add labels for interaction between study income and arm
)
makeTable <- function(models) {
table <- modelsummary(models,
escape = FALSE,
output = "kableExtra",
estimate = "{estimate}{stars}",
statistic = "{std.error} ({conf.low}, {conf.high})",
coef_omit = "_bl|hh_size|census_wealth_index",
coef_rename = allLabels,
coef_omit_sources = FALSE,
stars = c(`***` = 0.01, `**` = 0.05, `*` = 0.1),
gof_map = tribble(
~raw, ~clean, ~fmt,
"nobs", "Observations", 0,
"FE", "Community FE", 1,
)
) %>%
kable_classic(full_width = F, html_font = "Cambria", fixed_thead = T)
table
}
# Helper: given a vector of model indices, collect models for all dependent_vars
collect_models <- function(indices) {
unlist(lapply(indices, function(i) {
lapply(dependent_vars, function(v) models[[v]][[i]])
}), recursive = FALSE)
}
```