library(car)
library(stargazer)
library(questionr)
library(dplyr)

#Recode Mental-Outcome Variable

brfss_17$mental<-Recode(brfss_17$menthlth, recodes="1:30=1; 88=0; else=NA")
```{r}
table(brfss_17$mental)
table(brfss_17$menthlth)

#b Research Question Research Question: The outcome variable that I will be using is mental health. I examine the relationship between gender and level of education with mental health. Do males who are less educated have a greater chance of having mental health issues? #c.ย The predictor variables that will be used are gender, and level of education.

#Recode Education

brfss_17$educa2<-Recode(brfss_17$educa, recodes="1:2='OPrime'; 3='1somehs'; 4='2hsgrad'; 5='3somecol'; 6='4colgrad'; 9=NA", as.factor=T)
brfss_17$educa2<-relevel(brfss_17$educa2, ref='2hsgrad')
table(brfss_17$educa)
table(brfss_17$educa2)

Recode sex

brfss_17$male<-as.factor(ifelse(brfss_17$sex==1, "Male", "Female"))
table(brfss_17$male)
table(brfss_17$sex)

#above numbers are off

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

#Descriptive Statistics

table(brfss_17$mental, brfss_17$educa2)
prop.table(table(brfss_17$mental, brfss_17$educa2), margin=2)
chisq.test(table(brfss_17$mental, brfss_17$educa2))

#There is no correlation since the pvalue is low.

options(survey.lonely.psu = "adjust")
des<-svydesign(ids=~1, strata =~ststr, weights =~mmsawt, data = brfss_17)

#Column percentages of level of mental health by level of education, adjusted for weight.

panera<-wtd.table(brfss_17$mental, brfss_17$educa2, weights = brfss_17$mmsawt)
prop.table(wtd.table(brfss_17$mental, brfss_17$educa2, weights=brfss_17$mmsawt), margin=2)

#There are differences between the weighted and non weighted percentages. #Standard Errors of Percentages

prop.table(table(brfss_17$mental, brfss_17$educa2), margin = 2)
n<-table(is.na(brfss_17$mental)==F)
p<-prop.table(wtd.table(brfss_17$mental, brfss_17$educa2, weights = brfss_17$mmsawt), margin = 2)
se<-(p*(1-p))/n[2]
stargazer(data.frame(proportion=p, se=sqrt(se)), summary = F, type="html", digits=2)

Proper survey design analysis

panera1<-svytable(~mental+educa2, design=des)
prop.table(svytable(~mental+educa2, design=des), margin = 2)
stargazer(data.frame(prop.table(svytable(~mental+educa2, design=des), margin = 2)), summary=F, type = "html", digits = 3)
panera<-wtd.table(brfss_17$mental, brfss_17$educa2, weights = brfss_17$mmsawt)
prop.table(wtd.table(brfss_17$mental, brfss_17$educa2, weights=brfss_17$mmsawt), margin=2)
```{r}
panera2<-(~mental+educa2, design = des)
prop.table(svytable(~mental+educa2, design = des), margin = 2)
```{r}
panera2<-(~mental+educa2, design = des)
prop.table(svytable(~mental+educa2, design = des), margin = 2)
reg2<-lm(mental~educa2+male, data=brfss_17, weights = brfss_17$mmsawt)
summary(reg2)
reg1<-lm(mental ~ educa2 + male, data = brfss_17)
summary(reg1)

#Conclusion We can conclude after completing the analysis that the only statistical significance is found between level of education, gender with mental health. The direction of significance remains the same between regressions.