library(dplyr)
library(lme4)
library(tidyverse)
NCI_merge <- left_join(NCI_17_18, state_disprov, by = "STATENUM")
glimpse(NCI_merge)
NCI.select <- NCI_merge %>%
  select(.,
         STATENUM,
         STATENAM,
         DIS_PROV,
         VOTE15,
         AGE,
         RACE15_AMERIN,
         RACE15_ASIAN,
         RACE15_BLACK,
         RACE15_DK,
         RACE15_HAWAII,
         RACE15_WHITE,
         RACE15_LATINO,
         RACE15_OTHER,
         LGLSTAT15,
         LEVELMR08,
         EXPRESS15,
         MOBILITY08,
         GENDER)
NCI.clean <- NCI.select %>%
  filter(!(LEVELMR08 %in%
             c("5", "6", "98")))%>%
  filter(!(VOTE15 %in%
             c("99")))%>%
  filter(!(EXPRESS15 %in%
             c("99")))%>%
  filter(!(MOBILITY08 %in%
             c("99")))%>%
  filter(!(LGLSTAT15 %in%
             c("99")))%>%
  mutate(., 
         vote.re = case_when(
           VOTE15 == 1 ~ 0,
           VOTE15 == 2 ~ 1,
           VOTE15 == 3 ~ 1
         )) %>%
  mutate(.,
         mobility.re = case_when(
           MOBILITY08 == 1 ~ 0,
           MOBILITY08 == 2 ~ 1,
           MOBILITY08 == 3 ~ 1
         ))  %>%
  mutate(.,
         communication = case_when(
           EXPRESS15 == 1 ~ 0,
           EXPRESS15 == 2 ~ 1,
           EXPRESS15 == 3 ~ 1,
           EXPRESS15 == 4 ~ 1,
           EXPRESS15 == 5 ~ 1
         )) %>%
  mutate (.,
          guardian = case_when(
            LGLSTAT15 == 1 ~ 0,
            LGLSTAT15 == 2 ~ 1,
            LGLSTAT15 == 3 ~ 1,
            LGLSTAT15 == 4 ~ 1
          )) %>%
  mutate(.,
         white = case_when(
           RACE15_WHITE == 1 ~ 0,
           RACE15_WHITE == 2 ~ 1
         ))  %>%
  mutate(.,
         black = case_when(
           RACE15_BLACK == 1 ~ 0,
           RACE15_BLACK == 2 ~ 1
         ))  %>%
 mutate(.,
         latino = case_when(
           RACE15_LATINO == 1 ~ 0,
           RACE15_LATINO == 2 ~ 1
         )) %>%
   mutate(.,
         other = case_when(
           RACE15_AMERIN == 1 ~ 0,
           RACE15_AMERIN == 2 ~ 1,
           RACE15_ASIAN == 1 ~ 0,
           RACE15_ASIAN == 2 ~ 1,
           RACE15_HAWAII == 1 ~ 0,
           RACE15_HAWAII == 2 ~ 1,
           RACE15_DK == 1 ~ 0,
           RACE15_DK == 2 ~ 1,
           RACE15_OTHER == 1 ~ 0,
           RACE15_OTHER == 2 ~ 1
         )) %>%
  mutate(.,
         gender.re = case_when(
           GENDER == 1 ~ 0,
           GENDER == 2 ~ 1,
           GENDER == 3 ~ 2
         ))
           
age.cat <- rep(NA,19862)
age.cat[NCI.clean$AGE <= 29] <- 1
age.cat[NCI.clean$AGE >= 30 & NCI.clean$AGE <=44] <- 2
age.cat[NCI.clean$AGE >=45 & NCI.clean$AGE <=59] <- 3
age.cat[NCI.clean$AGE >=60] <- 4
NCI.clean$age.cat <- as.factor(age.cat)
table(NCI.clean$white)
table(NCI.clean$STATENAM)
table(NCI.clean$gender.re)
table(NCI.clean$age.cat)
table(NCI.clean$guardian)
table(NCI.clean$LEVELMR08)
table(NCI.clean$communication)
table(NCI.clean$mobility.re)
table(NCI.clean$DIS_PROV)
table(NCI.clean$black)
table(NCI.clean$latino)
table(NCI.clean$other)
table(NCI.clean$vote.re)
model.null <- glmer(vote.re ~ (1|STATENUM), family = binomial, data = NCI.clean)
summary(model.null)
ICC.null <- 0.3244 / (0.3244 + (pi^2/3))
ICC.null
model.2 <- glmer(vote.re ~ age.cat + LEVELMR08 + communication + mobility.re + GENDER + other + black +  white + latino + guardian+ (1|STATENUM), family = binomial, data = NCI.clean)
summary(model.2)
model.3 <- glmer(vote.re ~ age.cat + LEVELMR08 + communication + mobility.re + GENDER + other + black +  white + latino + guardian + DIS_PROV + (1|STATENUM), family = binomial, data = NCI.clean)
summary(model.3)