library(car)
library(stargazer)
library(survey)
library(sjPlot)
library(ggplot2)
library(pander)
library(knitr)
library(tidycensus)
library(dplyr)
library(lme4)
library(arm)
library(haven)
library(censusapi)
library(tidyverse)
library(sf)
library(lmerTest)
##Recoding Variables
load(url("https://github.com/coreysparks/data/blob/master/brfss_2017.Rdata?raw=true"))
# Cleaning the variable names for space, underscore & Uppercase Characters
nams<-names(brfss_17)
newnames<-gsub("_", "", nams)
names(brfss_17)<-make.names(tolower(newnames), unique = T)
#Healthy days
brfss_17$healthdays<-Recode(brfss_17$physhlth, recodes = "88=0; 77=NA; 99=NA")
# sex
brfss_17$sex<-as.factor(ifelse(brfss_17$sex==1, "Male", "Female"))
brfss_17$badhealth<-Recode(brfss_17$genhlth, recodes="4:5=1; 1:3=0; else=NA")
#race/ethnicity
brfss_17$black<-Recode(brfss_17$racegr3, recodes="2=1; 9=NA; else=0")
brfss_17$white<-Recode(brfss_17$racegr3, recodes="1=1; 9=NA; else=0")
brfss_17$other<-Recode(brfss_17$racegr3, recodes="3:4=1; 9=NA; else=0")
brfss_17$hispanic<-Recode(brfss_17$racegr3, recodes="5=1; 9=NA; else=0")
brfss_17$race_eth<-Recode(brfss_17$racegr3,
recodes="1='nhwhite'; 2='nh black'; 3='nh other';4='nh multirace'; 5='hispanic'; else=NA",
as.factor = T)
brfss_17$race_eth<-relevel(brfss_17$race_eth, ref = "nhwhite")
#insurance
brfss_17$ins<-Recode(brfss_17$hlthpln1, recodes ="7:9=NA; 1=1;2=0")
#income grouping
brfss_17$inc<-ifelse(brfss_17$incomg==9, NA, brfss_17$incomg)
#education level
brfss_17$educ<-Recode(brfss_17$educa,
recodes="1:2='0Prim'; 3='1somehs'; 4='2hsgrad'; 5='3somecol'; 6='4colgrad';9=NA",
as.factor=T)
brfss_17$educ<-relevel(brfss_17$educ, ref='2hsgrad')
#employment
brfss_17$employ<-Recode(brfss_17$employ1,
recodes="1:2='Employed'; 2:6='nilf'; 7='retired'; 8='unable'; else=NA",
as.factor=T)
brfss_17$employ<-relevel(brfss_17$employ, ref='Employed')
#marital status
brfss_17$marst<-Recode(brfss_17$marital,
recodes="1='married'; 2='divorced'; 3='widowed'; 4='separated'; 5='nm';6='cohab'; else=NA",
as.factor=T)
brfss_17$marst<-relevel(brfss_17$marst, ref='married')
#Age cut into intervals
brfss_17$agec<-cut(brfss_17$age80, breaks=c(0,24,39,59,79,99))
#BMI, in the brfss_17a the bmi variable has 2 implied decimal places,
#so we must divide by 100 to get real bmi's
brfss_17$bmi<-brfss_17$bmi5/100
#smoking currently
brfss_17$smoke<-Recode(brfss_17$smoker3,
recodes="1:2=1; 3:4=0; else=NA")
brfss_17$skcancer<-Recode(brfss_17$chcscncr, recodes = "1= 1; 2= 0; else=NA")
brfss_17$kidney<-Recode(brfss_17$chckidny, recodes = "1= 1; 2= 0; else=NA")
brfss_17$chrobpuld<-Recode(brfss_17$chccopd1, recodes = "1= 1; 2= 0; else=NA")
brfss_17$arthritis<-Recode(brfss_17$havarth3, recodes = "1= 1; 2= 0; else=NA")
brfss_17$depdisorder<-Recode(brfss_17$addepev2, recodes = "1= 1; 2:4=0; else=NA")
merged<-brfss_17%>%
dplyr::select( depdisorder, mmsa, sex, agec, badhealth, marst, educ, race_eth, employ, badhealth,bmi, mmsaname, mmsawt )%>%
filter(complete.cases(.))
fit.an<-lm(depdisorder~as.factor(mmsa),
merged)
anova(fit.an)
## Analysis of Variance Table
##
## Response: depdisorder
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(mmsa) 135 170 1.26154 7.9205 < 2.2e-16 ***
## Residuals 203961 32486 0.15928
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
fit<-lmer(depdisorder~(1|mmsa),
data=merged)
arm::display(fit,
detail=T)
## lmer(formula = depdisorder ~ (1 | mmsa), data = merged)
## coef.est coef.se t value
## (Intercept) 0.20 0.00 80.31
##
## Error terms:
## Groups Name Std.Dev.
## mmsa (Intercept) 0.03
## Residual 0.40
## ---
## number of obs: 204097, groups: mmsa, 136
## AIC = 204518, DIC = 204491.8
## deviance = 204502.0
performance::icc(fit)
## # Intraclass Correlation Coefficient
##
## Adjusted ICC: 0.005
## Conditional ICC: 0.005
fit2<-lmer(depdisorder~sex+agec+educ+(1|mmsa),
data=merged,
na.action=na.omit)
arm::display(fit2, detail=T)
## lmer(formula = depdisorder ~ sex + agec + educ + (1 | mmsa),
## data = merged, na.action = na.omit)
## coef.est coef.se t value
## (Intercept) 0.27 0.00 59.19
## sexMale -0.10 0.00 -55.92
## agec(24,39] 0.02 0.00 3.77
## agec(39,59] 0.02 0.00 4.63
## agec(59,79] -0.02 0.00 -4.16
## agec(79,99] -0.12 0.00 -24.67
## educ0Prim 0.02 0.01 3.66
## educ1somehs 0.07 0.00 14.17
## educ3somecol 0.01 0.00 4.81
## educ4colgrad -0.04 0.00 -17.81
##
## Error terms:
## Groups Name Std.Dev.
## mmsa (Intercept) 0.03
## Residual 0.39
## ---
## number of obs: 204097, groups: mmsa, 136
## AIC = 199222, DIC = 199000.1
## deviance = 199099.1
performance::icc(fit2)
## # Intraclass Correlation Coefficient
##
## Adjusted ICC: 0.004
## Conditional ICC: 0.004
wadt <- read_csv("C:/Users/chris/University of Texas at San Antonio/SPRING 2021/DEM 7283 STATISTICS FOR DEMOGRAPHIC DATA 2/states.csv")
##
## -- Column specification --------------------------------------------------------
## cols(
## .default = col_character()
## )
## i Use `spec()` for the full column specifications.
anova(fit, fit2, test="Chisq")
## refitting model(s) with ML (instead of REML)
## Data: merged
## Models:
## fit: depdisorder ~ (1 | mmsa)
## fit2: depdisorder ~ sex + agec + educ + (1 | mmsa)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## fit 3 204508 204539 -102251 204502
## fit2 12 199123 199246 -99550 199099 5402.9 9 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1