##############################
# HETEROGENEOUS EFFECTS ANALYSIS
#
#
# Part of replication files for:
# Dill, Janina, Howlett, Marnie, & Müller-Crepon, Carl (2025).
# Do Ukrainians Still Prefer Self-Defense Against Russia At Any Cost? 
# Journal of Peace Research, forthcoming.
#
#############################

# Main results
for(e in c(as.list(1:2), list(1:2))){
  print(paste("Experiment", e))
  # Subset 
  data.sub <- data.long[data.long$experiment %in% e & data.long$in_2024 == 1, ]
  
  # Affectedness score
  # Estimate
  pca <- prcomp(data.frame(lapply(na.omit(data.sub[,hetvars$Affectedness[-1]]), function(x){as.numeric(as.factor(x))})),
                center = TRUE, 
                scale. = TRUE) 
  print(summary(pca))
  print(pca)
  
  # Save to data
  data.sub[apply(data.sub[,hetvars$Affectedness[-1]], 1, function(x){!any(is.na(x))}), "aff_score"] <- 
    pca$x[,1]
  
  # Cut into 3 values
  data.sub$aff_score <- cut(data.sub$aff_score, c(-99, quantile(data.sub$aff_score, 1:3/3, na.rm = T)),
                            labels = c("low", "medium", "high"))
  
  # These het.vars
  these.hetvars <- lapply(hetvars, function(h){
    h[sapply(h, function(v){any(!is.na(data.sub[, v]))})]
  })
  
  # Het Effects Table
  all.df <- heteff_acme(df = data.sub,
                        outvars = outvars,
                        hetvars = these.hetvars,
                        attr.vec = 1:5,
                        attr.labs = attr.labs, 
                        prefix = paste0("exp", paste(e, collapse = ""), "_heteff"), 
                        summary = TRUE, 
                        plot = ifelse(paste(e, collapse = "") == "12", T, F))

}



## With Location Slopes -- identity
loc.sl.df <- heteff_acme_fsl(df = data.long[data.long$experiment %in% 1:2 & 
                                              data.long$in_2024 == 1, ],
                             outvars = outvars,
                             hetvars = list(c("Interview language" = "lang",  
                                              "Native language" = "natlang",
                                              "Ethnic identity" = "ethnic")),
                             attr.vec = 1:5,
                             attr.labs = attr.labs, 
                             prefix = paste0("exp12", "_fsl"), 
                             summary = FALSE, plot = TRUE,
                             fsl = paste(paste0("PSU[", paste0("attr",rep(1:5, 2), "_d", rep(2:3, each = 5)), "]"),
                                         collapse = " +"),
                             refgrp = "Ukrainian")



## With Group Slopes -- identity
loc.sl.df <- heteff_acme_fsl(df = data.long[data.long$experiment %in% 1:2 & 
                                              data.long$in_2024 == 1, ],
                             outvars = outvars,
                             hetvars = list(c( "East vs. West" = "region")),
                             attr.vec = 1:5,
                             attr.labs = attr.labs, 
                             prefix = paste0("exp12", "_fsl"), 
                             summary = FALSE, plot = TRUE,
                             fsl = paste(paste0("natlang[", paste0("attr",rep(1:5, 2), "_d", rep(2:3, each = 5)), "]"),
                                         collapse = " +"),
                             refgrp = "West")

loc.sl.df <- heteff_acme_fsl(df = data.long[data.long$experiment %in% 1:2 & 
                                              data.long$in_2024 == 1, ],
                             outvars = outvars,
                             hetvars = list(c("Oblast first attacked" = "diraffected")),
                             attr.vec = 1:5,
                             attr.labs = attr.labs, 
                             prefix = paste0("exp12", "_fsl"), 
                             summary = FALSE, plot = TRUE,
                             fsl = paste(paste0("natlang[", paste0("attr",rep(1:5, 2), "_d", rep(2:3, each = 5)), "]"),
                                         collapse = " +"),
                             refgrp = "No")





