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

#So what haven't we done yet?
#Missing data - there are about 400 NA(d) in the spkath, for instance
table(na_tag(gss_sub$spkath))

gss_sub <- gss |> select(year, spkath, colath, libath, spkrac, colrac, librac, spkcom, colcom, libcom, spkmil, colmil, libmil, spkhomo, colhomo, libhomo)

#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)

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

#Generate our variables, like before
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$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 = TRUE),
    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)