#Title: dwv_ps6_ms_2_dropmissing.R
#Author: Jack Reilly
#Purpose: Load data
# requires: working directory, libraries, gss_subset
# result: gss_collapse

#There is also an experiment the gss starts running with naming of these variables after 2021
#We'll ignore that and just look up to 2021

gss_sub <- filter(gss_sub, year >= 1976 & year <= 2021)


table(gss_sub$year)

table(gss_sub$colath)
unique(gss_sub$spkath)

gss_sub <- gss_sub |>
  mutate(
    ath1  = case_when(spkath == 1 ~ 0,
                      spkath == 2 ~ 1),
    ath2  = case_when(colath == 4 ~ 0,
                      colath == 5 ~ 1),
    ath3  = case_when(libath == 2 ~ 0,
                      libath == 1 ~ 1),
    
    rac1  = case_when(spkrac == 1 ~ 0,
                      spkrac == 2 ~ 1),
    rac2  = case_when(colrac == 4 ~ 0,
                      colrac == 5 ~ 1),
    rac3  = case_when(librac == 2 ~ 0,
                      librac == 1 ~ 1),
    
    com1  = case_when(spkcom == 1 ~ 0,
                      spkcom == 2 ~ 1),
    com2  = case_when(colcom == 5 ~ 0,   # note: colcom 4=1, 5=0
                      colcom == 4 ~ 1),
    com3  = case_when(libcom == 2 ~ 0,
                      libcom == 1 ~ 1),
    
    mil1  = case_when(spkmil == 1 ~ 0,
                      spkmil == 2 ~ 1),
    mil2  = case_when(colmil == 4 ~ 0,
                      colmil == 5 ~ 1),
    mil3  = case_when(libmil == 2 ~ 0,
                      libmil == 1 ~ 1),
    
    hom1 = case_when(spkhomo == 1 ~ 0,
                      spkhomo == 2 ~ 1),
    hom2 = case_when(colhomo == 4 ~ 0,
                      colhomo == 5 ~ 1),
    hom3 = case_when(libhomo == 2 ~ 0,
                      libhomo == 1 ~ 1)
  )

#Add all the variables together to make stouffer for fig1
gss_sub$stouffer <- gss_sub$ath1 + gss_sub$ath2 + gss_sub$ath3 + gss_sub$rac1 + gss_sub$rac2 + gss_sub$rac3 + gss_sub$com1 + gss_sub$com2 + gss_sub$com3 + gss_sub$mil1 + gss_sub$mil2 + gss_sub$mil3 + gss_sub$hom1 + gss_sub$hom2 + gss_sub$hom3
gss_sub$stouffer2 <- rowSums(gss_sub[, c("ath1", "ath2", "ath3", "rac1", "rac2", "rac3", "com1", "com2", "com3", "mil1", "mil2", "mil3", "hom1", "hom2", "hom3")], na.rm = TRUE)
gss_sub$stouffer3 <- rowSums(gss_sub[, c("ath1", "ath2", "ath3", "rac1", "rac2", "rac3", "com1", "com2", "com3", "mil1", "mil2", "mil3", "hom1", "hom2", "hom3")])

cor(gss_sub$stouffer, gss_sub$stouffer2, use = "pairwise.complete.obs")
cor(gss_sub$stouffer, gss_sub$stouffer3, use = "pairwise.complete.obs")
cor(gss_sub$stouffer2, gss_sub$stouffer3, use = "pairwise.complete.obs")

sum(!is.na(gss_sub$stouffer))
sum(!is.na(gss_sub$stouffer2))
sum(!is.na(gss_sub$stouffer3))

#What am I going to do here?
#Missing data management is important - here, I'm going to decide that I *just* want to have observations where I have complete data
#So I'm going to use the first or third commands

#Add rac and hom variables together for fig 2
gss_sub$rac = gss_sub$rac1 + gss_sub$rac2 + gss_sub$rac3
gss_sub$hom = gss_sub$hom1 + gss_sub$hom2 + gss_sub$hom3

#Generate intolerance variables - keep 0 as 0; keep missing as missing, else; turn into 1
gss_sub <- gss_sub |>
  mutate(
    intol = case_when(
      stouffer == 0 ~ 0,
      stouffer %in% 1:15 ~ 1  # everything else
    )
  )

#Look at our results
table(gss_sub$stouffer)
table(gss_sub$rac)
table(gss_sub$hom)
table(gss_sub$intol)

gss_collapse <- gss_sub |>
  group_by(year) |>
  summarize(
    stouffer = mean(stouffer, na.rm = T),
    rac = mean(rac, na.rm=T),
    hom = mean(hom, na.rm=T),
    intol = mean(intol, na.rm=T)
    )

table(gss_collapse$stouffer)
table(gss_collapse$rac)
table(gss_collapse$hom)
table(gss_collapse$intol)


plot(gss_collapse$year, gss_collapse$stouffer)
plot(gss_collapse$year, gss_collapse$rac)
plot(gss_collapse$year, gss_collapse$hom)
plot(gss_collapse$year, gss_collapse$intol)