library(car)
## Warning: package 'car' was built under R version 3.6.2
## Loading required package: carData
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
library(survey)
## Warning: package 'survey' was built under R version 3.6.2
## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
## Warning: package 'survival' was built under R version 3.6.2
## 
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
## 
##     dotchart
library(questionr)
## Warning: package 'questionr' was built under R version 3.6.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.2
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tableone)
## Warning: package 'tableone' was built under R version 3.6.2
load("C:/Users/canda/Documents/Statistics II/brfss_2017.Rdata")
nams<-names(brfss_17)
head(nams,n=20)
##  [1] "dispcode" "statere1" "safetime" "hhadult"  "genhlth"  "physhlth"
##  [7] "menthlth" "poorhlth" "hlthpln1" "persdoc2" "medcost"  "checkup1"
## [13] "bphigh4"  "bpmeds"   "cholchk1" "toldhi2"  "cholmed1" "cvdinfr4"
## [19] "cvdcrhd4" "cvdstrk3"

##Recode binary outcome variable.

###Variable Cholchk1 means “How long since cholesterol checked” From never to 5 or more years ago. Recode to never = 0, answer of yes and when = 1.

brfss_17$cholyes<-Recode(brfss_17$cholchk1, recodes = "7:9=NA; 2:5=1; 1=0")

##Research question

###Are people who are in good health more likely to say they never had cholesterol checked?

##Define 2 predictor variables - checkup1 asks “About how long has it been since you last visited a doctor for a routine checkup?” and genhlth asks “Would you say that in general your health is: excelent, very good, good, fair, poor?”

brfss_17$checkup1<-Recode(brfss_17$checkup1, recodes = "1='1yr';  2='2yrs'; 3='5yrs'; 4='5yrs or more'; 7:9=NA", as.factor=T)
brfss_17$checkup1<-relevel(brfss_17$checkup1, ref='1yr')


brfss_17$genhlth<-Recode(brfss_17$genhlth, recodes = "1='excellent'; 2='very good'; 3='good'; 4='fair'; 5='poor'; 7:9=NA", as.factor=T)
brfss_17$genhlth<-relevel(brfss_17$genhlth, ref='excellent')
table(brfss_17$cholyes, brfss_17$genhlth)
##    
##     excellent  fair  good  poor very good
##   0      2798  1311  3962   326      4080
##   1     35894 27124 64246  9958     69432
table(brfss_17$cholyes, brfss_17$checkup1)
##    
##        1yr   2yrs   5yrs 5yrs or more
##   0   5036   1850   1806         2808
##   1 160259  23136  12080         9167
prop.table(table(brfss_17$cholyes, brfss_17$genhlth), margin = 2)
##    
##      excellent       fair       good       poor  very good
##   0 0.07231469 0.04610515 0.05808703 0.03169973 0.05550114
##   1 0.92768531 0.95389485 0.94191297 0.96830027 0.94449886
prop.table(table(brfss_17$cholyes, brfss_17$checkup1), margin = 2)
##    
##            1yr       2yrs       5yrs 5yrs or more
##   0 0.03046674 0.07404146 0.13005905   0.23448852
##   1 0.96953326 0.92595854 0.86994095   0.76551148
chisq.test(table(brfss_17$cholyes, brfss_17$genhlth))
## 
##  Pearson's Chi-squared test
## 
## data:  table(brfss_17$cholyes, brfss_17$genhlth)
## X-squared = 359.01, df = 4, p-value < 2.2e-16
chisq.test(table(brfss_17$cholyes, brfss_17$checkup1))
## 
##  Pearson's Chi-squared test
## 
## data:  table(brfss_17$cholyes, brfss_17$checkup1)
## X-squared = 11352, df = 3, p-value < 2.2e-16
brfss_17$tx<-NA
brfss_17$tx[grep(pattern = "TX", brfss_17$mmsaname)]<-1
brfss_17<-brfss_17%>%
  filter(tx==1, is.na(mmsawt)==F)
options(survey.lonely.psu = "adjust")
des<-svydesign(ids=~1, strata = ~ststr, weights = ~mmsawt, data = brfss_17)
cat<-wtd.table(brfss_17$cholyes, brfss_17$genhlth, weights = brfss_17$mmsawt)
prop.table(wtd.table(brfss_17$cholyes, brfss_17$genhlth, weights = brfss_17$mmsawt), margin = 2)
##    excellent       fair       good       poor  very good
## 0 0.09209105 0.09380954 0.12541474 0.09168181 0.07896195
## 1 0.90790895 0.90619046 0.87458526 0.90831819 0.92103805
cat<-wtd.table(brfss_17$cholyes, brfss_17$checkup1, weights = brfss_17$mmsawt)
prop.table(wtd.table(brfss_17$cholyes, brfss_17$checkup1, weights = brfss_17$mmsawt), margin = 2)
##          1yr       2yrs       5yrs 5yrs or more
## 0 0.05029818 0.09743705 0.17203439   0.28920182
## 1 0.94970182 0.90256295 0.82796561   0.71079818
n<-table(is.na(brfss_17$cholyes)==F)
n
## 
## FALSE  TRUE 
##   356  8277
p<-prop.table(wtd.table(brfss_17$cholyes, brfss_17$genhlth, weights = brfss_17$mmsawt), margin = 2)
se<-(p*(1-p))/n[2]
p<-prop.table(wtd.table(brfss_17$cholyes, brfss_17$checkup1, weights = brfss_17$mmsawt), margin = 2)
se<-(p*(1-p))/n[2]

stargazer::stargazer(data.frame(proportion=p, se=sqrt(se)), summary = F, type = "text", digits = 2)
## 
## ==============================================================================
##   proportion.Var1 proportion.Var2 proportion.Freq se.Var1   se.Var2    se.Freq
## ------------------------------------------------------------------------------
## 1        0              1yr            0.05          0        1yr       0.002 
## 2        1              1yr            0.95          1        1yr       0.002 
## 3        0             2yrs            0.10          0        2yrs      0.003 
## 4        1             2yrs            0.90          1        2yrs      0.003 
## 5        0             5yrs            0.17          0        5yrs      0.004 
## 6        1             5yrs            0.83          1        5yrs      0.004 
## 7        0         5yrs or more        0.29          0    5yrs or more  0.005 
## 8        1         5yrs or more        0.71          1    5yrs or more  0.005 
## ------------------------------------------------------------------------------
cat<-svytable(~cholyes+genhlth, design = des)
stargazer(data.frame(prop.table(svytable(~cholyes+genhlth, design = des), margin = 2)), summary = F, type = "text", digits=3)
## 
## ==========================
##    cholyes  genhlth  Freq 
## --------------------------
## 1     0    excellent 0.092
## 2     1    excellent 0.908
## 3     0      fair    0.094
## 4     1      fair    0.906
## 5     0      good    0.125
## 6     1      good    0.875
## 7     0      poor    0.092
## 8     1      poor    0.908
## 9     0    very good 0.079
## 10    1    very good 0.921
## --------------------------
cat<-svytable(~cholyes+checkup1, design = des)
stargazer(data.frame(prop.table(svytable(~cholyes+checkup1, design = des), margin = 2)), summary = F, type = "text", digits=3)
## 
## ============================
##   cholyes   checkup1   Freq 
## ----------------------------
## 1    0        1yr      0.050
## 2    1        1yr      0.950
## 3    0        2yrs     0.097
## 4    1        2yrs     0.903
## 5    0        5yrs     0.172
## 6    1        5yrs     0.828
## 7    0    5yrs or more 0.289
## 8    1    5yrs or more 0.711
## ----------------------------
sv.table<-svyby(formula = ~cholyes, by = ~genhlth, design = des, FUN = svymean, na.rm=T)
stargazer(sv.table, summary = F, type = "text", digits = 2)
## 
## ================================
##            genhlth  cholyes  se 
## --------------------------------
## excellent excellent  0.91   0.02
## fair        fair     0.91   0.02
## good        good     0.87   0.01
## poor        poor     0.91   0.03
## very good very good  0.92   0.01
## --------------------------------
sv.table<-svyby(formula = ~cholyes, by = ~checkup1, design = des, FUN = svymean, na.rm=T)
stargazer(sv.table, summary = F, type = "text", digits = 2)
## 
## ======================================
##                checkup1   cholyes  se 
## --------------------------------------
## 1yr              1yr       0.95   0.01
## 2yrs             2yrs      0.90   0.02
## 5yrs             5yrs      0.83   0.03
## 5yrs or more 5yrs or more  0.71   0.04
## --------------------------------------

There are substantive differences in the descriptive results between the analysis using survey design and that not using survey design.