library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(magrittr)
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
library(foreign)
#download datasets - AddHealth
wave1<-read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0001/da27021p1.dta")
wave3<-read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0003/da27021p3.dta")
wave4<-read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0012/da27021p12.dta")
wave4a<-read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0013/da27021p13.dta")
wave5<-read.xport("/projects/add_health/data/wave_5/wave5.xpt")
wave5age<-read.xport("/projects/add_health/data/wave_5/Constructed Files/Wave V Constructed Age/agew5.xpt")
#wave3section19<-read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0006/da27021p6.dta")
#download biomarkers Wave 4
w4glu <- read.xport("/projects/add_health/data/wave_4/biomarkers/glu_a1c.xpt")
w4crp <- read.xport("/projects/add_health/data/wave_4/biomarkers/crp_ebv.xpt")
w4lipids <- read.xport("/projects/add_health/data/wave_4/biomarkers/lipids.xpt")
#weightW4 <- read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0028/da27021p28.dta")
#weightW4<-read.xport("/projects/add_health/data/wave_4/Wave IV Grand Sample Weights/weights4/weights4.xpt")
#download biomarkers wave 5
w5brenal<-read.xport("/projects/add_health/data/wave_5/brenal5.xpt")
w5blipids<-read.xport("/projects/add_health/data/wave_5/blipids5.xpt")
w5bcardio<-read.xport("/projects/add_health/data/wave_5/bcardio5.xpt")
w5bglua1c<-read.xport("/projects/add_health/data/wave_5/bglua1c5.xpt")
w5banthro<-read.xport("/projects/add_health/data/wave_5/banthro5.xpt")
w5bcrp<-read.xport("/projects/add_health/data/wave_5/bcrp5.xpt")
w5bdemo<-read.xport("/projects/add_health/data/wave_5/bdemo5.xpt")
w5bweight<-read.xport("/projects/add_health/data/wave_5/bweight5.xpt")
#w5bweight=read.dta("/projects/add_health/data/wave_5/wave5weight.dta")
friendship <- read.dta("/projects/add_health/data/11613344/ICPSR_27033/DS0001/schoolnetworkvariables.dta")
w1school<-read.dta("/projects/add_health/data/11613344/ICPSR_27021/DS0019/da27021p19.dta")
w1school$s11<-w1school$S11
w1school$s17<-w1school$S17
w1school$s25<-w1school$S25
w1school$s26<-w1school$S26
w1school$s44<-w1school$S44
w1school$s46a<-w1school$S46A
w1school$s46d<-w1school$S46D

w1school2<-w1school%>%
  dplyr::select(aid,s11,s17,s25,s26,s44,s46a,s46d)
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
names(wave5)[1] <- "aid"
names(wave5age)[1] <- "aid"
names(w4glu)[1] <- "aid"
names(w4crp)[1] <- "aid"
names(w4lipids)[1] <- "aid"
#names(weightW4)[1] <- "aid"

names(w5brenal)[1] <- "aid"
names(w5blipids)[1] <- "aid"
names(w5bcardio)[1] <- "aid"
names(w5bglua1c)[1] <- "aid"
names(w5banthro)[1] <- "aid"
names(w5bcrp)[1] <- "aid"
names(w5bdemo)[1] <- "aid"
names(w5bweight)[1] <- "aid"
#change uppercase to lower case
#wave5$aid<-wave5$AID
#wave5age$aid<-wave5age$AID
#w4glu$aid<-w4glu$AID
#w4crp$aid<-w4crp$AID
#w4lipids$aid<-w4lipids$AID
#remove variables
#wave5 = subset(wave5, select = -c(AID) )
#w4glu = subset(w4glu, select = -c(AID) )
#w4crp = subset(w4crp, select = -c(AID) )
#w4lipids = subset(w4lipids, select = -c(AID) )
#merge datasets
a<-merge(wave1,wave3,by="aid")
bb<-merge(a,wave4,by="aid")
b<-merge(bb,wave4a,by="aid")
c<-merge(b,wave5,by="aid")
d<-merge(c,w4glu,by="aid")
e<-merge(d,w4crp,by="aid")
f<-merge(e,w4lipids,by="aid")
addhealtha<-merge(f,w5bweight,by="aid")
addhealthb<-merge(addhealtha,wave5age,by="aid")
addhealthc<-merge(addhealthb,w5brenal,by="aid")
addhealthd<-merge(addhealthc,w5blipids,by="aid")
addhealthe<-merge(addhealthd,w5bcardio,by="aid")
addhealthf<-merge(addhealthe,w5bglua1c,by="aid")
addhealthg<-merge(addhealthf,w5banthro,by="aid")
addhealthh<-merge(addhealthg,w5bcrp,by="aid")
addhealthi<-merge(addhealthh,w5bdemo,by="aid")
addhealthj<-merge(addhealthi,friendship,by="aid")
addhealth<-merge(addhealthj,w1school2,by="aid")
addhealth$livesMother<-Recode(addhealth$s11,recodes="0=0;1=1;else=NA")
addhealth$livesFather<-Recode(addhealth$s17,recodes="0=0;1=1;else=NA")

addhealth$livesParent<-ifelse(addhealth$livesMother==1&addhealth$livesFather==1,"lives_2_parents",
                             ifelse(addhealth$livesMother==1&addhealth$livesFather==0,"lives_mom",
                                    ifelse(addhealth$livesMother==0&addhealth$livesFather==1,"lives_dad",
                                           ifelse(addhealth$livesMother==0&addhealth$livesFather==0,"lives_other",NA))))

addhealth$livesParent2<-ifelse(addhealth$livesMother==1&addhealth$livesFather==1,"lives_2_parents",
                             ifelse(addhealth$livesMother==1&addhealth$livesFather==0,"lives_1_parent",
                                    ifelse(addhealth$livesMother==0&addhealth$livesFather==1,"lives_1_parent",
                                           ifelse(addhealth$livesMother==0&addhealth$livesFather==0,"lives_other",NA))))

addhealth$adopt<-ifelse(addhealth$s25==0,"no",
                       ifelse(addhealth$s25==1&addhealth$s26==0,"adoptNONbioparent",
                              ifelse(addhealth$s25==1&addhealth$s26==1,"adoptbioparent",NA)))
addhealth$schoolClubJoin<-addhealth$s44
addhealth$teacherTrouble<-Recode(addhealth$s46a,recodes="0:1=0;2:4=1;else=NA")
addhealth$studentTrouble<-Recode(addhealth$s46d,recodes="0:1=0;2:4=1;else=NA")
table(addhealth$schoolClubJoin)
## 
##    0    1 
## 4980  907
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
#age wave 4
#birth month wave 4
#addhealth$birthmonthw4<-Recode(addhealth$H4OD1M,recodes="1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;10=10;11=11;12=12")
#birth year wave 4
#addhealth$birthyearw4<-addhealth$H4OD1Y
#addhealth$birthyearw4 <- Recode(addhealth$H4OD1Y,recodes="1974=1974;1975=1975;1976=1976;1977=1977;1978=1978;1979=1979;1980=1980;1981=1981;1982=1982;1983=1983")
#combine year and month for birth date wave 4
#addhealth$birthdatew4 <- as.yearmon(paste(addhealth$birthyearw4, addhealth$birthmonthw4), "%Y %m")
#interview date w4
#addhealth$interviewmonthw4 <- Recode(addhealth$IMONTH4,recodes="1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;10=10;11=11;12=12")
#addhealth$interviewyearw4 <- Recode(addhealth$IYEAR4,recodes="2007=2007;2008=2008;2009=2009")
#addhealth$interviewdatew4<-as.yearmon(paste(addhealth$interviewyearw4,addhealth$interviewmonthw4),"%Y %m")
#age - derived from date of birth subtracted from wave interview date 
#addhealth$ageW4<-(as.numeric(round(addhealth$interviewdatew4 - addhealth$birthdatew4)))
#table(addhealth$ageW4)
#Date of birth variables; Wave 1: H1GI1M (month), H1GI1Y (year); Wave 3: H3OD1M (month), H3OD1Y (year); Wave 4: H4OD1M (month), H4OD1Y (year)
addhealth$birthmonthw4 <- Recode(addhealth$H4OD1M,recodes="1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;10=10;11=11;12=12")
addhealth$birthyearw1 <- factor(ifelse(addhealth$H1GI1Y=="74",1974,
                                             ifelse(addhealth$H1GI1Y=="75",1975,
                                                           ifelse(addhealth$H1GI1Y=="76",1976,
                                                                         ifelse(addhealth$H1GI1Y=="77",1977,
                                                                                       ifelse(addhealth$H1GI1Y=="78",1978,
                                                                                                     ifelse(addhealth$H1GI1Y=="79",1979,
                                                                                                                   ifelse(addhealth$H1GI1Y=="80",1980,
                                                                                                                                ifelse(addhealth$H1GI1Y=="81",1981,
                                                                                                                                               ifelse(addhealth$H1GI1Y=="82",1982,
                                                                                                                                                             ifelse(addhealth$H1GI1Y=="83",1983,1979)))))))))))
addhealth$birthmonthw1 <- Recode(addhealth$H1GI1M,recodes="1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;10=10;11=11;12=12;else=NA")
addhealth$birthyearw4<-addhealth$H4OD1Y
addhealth$birthyearw4 <- Recode(addhealth$H4OD1Y,recodes="1974=1974;1975=1975;1976=1976;1977=1977;1978=1978;1979=1979;1980=1980;1981=1981;1982=1982;1983=1983")
addhealth$interviewmonthw4 <- Recode(addhealth$IMONTH4,recodes="1=1;2=2;3=3;4=4;5=5;6=6;7=7;8=8;9=9;10=10;11=11;12=12")
table(addhealth$interviewmonthw4)
## 
##    1    2    3    4    5    6    7    8    9   10   11   12 
##  277 1056 1141 1147  870  450  332  241  147   98   81   47
addhealth$interviewyearw4 <- Recode(addhealth$IYEAR4,recodes="2007=2007;2008=2008;2009=2009")
addhealth$iyearfix <- Recode(addhealth$iyear,recodes="'94'='1994';'95'='1995'")
addhealth$birthdatew4 <- as.yearmon(paste(addhealth$birthyearw4, addhealth$birthmonthw4), "%Y %m")
addhealth$interviewdatew4<-as.yearmon(paste(addhealth$interviewyearw4,addhealth$interviewmonthw4),"%Y %m")
addhealth$birthdatew3<-as.yearmon(paste(addhealth$H3OD1Y,addhealth$H3OD1M),"%Y %m")
addhealth$interviewdatew3<-as.yearmon(paste(addhealth$IYEAR3,addhealth$IMONTH3 ),"%Y %m")
addhealth$birthdatew1 <- as.yearmon(paste(addhealth$birthyearw1, addhealth$birthmonthw1), "%Y %m")
addhealth$interviewdatew1 <- as.yearmon(paste(addhealth$iyearfix, addhealth$imonth), "%Y %m")

addhealth$ageW4<-(as.numeric(round(addhealth$interviewdatew4 - addhealth$birthdatew4)))
addhealth$ageW3<-(as.numeric(round(addhealth$interviewdatew3 - addhealth$birthdatew3)))
addhealth$ageW1<-(as.numeric(round(addhealth$interviewdatew1 - addhealth$birthdatew1)))
addhealth$ageW5<-addhealth$AGE
#sex assigned at birth
addhealth$sex<-Recode(addhealth$H5OD2A,recodes="1='Male';2='Female';else=NA")
addhealth$sexW5<-factor(addhealth$sex,levels=c("Male","Female"))

#Sexual Orientation
# Wave 3
addhealth$sxorW3<-Recode(addhealth$H3SE13, recodes="1:2='Straight'; 3='Bisexual';4:5='Gay';else=NA")
addhealth$sexualOrientationW3 <- factor(addhealth$sxorW3, levels = c("Straight", "Gay", "Bisexual"))
# Wave 4
addhealth$sxorW4<-Recode(addhealth$H4SE31, recodes="1:2='Straight'; 3='Bisexual';4:5='Gay';else=NA")
addhealth$sexualOrientationW4 <- factor(addhealth$sxorW4, levels = c("Straight", "Gay", "Bisexual"))
# Wave 5
addhealth$sxorW5<-Recode(addhealth$H5SE15,recodes="1:2='Straight';3='Bisexual';4:5='Gay';else=NA")
addhealth$sexualOrientationW5 <- factor(addhealth$sxorW5, levels = c("Straight", "Gay", "Bisexual"))
#education wave 5
addhealth$educW5<-Recode(addhealth$H5OD11,recodes="1:2=0;3:9=1;10:16=2;else=NA")

addhealth$educW5<-as.factor(addhealth$educW5)
#employment status wave 1
#addhealth$H1EE3[addhealth$H1EE3 == 6] <- NA
#addhealth$H1EE3[addhealth$H1EE3 == 8] <- NA
#addhealth$employmentW1<-addhealth$H1EE3
#employment status wave 3
#addhealth$H3LM1[addhealth$H3LM1 == 6] <- NA
#addhealth$H3LM1[addhealth$H3LM1 == 8] <- NA
#addhealth$H3LM2[addhealth$H3LM2 == 6] <- NA
#addhealth$H3LM2[addhealth$H3LM2 == 7] <- NA
#addhealth$H3LM2[addhealth$H3LM2 == 8] <- NA
#addhealth$H3LM2[addhealth$H3LM2 == 9] <- NA
#addhealth$H3LM7[addhealth$H3LM7 == 7] <- NA
#addhealth$employmentW3<-ifelse(addhealth$H3LM1<2&addhealth$H3LM7>0,1,
#                               ifelse(addhealth$H3LM1<2&addhealth$H3LM7<1,0,
#                                      ifelse(addhealth$H3LM2<2&addhealth$H3LM7>0,1,
#                                             ifelse(addhealth$H3LM2<2&addhealth$H3LM7<1,0,NA))))
#employment status wave 4
#addhealth$H4LM1[addhealth$H4LM1 == 6] <- NA
#addhealth$H4LM2[addhealth$H4LM2 == 6] <- NA
#addhealth$H4LM2[addhealth$H4LM2 == 7] <- NA

#addhealth$employmentW4<-ifelse(addhealth$H4LM1<2&addhealth$H4LM14<11,0,
#                                ifelse(addhealth$H4LM2<2&addhealth$H4LM14<11,0,1))

#employment status Wave 5
#addhealth$employmentW5<-ifelse(addhealth$H5LM5==1,1,0)

#addhealth$employmentW1<-as.factor(addhealth$employmentW1)
#addhealth$employmentW3<-as.factor(addhealth$employmentW3)
#addhealth$employmentW4<-as.factor(addhealth$employmentW4)
#addhealth$employmentW5<-as.factor(addhealth$employmentW5)
#marital status Wave 1
addhealth$H1GI15[addhealth$H1GI15 == 6] <- NA
addhealth$H1GI15[addhealth$H1GI15 == 7] <- NA
addhealth$H1GI15[addhealth$H1GI15 == 8] <- NA

addhealth$maritalW1<-ifelse(addhealth$ageW1<15,0,
                             ifelse(addhealth$ageW1>14&addhealth$H1GI15<1,0,
                                    ifelse(addhealth$ageW1>14&addhealth$H1GI15>0,1,NA)))

#marital status Wave 3 NOTE 0=never married, 1=married, 2=widow, 3=divorce
addhealth$H3MR1[addhealth$H3MR1 == 6] <- NA
addhealth$H3MR1[addhealth$H3MR1 == 8] <- NA
addhealth$H3MR1[addhealth$H3MR1 == 9] <- NA
addhealth$H3MR3_A[addhealth$H3MR3_A == 7] <- NA
addhealth$H3MR3_A[addhealth$H3MR3_A == 8] <- NA
addhealth$maritalW3<-ifelse(addhealth$H3MR1==0,0,
                            ifelse(addhealth$H3MR1>0&addhealth$H3MR3_A==1,1,2))
#marital status wave 4
addhealth$H4TR1[addhealth$H4TR1 == 6] <- NA
addhealth$H4TR1[addhealth$H4TR1 == 8] <- NA
addhealth$maritalW4<-ifelse(addhealth$H4TR1==0,0,
                            ifelse(addhealth$H4TR1>0&addhealth$H4TR13==1,1,
                                   ifelse(addhealth$H4TR1>0&addhealth$H4TR13!=1,2,NA)))
#marital status wave 5 
addhealth$maritalW5<-Recode(addhealth$H5HR1,recodes="1=1;2:4=2;5=0;else=NA")


#addhealth$maritalW1<-as.factor(addhealth$maritalW1)
#addhealth$maritalW3<-as.factor(addhealth$maritalW3)
#addhealth$maritalW4<-as.factor(addhealth$maritalW4)
#addhealth$maritalW5<-as.factor(addhealth$maritalW5)
#table(addhealth$maritalW5)
#combine race to one variable.  This is for white, black, hispanic, asian, native american, pacific islander, and other.
addhealth$raceW5all <- factor(ifelse(addhealth$H5OD4A==1,"a_white", 
                                            ifelse(addhealth$H5OD4B==1, "b_black", 
                                            ifelse(addhealth$H5OD4C==1, "c_hispanic",
                                            ifelse(addhealth$H5OD4D==1,"d_asian",
                                            ifelse(addhealth$H5OD4F==1,"e_nativeamer",
                                            ifelse(addhealth$H5OD4E==1,"f_pacificisl",
                                            ifelse(addhealth$H5OD4G==1,"g_other",NA))))))))
#combine race to one variable.  This is for white, black, hispanic, asian, native american, pacific islander, and other.
addhealth$raceW5min <- factor(ifelse(addhealth$H5OD4A==1,"a_white", 
                                            ifelse(addhealth$H5OD4B==1, "b_black", 
                                            ifelse(addhealth$H5OD4C==1, "c_hispanic",NA))))
library(StatMeasures)
#biomarkers wave 5
#1 systolic bp
addhealth$H5SBP<-Recode(addhealth$H5SBP,recodes="9999=NA;9997=NA;9996=NA")
addhealth$systolicbpW5<-ifelse(addhealth$H5SBP>=140,1,0)
#2 diastolic bp
addhealth$H5DBP<-Recode(addhealth$H5DBP,recodes="9999=NA;9997=NA;9996=NA")
addhealth$diastolicbpW5<-ifelse(addhealth$H5DBP>=90,1,0)
#3 pulse rate
addhealth$H5PR<-Recode(addhealth$H5PR,recodes="9999=NA;9997=NA;9996=NA")
addhealth$pulserateW5<-ifelse(addhealth$H5PR>=85,1,0)
#4 pulse pressure
addhealth$H5PP<-Recode(addhealth$H5PP,recodes="9999=NA;9997=NA;9996=NA")
addhealth$pulsepressureW5<-ifelse(addhealth$H5PP>=60,1,0)
#5 TOTAL CHOLESTEROL W5
addhealth$tcW5<-decile(addhealth$H5TC)
addhealth$totalcholW5<-ifelse(addhealth$tcW5>=8,1,0)
table(addhealth$totalcholW5)
## 
##    0    1 
## 3741 1519
#6 HDL, HIGH-DENSITY LIPOPROTEIN CHOLESTEROL
addhealth$H5HDL<-Recode(addhealth$H5HDL,recodes="9999=NA")
addhealth$hdlW5<-decile(addhealth$H5HDL)
addhealth$hidensity_lipW5<-ifelse(addhealth$hdlW5<=2,1,0)
#7 TRIGLYCERIDES
addhealth$TG<-decile(addhealth$H5TG)
addhealth$triglycerideW5<-ifelse(addhealth$TG>=8,1,0)
#8 HEMOGLOBIN
addhealth$hemoglobinW5<-ifelse(addhealth$H5HBA1C>=6.4,1,0)
table(addhealth$hemoglobinW5)
## 
##    0    1 
## 4901  246
#9 glucose
addhealth$GLUCOSE<-Recode(addhealth$H5GLUCOS,recodes="9999=NA")
addhealth$glucoseW5<-ifelse(addhealth$GLUCOSE>=200,1,0)
table(addhealth$glucoseW5)
## 
##    0    1 
## 5123  132
#10 BMI
addhealth$H5BMI<-Recode(addhealth$H5BMI,recodes="9999=NA;9996=NA;9997=NA;9994=NA")
addhealth$bmiW5<-ifelse(addhealth$H5BMI>=30,1,0)
table(addhealth$bmiW5)
## 
##    0    1 
## 2982 2780
#11 waist circumference 
addhealth$waistW5<-Recode(addhealth$H5WSTCLS,recodes="1=0;2=1")
#12 high-sensitivity C-reactive protein test. 
addhealth$hsCRPW5<-ifelse(addhealth$H5CRP>=3,1,0)
table(addhealth$hsCRPW5)
## 
##    0    1 
## 3124 1938
# COMBINE BIOMARKERS
addhealth$scoreW5<-addhealth$systolicbpW5+addhealth$diastolicbpW5+addhealth$pulserateW5+addhealth$pulsepressureW5+addhealth$totalcholW5+addhealth$hidensity_lipW5+addhealth$triglycerideW5+addhealth$hemoglobinW5+addhealth$glucoseW5+addhealth$bmiW5+addhealth$waistW5+addhealth$hsCRPW5
table(addhealth$scoreW5)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
## 784 687 761 788 608 418 337 169  76  32  20
#biomarkers Wave 4
#1 systolic bp
addhealth$H4SBP<-Recode(addhealth$H4SBP,recodes="999=NA;997=NA;996=NA")
addhealth$systolicbp<-ifelse(addhealth$H4SBP>=140,1,0)
#2 diastolic bp
addhealth$H4DBP<-Recode(addhealth$H4DBP,recodes="999=NA;997=NA;996=NA")
addhealth$diastolicbp<-ifelse(addhealth$H4DBP>=90,1,0)
#3 pulse rate
addhealth$H4PR<-Recode(addhealth$H4PR,recodes="999=NA;997=NA;996=NA")
addhealth$pulserate<-ifelse(addhealth$H4PR>=85,1,0)
#4 pulse pressure
addhealth$H4PP<-Recode(addhealth$H4PP,recodes="999=NA;997=NA;996=NA")
addhealth$pulsepressure<-ifelse(addhealth$H4PP>=60,1,0)
#5 TOTAL CHOLESTEROL
addhealth$TC<-Recode(addhealth$TC,recodes="99=NA")
addhealth$totalchol<-ifelse(addhealth$TC>=8,1,0)
#6 HDL, HIGH-DENSITY LIPOPROTEIN CHOLESTEROL
addhealth$HDL<-Recode(addhealth$HDL,recodes="99=NA")
addhealth$hidensity_lip<-ifelse(addhealth$HDL<=2,1,0)

#7 TRIGLYCERIDES
addhealth$TG<-Recode(addhealth$TG,recodes="99=NA")
addhealth$triglyceride<-ifelse(addhealth$TG>=8,1,0)

#8 HEMOGLOBIN
addhealth$HBA1C<-Recode(addhealth$HBA1C,recodes="99=NA")
addhealth$hemoglobin<-ifelse(addhealth$HBA1C>=6.4,1,0)

#9 glucose
addhealth$GLUCOSE<-Recode(addhealth$GLUCOSE,recodes="999=NA")
addhealth$glucose<-ifelse(addhealth$GLUCOSE>=200,1,0)

#10 BMI
addhealth$H4BMI<-Recode(addhealth$H4BMI,recodes="999=NA;888=NA;889=NA;996=NA;997=NA")
addhealth$BMI<-ifelse(addhealth$H4BMI>=30,1,0)

# 11 high-sensitivity C-reactive protein test. 
addhealth$CRPa<-Recode(addhealth$CRP,recodes="999=NA;998=NA")
addhealth$hsCRP<-ifelse(addhealth$CRPa>=3,1,0)

#EPSTEIN BARR
#quantile(addhealth$EBVa, c(.80), na.rm = T)
#addhealth$EBVa<-Recode(addhealth$EBV,recodes="9999=NA;9998=NA")
#addhealth$EpsteinBar<-ifelse(addhealth$EBVa>=243,1,0)

#12 waist circumference 
addhealth$H4WAIST[addhealth$H4WAIST == 996] <- NA
addhealth$H4WAIST[addhealth$H4WAIST == 997] <- NA
addhealth$H4WAIST[addhealth$H4WAIST == 998] <- NA
addhealth$waist<-ifelse(addhealth$H5OD2A==2&addhealth$H4WAIST>88,1,
                        ifelse(addhealth$H5OD2A==2&addhealth$H4WAIST<89,0,
                        ifelse(addhealth$H5OD2A==1&addhealth$H4WAIST>102,1,
                               ifelse(addhealth$H5OD2A==1&addhealth$H4WAIST<103,0,NA))))

#combine biomarkers
addhealth$scoreW4<-addhealth$hsCRP+addhealth$BMI+addhealth$triglyceride+addhealth$hemoglobin+addhealth$hidensity_lip+addhealth$totalchol+addhealth$pulserate+addhealth$systolicbp+addhealth$pulsepressure+addhealth$diastolicbp+addhealth$glucose+addhealth$waist
summary(addhealth$HHincomeW3)
## Length  Class   Mode 
##      0   NULL   NULL
#household income wave 3
#addhealth$HHincomeW3<-Recode(addhealth$H3EC6)
addhealth$HHincomeW3<-ifelse(addhealth$H3EC6>0&addhealth$H3EC6<30000,'a_<30K',
                             ifelse(addhealth$H3EC6>29999&addhealth$H3EC6<50000,'b_30k<50k',
                                    ifelse(addhealth$H3EC6>49999&addhealth$H3EC6<100000,'c_50k>100k',
                                           ifelse(addhealth$H3EC6>99999,'d_>100k',NA))))
addhealth$HHincomeW3<-as.factor(addhealth$HHincomeW3)
#household income wave 5
addhealth$HHincomeW5<- Recode(addhealth$H5EC2,recodes="1:6='a_<30K';7:8='b_30k<50k';9:10='c_50k>100k';11:13='d_>100k';else=NA")

#household income wave 4
addhealth$HHincomeW4<- Recode(addhealth$H4EC1,recodes="1:6='a_<30K';7:8='b_30k<50k';9:10='c_50k>100k';11:12='d_>100k';else=NA")
#percieved closeness mom WAVE 1
addhealth$close.momW1<-Recode(addhealth$H1WP9,recodes="1=1;2=2;3=3;4=4;5=5;else=NA")
#percieved closeness mom WAVE 3
addhealth$close.momW3<-Recode(addhealth$H3WP20,recodes="5=1;4=2;3=3;2=4;1=5;else=NA")
#percieved closeness mom WAVE 4
addhealth$close.momW4<-Recode(addhealth$H4WP24,recodes="1=1;2=2;3=3;4=4;5=5;else=NA")
#percieved closeness mom WAVE 5
addhealth$close.momW5<-Recode(addhealth$H5WP14,recodes="1=1;2=2;3=3;4=4;5=5;else=NA")

#parent SUPPORT/SUPERVISION involvement 
addhealth$curfew<-Recode(addhealth$H1WP1,recodes="0=0;1=1;else=NA")
addhealth$hang<-Recode(addhealth$H1WP2,recodes="0=0;1=1;else=NA")
addhealth$wear<-Recode(addhealth$H1WP3,recodes="0=0;1=1;else=NA")
addhealth$tv<-Recode(addhealth$H1WP4,recodes="0=0;1=1;else=NA")
addhealth$prog<-Recode(addhealth$H1WP5,recodes="0=0;1=1;else=NA")
addhealth$bed<-Recode(addhealth$H1WP6,recodes="0=0;1=1;else=NA")
addhealth$eat<-Recode(addhealth$H1WP7,recodes="0=0;1=1;else=NA")

addhealth$parentSupervise<-addhealth$curfew+addhealth$hang+addhealth$wear+addhealth$tv+addhealth$prog+addhealth$bed+addhealth$eat

#mothers education
addhealth$mom.educ<-Recode(addhealth$H1RM1,recodes="1:3=0;4:7=1;8:9=2;else=NA")
addhealth$mom.educ2<-Recode(addhealth$H1RM1,recodes="1:3=0;4:9=1;else=NA")
#mothers nativity
addhealth$mom.nativity<-Recode(addhealth$H1RM2,recodes="0=0;1=1;else=NA")
#mother employed
addhealth$mom.employed<-Recode(addhealth$H1RM4,recodes="16=0;1:15=1;else=NA")
#mother accecpt public assistance
addhealth$mom.pubass<-Recode(addhealth$H1RM9,recodes="0=0;1=1;else=NA")
addhealth$motherInequity<-addhealth$mom.educ2+addhealth$mom.nativity+addhealth$mom.employed+addhealth$mom.pubass
addhealth$relw1<-Recode(addhealth$H1RE1,recodes="0=0;1:28=1;else=NA")

addhealth$religionW1<-ifelse(addhealth$relw1==0,0,
                              ifelse(addhealth$relw1<2&addhealth$H1RE3==4,0,
                                  ifelse(addhealth$relw1<2&addhealth$H1RE3==3,0,
                                      ifelse(addhealth$relw1<2&addhealth$H1RE3==2,0,
                                          ifelse(addhealth$relw1<2&addhealth$H1RE3==1,1,NA)))))
                                      
                                  

addhealth$religionW3<-Recode(addhealth$H3RE24,recodes="0:4=0;5:6=1;else=NA")                                        

addhealth$religionW4<-Recode(addhealth$H4RE7,recodes="0:3=0;4:5=1;else=NA")                                                
                                                            
addhealth$religionW5<-Recode(addhealth$H5RE2,recodes="1:4=0;5:6=1")

#school prestige friends
addhealth$pre<-addhealth$prxprest
addhealth$schoolSeg<-addhealth$SEG1RCE5
#education level of 0=less high school, 1=highschool, 
#education wave 3
addhealth$educW3<-ifelse(addhealth$H3ED2==1,1,
                         ifelse(addhealth$H3ED3==1,1,
                                ifelse(addhealth$H3ED5==1,2,
                                       ifelse(addhealth$H3ED6==1&addhealth$H3ED5!=1,2,
                                              ifelse(addhealth$H3ED7==1&addhealth$H3ED5!=1,2,
                                                     ifelse(addhealth$H3ED8==1&addhealth$H3ED5!=1,2,
                                                            ifelse(addhealth$H3ED9==1,0,NA)))))))
#education wave 4
addhealth$educW4<-Recode(addhealth$H4ED2,recodes="1:2=0;3:6=1;7:13=2;else=NA")
#education wave 5
addhealth$educW5<-Recode(addhealth$H5OD11,recodes="1:2=0;3:9=1;10:16=2;else=NA")
#education wave 1
addhealth$educW1<-addhealth$H1GI18

addhealth$educW1<-as.factor(addhealth$educW1)
addhealth$educW3<-as.factor(addhealth$educW3)
addhealth$educW4<-as.factor(addhealth$educW4)
addhealth$educW5<-as.factor(addhealth$educW5)
#employment
#employment binary scaled
addhealth$currently.employed<-Recode(addhealth$H5LM5,recodes="1=1;2:3=0;else=NA")
#employer provides insurance, benefits, sick leave
addhealth$H5LM13A<-Recode(addhealth$H5LM13A,recodes="0=0;1=1;else=NA")
addhealth$H5LM13B<-Recode(addhealth$H5LM13B,recodes="0=0;1=1;else=NA")
addhealth$H5LM13C<-Recode(addhealth$H5LM13C,recodes="0=0;1=1;else=NA")
#scale benefits
addhealth$benefitscore<-addhealth$H5LM13A+addhealth$H5LM13B+addhealth$H5LM13C
addhealth$beneScore<-Recode(addhealth$benefitscore,recodes="0=0;1:2=1;3=2")
table(addhealth$benefitscore)
## 
##    0    1    2    3 
##  604  380  351 3406
table(addhealth$beneScore)
## 
##    0    1    2 
##  604  731 3406
addhealth$employment<-ifelse(addhealth$currently.employed==0,"unemployed",
                             ifelse(addhealth$currently.employed==1&addhealth$beneScore==0,"empNoBene",
                                    ifelse(addhealth$currently.employed==1&addhealth$beneScore==1,"empPartBene",
                                           ifelse(addhealth$currently.employed==1&addhealth$beneScore==2,"empFullBene",NA))))
table(addhealth$currently.employed)
## 
##    0    1 
## 1084 4783
table(addhealth$benefitscore)
## 
##    0    1    2    3 
##  604  380  351 3406
table(addhealth$beneScore)
## 
##    0    1    2 
##  604  731 3406
addhealth$employment<-as.factor(addhealth$employment)
summary(addhealth$employment)
## empFullBene   empNoBene empPartBene  unemployed        NA's 
##        3406         604         731        1084          62
#install.packages("magrittr")
#library(data.table)
library(magrittr)
addhealthSub<-addhealth%>%
  dplyr::select(aid,W5BIOWGT,raceW5min,sexW5,scoreW4,scoreW5,ageW1,ageW3,ageW4,ageW5,sexualOrientationW3,sexualOrientationW4,sexualOrientationW5,educW1,educW3,educW4,educW5,employment,maritalW1,maritalW3,maritalW4,maritalW5,religionW1,religionW3,religionW4,religionW5,HHincomeW3,HHincomeW4,HHincomeW5,livesParent2,close.momW1,close.momW3,close.momW4,close.momW5,parentSupervise,mom.educ,mom.educ2,mom.nativity,mom.employed,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,scoreW4,scoreW5)
out<-melt(setDT(addhealthSub), id = "aid",
          measure.vars = list(age=c("ageW1","ageW3","ageW4","ageW5"),
                             sexualorientation=c("sexualOrientationW3","sexualOrientationW3", "sexualOrientationW4", "sexualOrientationW5"), 
                              education=c("educW1","educW3", "educW4", "educW5"),
                              hhincome=c("HHincomeW3","HHincomeW4", "HHincomeW4", "HHincomeW5"),
                              marriage=c("maritalW1","maritalW3","maritalW4","maritalW5"),
                              religion=c("religionW1","religionW3","religionW4","religionW5"),
                              closeMom=c("close.momW1","close.momW3","close.momW4","close.momW5"),
                             score=c("scoreW4","scoreW4","scoreW4","scoreW5")))%>%
  setorder(aid)

#library(dplyr)

addhealthLong<-addhealthSub%>%
  dplyr::select(aid, W5BIOWGT,raceW5min,sexW5,livesParent2,parentSupervise,mom.educ,mom.educ2,mom.nativity,mom.employed,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,employment)%>%
  left_join(., out, "aid")


addhealthLong$wave<-Recode(addhealthLong$variable,recodes="1=1;2=3;3=4;4=5")

addhealthS1long <- addhealthLong[,c(27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26)]
addhealthS1long$sexualorientation <- factor(addhealthS1long$sexualorientation, levels = c("Straight", "Gay", "Bisexual"))
addhealthS1long$raceXsexo<-interaction(addhealthS1long$raceW5min,addhealthS1long$sexualorientation,drop=TRUE)

addhealthS1long$education<-as.factor(addhealthS1long$education)
addhealthS1long$hhincome<-as.factor(addhealthS1long$hhincome)
#addhealthS1long$employment<-as.factor(addhealthS1long$employment)
addhealthS1long$marriage<-as.factor(addhealthS1long$marriage)
addhealthS1long$mom.educ<-as.factor(addhealthS1long$mom.educ)
addhealthS1long$mom.educ2<-as.factor(addhealthS1long$mom.educ2)
addhealthS1long$mom.employed<-as.factor(addhealthS1long$mom.employed)
addhealthS1long$mom.pubass<-as.factor(addhealthS1long$mom.pubass)
addhealthS1long$mom.nativity<-as.factor(addhealthS1long$mom.nativity)
addhealthS1long$religion<-as.factor(addhealthS1long$religion)

addhealthS1long$livesParent2<-as.factor(addhealthS1long$livesParent2)
addhealthS1long$schoolClubJoin<-as.factor(addhealthS1long$schoolClubJoin)
addhealthS1long$teacherTrouble<-as.factor(addhealthS1long$teacherTrouble)
addhealthS1long$studentTrouble<-as.factor(addhealthS1long$studentTrouble)
addhealthS1long$parentSupervise<-as.factor(addhealthS1long$parentSupervise)
addhealthS1long$closeMom<-as.factor(addhealthS1long$closeMom)
addhealthexp<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo)

#library(tidyr)

addhealthBOMB<-addhealthexp%>%
  filter(complete.cases(.))
addhealthBOMB$livesParent2<-factor(addhealthBOMB$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthBOMB$closeMom<-as.numeric(addhealthBOMB$closeMom)
addhealthBOMB$closeMom2<-Recode(addhealthBOMB$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthBOMB$parentSupervise<-as.numeric(addhealthBOMB$parentSupervise)
#table(addhealthBOMB$parentSupervise)
addhealthBOMB$parentSupervise2<-Recode(addhealthBOMB$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthBOMB$teacherTrouble2<-as.numeric(addhealthBOMB$teacherTrouble)
addhealthBOMB$studentTrouble2<-as.numeric(addhealthBOMB$studentTrouble)
addhealthBOMB$schoolconflict<-addhealthBOMB$studentTrouble2+addhealthBOMB$teacherTrouble2
addhealthBOMB$schoolconflict<-as.factor(addhealthBOMB$schoolconflict)
#install.packages("geepack")
library(geepack)
#wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,,,,,,,,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,

fit<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBOMB, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fit)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage, data = addhealthBOMB, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                                Estimate    Std.err   Wald Pr(>|W|)    
## (Intercept)                   3.4649189  0.6168953 31.547 1.95e-08 ***
## age                          -0.0875666  0.0379377  5.328  0.02099 *  
## I(age^2)                      0.0020074  0.0007525  7.116  0.00764 ** 
## sexW5Female                   0.1581419  0.1211654  1.703  0.19183    
## raceXsexob_black.Straight     0.3806230  0.1539909  6.109  0.01345 *  
## raceXsexoc_hispanic.Straight  0.2365964  0.2456997  0.927  0.33557    
## raceXsexoa_white.Gay         -0.3981976  0.1595432  6.229  0.01257 *  
## raceXsexob_black.Gay          0.1747051  0.2207857  0.626  0.42878    
## raceXsexoc_hispanic.Gay       0.8971712  0.6982601  1.651  0.19884    
## raceXsexoa_white.Bisexual     0.3043039  0.1867727  2.655  0.10326    
## raceXsexob_black.Bisexual    -0.1947599  0.3981531  0.239  0.62473    
## raceXsexoc_hispanic.Bisexual  0.2228296  0.3274918  0.463  0.49624    
## education1                   -0.1437687  0.2037183  0.498  0.48036    
## education2                   -0.4305850  0.2211951  3.789  0.05158 .  
## employmentempNoBene           0.0930432  0.1954456  0.227  0.63403    
## employmentempPartBene         0.1428496  0.1765695  0.655  0.41850    
## employmentunemployed          0.4651464  0.1819475  6.536  0.01057 *  
## hhincomeb_30k<50k            -0.3015014  0.1581869  3.633  0.05665 .  
## hhincomec_50k>100k           -0.0190452  0.1552813  0.015  0.90238    
## hhincomed_>100k              -0.0955309  0.1868809  0.261  0.60922    
## marriage1                     0.0170306  0.1052989  0.026  0.87151    
## marriage2                     0.2332339  0.2060918  1.281  0.25776    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)    3.602  0.2629
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha   0.7911  0.1541
## Number of clusters:   2063  Maximum cluster size: 450
#education+employment+hhincome+marriage
#fit2<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBOMB, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fit2)

fit3<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBOMB, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fit3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage + livesParent2 + 
##     mom.educ + mom.pubass + closeMom + parentSupervise + religion + 
##     schoolconflict + schoolClubJoin + scale(schoolSeg) + scale(pre), 
##     data = addhealthBOMB, weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), 
##     id = aid, waves = wave, corstr = "exchangeable")
## 
##  Coefficients:
##                               Estimate   Std.err  Wald Pr(>|W|)    
## (Intercept)                   3.510446  0.710784 24.39  7.9e-07 ***
## age                          -0.094999  0.040990  5.37   0.0205 *  
## I(age^2)                      0.002116  0.000793  7.12   0.0076 ** 
## sexW5Female                   0.123144  0.119761  1.06   0.3038    
## raceXsexob_black.Straight     0.213006  0.163122  1.71   0.1916    
## raceXsexoc_hispanic.Straight -0.047661  0.268801  0.03   0.8593    
## raceXsexoa_white.Gay         -0.393744  0.167590  5.52   0.0188 *  
## raceXsexob_black.Gay         -0.030754  0.233047  0.02   0.8950    
## raceXsexoc_hispanic.Gay       0.531514  0.706117  0.57   0.4516    
## raceXsexoa_white.Bisexual     0.318376  0.181094  3.09   0.0787 .  
## raceXsexob_black.Bisexual    -0.386330  0.399730  0.93   0.3338    
## raceXsexoc_hispanic.Bisexual -0.158454  0.332452  0.23   0.6336    
## education1                   -0.156093  0.200158  0.61   0.4355    
## education2                   -0.446832  0.223275  4.01   0.0454 *  
## employmentempNoBene           0.084296  0.192590  0.19   0.6616    
## employmentempPartBene         0.071741  0.171427  0.18   0.6756    
## employmentunemployed          0.416257  0.177641  5.49   0.0191 *  
## hhincomeb_30k<50k            -0.273597  0.150162  3.32   0.0685 .  
## hhincomec_50k>100k           -0.015090  0.151758  0.01   0.9208    
## hhincomed_>100k              -0.088484  0.180598  0.24   0.6242    
## marriage1                     0.030670  0.109289  0.08   0.7790    
## marriage2                     0.252877  0.209135  1.46   0.2266    
## livesParent2lives_1_parent    0.244244  0.161862  2.28   0.1313    
## livesParent2lives_other      -0.119122  0.328301  0.13   0.7167    
## mom.educ1                    -0.360046  0.170907  4.44   0.0351 *  
## mom.educ2                    -0.484507  0.189160  6.56   0.0104 *  
## mom.pubass1                   0.347122  0.234055  2.20   0.1381    
## closeMom                      0.018181  0.038036  0.23   0.6327    
## parentSupervise               0.060637  0.037326  2.64   0.1043    
## religion1                    -0.143936  0.120553  1.43   0.2325    
## schoolconflict3               0.049418  0.133185  0.14   0.7106    
## schoolconflict4              -0.033291  0.155031  0.05   0.8300    
## schoolClubJoin1               0.297985  0.185828  2.57   0.1088    
## scale(schoolSeg)              0.092086  0.059905  2.36   0.1242    
## scale(pre)                   -0.087763  0.043465  4.08   0.0435 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.58   0.252
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.797   0.125
## Number of clusters:   2063  Maximum cluster size: 450
#fit4<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoin+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBOMB, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fit4)
library(huxtable)
## 
## Attaching package: 'huxtable'
## The following object is masked from 'package:StatMeasures':
## 
##     contents
## The following object is masked from 'package:dplyr':
## 
##     add_rownames
allmodels<-huxreg(fit,fit3,statistics = character(0)) 
allmodels
(1)(2)
(Intercept)3.465 ***3.510 ***
(0.617)   (0.711)   
age-0.088 *  -0.095 *  
(0.038)   (0.041)   
I(age^2)0.002 ** 0.002 ** 
(0.001)   (0.001)   
sexW5Female0.158    0.123    
(0.121)   (0.120)   
raceXsexob_black.Straight0.381 *  0.213    
(0.154)   (0.163)   
raceXsexoc_hispanic.Straight0.237    -0.048    
(0.246)   (0.269)   
raceXsexoa_white.Gay-0.398 *  -0.394 *  
(0.160)   (0.168)   
raceXsexob_black.Gay0.175    -0.031    
(0.221)   (0.233)   
raceXsexoc_hispanic.Gay0.897    0.532    
(0.698)   (0.706)   
raceXsexoa_white.Bisexual0.304    0.318    
(0.187)   (0.181)   
raceXsexob_black.Bisexual-0.195    -0.386    
(0.398)   (0.400)   
raceXsexoc_hispanic.Bisexual0.223    -0.158    
(0.327)   (0.332)   
education1-0.144    -0.156    
(0.204)   (0.200)   
education2-0.431    -0.447 *  
(0.221)   (0.223)   
employmentempNoBene0.093    0.084    
(0.195)   (0.193)   
employmentempPartBene0.143    0.072    
(0.177)   (0.171)   
employmentunemployed0.465 *  0.416 *  
(0.182)   (0.178)   
hhincomeb_30k<50k-0.302    -0.274    
(0.158)   (0.150)   
hhincomec_50k>100k-0.019    -0.015    
(0.155)   (0.152)   
hhincomed_>100k-0.096    -0.088    
(0.187)   (0.181)   
marriage10.017    0.031    
(0.105)   (0.109)   
marriage20.233    0.253    
(0.206)   (0.209)   
livesParent2lives_1_parent        0.244    
        (0.162)   
livesParent2lives_other        -0.119    
        (0.328)   
mom.educ1        -0.360 *  
        (0.171)   
mom.educ2        -0.485 *  
        (0.189)   
mom.pubass1        0.347    
        (0.234)   
closeMom        0.018    
        (0.038)   
parentSupervise        0.061    
        (0.037)   
religion1        -0.144    
        (0.121)   
schoolconflict3        0.049    
        (0.133)   
schoolconflict4        -0.033    
        (0.155)   
schoolClubJoin1        0.298    
        (0.186)   
scale(schoolSeg)        0.092    
        (0.060)   
scale(pre)        -0.088 *  
        (0.043)   
*** p < 0.001; ** p < 0.01; * p < 0.05.
nobs(fit3)
## [1] 24657
addhealthwh<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo,raceW5min)

addhealthwhi<-addhealthwh%>%
  subset(raceW5min=='a_white')

addhealthWhite<-addhealthwhi%>%
  filter(complete.cases(.))
addhealthWhite$livesParent2<-factor(addhealthWhite$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthWhite$closeMom<-as.numeric(addhealthWhite$closeMom)
addhealthWhite$closeMom2<-Recode(addhealthWhite$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthWhite$parentSupervise<-as.numeric(addhealthWhite$parentSupervise)
#table(addhealthWhite$parentSupervise)
addhealthWhite$parentSupervise2<-Recode(addhealthWhite$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthWhite$teacherTrouble2<-as.numeric(addhealthWhite$teacherTrouble)
addhealthWhite$studentTrouble2<-as.numeric(addhealthWhite$studentTrouble)
addhealthWhite$schoolconflict<-addhealthWhite$studentTrouble2+addhealthWhite$teacherTrouble2
addhealthWhite$schoolconflict<-as.factor(addhealthWhite$schoolconflict)
addhealthWhite$raceXsexo<-as.factor(addhealthWhite$raceXsexo)
addhealthWhite$raceXsexo <- droplevels(addhealthWhite$raceXsexo)
table(addhealthWhite$raceXsexo)
## 
## a_white.Straight      a_white.Gay a_white.Bisexual 
##            15312              490              609
fitWh<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthWhite, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitWh)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage, data = addhealthWhite, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                            Estimate   Std.err  Wald Pr(>|W|)    
## (Intercept)                2.810140  0.567034 24.56  7.2e-07 ***
## age                       -0.031307  0.030707  1.04    0.308    
## I(age^2)                   0.000898  0.000649  1.91    0.166    
## sexW5Female                0.100711  0.135129  0.56    0.456    
## raceXsexoa_white.Gay      -0.350094  0.174113  4.04    0.044 *  
## raceXsexoa_white.Bisexual  0.223013  0.187780  1.41    0.235    
## education1                -0.177601  0.269628  0.43    0.510    
## education2                -0.532091  0.285953  3.46    0.063 .  
## employmentempNoBene        0.083545  0.224884  0.14    0.710    
## employmentempPartBene      0.135111  0.194388  0.48    0.487    
## employmentunemployed       0.444388  0.201968  4.84    0.028 *  
## hhincomeb_30k<50k         -0.150562  0.180686  0.69    0.405    
## hhincomec_50k>100k         0.160655  0.145681  1.22    0.270    
## hhincomed_>100k            0.071093  0.180332  0.16    0.693    
## marriage1                 -0.021352  0.127952  0.03    0.867    
## marriage2                  0.128264  0.259592  0.24    0.621    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.63   0.321
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.557  0.0845
## Number of clusters:   1532  Maximum cluster size: 392
#education+employment+hhincome+marriage
#fit2<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthWhite, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fit2)

fitWh3<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthWhite, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitWh3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage + livesParent2 + 
##     mom.educ + mom.pubass + closeMom + parentSupervise + religion + 
##     schoolconflict + schoolClubJoin + scale(schoolSeg) + scale(pre), 
##     data = addhealthWhite, weights = W5BIOWGT/mean(W5BIOWGT, 
##         na.rm = T), id = aid, waves = wave, corstr = "exchangeable")
## 
##  Coefficients:
##                             Estimate   Std.err  Wald Pr(>|W|)    
## (Intercept)                 2.616554  0.652674 16.07  6.1e-05 ***
## age                        -0.031241  0.030760  1.03   0.3098    
## I(age^2)                    0.000896  0.000654  1.88   0.1708    
## sexW5Female                 0.082918  0.133961  0.38   0.5359    
## raceXsexoa_white.Gay       -0.367379  0.184855  3.95   0.0469 *  
## raceXsexoa_white.Bisexual   0.232803  0.190112  1.50   0.2207    
## education1                 -0.152057  0.266101  0.33   0.5677    
## education2                 -0.485693  0.285829  2.89   0.0893 .  
## employmentempNoBene         0.117340  0.221241  0.28   0.5959    
## employmentempPartBene       0.057171  0.189720  0.09   0.7632    
## employmentunemployed        0.395037  0.199120  3.94   0.0473 *  
## hhincomeb_30k<50k          -0.145013  0.183905  0.62   0.4304    
## hhincomec_50k>100k          0.170580  0.148942  1.31   0.2521    
## hhincomed_>100k             0.079809  0.181147  0.19   0.6595    
## marriage1                  -0.017018  0.129100  0.02   0.8951    
## marriage2                   0.132122  0.262653  0.25   0.6149    
## livesParent2lives_1_parent  0.146458  0.197842  0.55   0.4591    
## livesParent2lives_other     0.114116  0.395390  0.08   0.7729    
## mom.educ1                  -0.434468  0.200679  4.69   0.0304 *  
## mom.educ2                  -0.568908  0.215076  7.00   0.0082 ** 
## mom.pubass1                 0.512910  0.327836  2.45   0.1177    
## closeMom                    0.014817  0.042193  0.12   0.7255    
## parentSupervise             0.076303  0.042331  3.25   0.0715 .  
## religion1                   0.007071  0.069796  0.01   0.9193    
## schoolconflict3            -0.027417  0.152315  0.03   0.8572    
## schoolconflict4             0.023652  0.175534  0.02   0.8928    
## schoolClubJoin1             0.313127  0.211909  2.18   0.1395    
## scale(schoolSeg)            0.113061  0.060863  3.45   0.0632 .  
## scale(pre)                 -0.109855  0.040076  7.51   0.0061 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.51   0.296
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.546    0.11
## Number of clusters:   1532  Maximum cluster size: 392
#fit4<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoin+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthWhite, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fit4)
#library(huxtable)
allmodelsWh<-huxreg(fitWh,fitWh3,statistics = character(0)) 
allmodelsWh
(1)(2)
(Intercept)2.810 ***2.617 ***
(0.567)   (0.653)   
age-0.031    -0.031    
(0.031)   (0.031)   
I(age^2)0.001    0.001    
(0.001)   (0.001)   
sexW5Female0.101    0.083    
(0.135)   (0.134)   
raceXsexoa_white.Gay-0.350 *  -0.367 *  
(0.174)   (0.185)   
raceXsexoa_white.Bisexual0.223    0.233    
(0.188)   (0.190)   
education1-0.178    -0.152    
(0.270)   (0.266)   
education2-0.532    -0.486    
(0.286)   (0.286)   
employmentempNoBene0.084    0.117    
(0.225)   (0.221)   
employmentempPartBene0.135    0.057    
(0.194)   (0.190)   
employmentunemployed0.444 *  0.395 *  
(0.202)   (0.199)   
hhincomeb_30k<50k-0.151    -0.145    
(0.181)   (0.184)   
hhincomec_50k>100k0.161    0.171    
(0.146)   (0.149)   
hhincomed_>100k0.071    0.080    
(0.180)   (0.181)   
marriage1-0.021    -0.017    
(0.128)   (0.129)   
marriage20.128    0.132    
(0.260)   (0.263)   
livesParent2lives_1_parent        0.146    
        (0.198)   
livesParent2lives_other        0.114    
        (0.395)   
mom.educ1        -0.434 *  
        (0.201)   
mom.educ2        -0.569 ** 
        (0.215)   
mom.pubass1        0.513    
        (0.328)   
closeMom        0.015    
        (0.042)   
parentSupervise        0.076    
        (0.042)   
religion1        0.007    
        (0.070)   
schoolconflict3        -0.027    
        (0.152)   
schoolconflict4        0.024    
        (0.176)   
schoolClubJoin1        0.313    
        (0.212)   
scale(schoolSeg)        0.113    
        (0.061)   
scale(pre)        -0.110 ** 
        (0.040)   
*** p < 0.001; ** p < 0.01; * p < 0.05.
addhealthM<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo,raceW5min)

addhealthMa<-addhealthM%>%
  subset(sexW5=='Male')


addhealthMale<-addhealthMa%>%
  filter(complete.cases(.))
addhealthMale$livesParent2<-factor(addhealthMale$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthMale$closeMom<-as.numeric(addhealthMale$closeMom)
addhealthMale$closeMom2<-Recode(addhealthMale$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthMale$parentSupervise<-as.numeric(addhealthMale$parentSupervise)
#table(addhealthMale$parentSupervise)
addhealthMale$parentSupervise2<-Recode(addhealthMale$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthMale$teacherTrouble2<-as.numeric(addhealthMale$teacherTrouble)
addhealthMale$studentTrouble2<-as.numeric(addhealthMale$studentTrouble)
addhealthMale$schoolconflict<-addhealthMale$studentTrouble2+addhealthMale$teacherTrouble2
addhealthMale$schoolconflict<-as.factor(addhealthMale$schoolconflict)
addhealthMale$raceXsexo<-factor(addhealthMale$raceXsexo)
table(addhealthMale$raceXsexo)
## 
##    a_white.Straight    b_black.Straight c_hispanic.Straight         a_white.Gay 
##                5846                2887                 643                 188 
##         b_black.Gay      c_hispanic.Gay    a_white.Bisexual 
##                  92                  13                  48
fitM<-geeglm(score~age+I(age^2)+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthMale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitM)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + raceXsexo + education + 
##     employment + hhincome + marriage, data = addhealthMale, weights = W5BIOWGT/mean(W5BIOWGT, 
##     na.rm = T), id = aid, waves = wave, corstr = "exchangeable")
## 
##  Coefficients:
##                              Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)                   4.22685  0.87293 23.45  1.3e-06 ***
## age                          -0.15694  0.06021  6.79   0.0091 ** 
## I(age^2)                      0.00344  0.00117  8.63   0.0033 ** 
## raceXsexob_black.Straight    -0.01165  0.30750  0.00   0.9698    
## raceXsexoc_hispanic.Straight  0.15088  0.41525  0.13   0.7163    
## raceXsexoa_white.Gay         -0.50843  0.34139  2.22   0.1364    
## raceXsexob_black.Gay         -0.09098  0.71681  0.02   0.8990    
## raceXsexoc_hispanic.Gay       1.01029  0.93637  1.16   0.2806    
## raceXsexoa_white.Bisexual     0.31074  0.47721  0.42   0.5150    
## education1                    0.19883  0.13938  2.03   0.1537    
## education2                   -0.14912  0.15676  0.90   0.3415    
## employmentempNoBene          -0.15896  0.30748  0.27   0.6052    
## employmentempPartBene         0.02251  0.29305  0.01   0.9388    
## employmentunemployed          0.14147  0.40020  0.12   0.7237    
## hhincomeb_30k<50k            -0.42724  0.23161  3.40   0.0651 .  
## hhincomec_50k>100k           -0.10488  0.24239  0.19   0.6652    
## hhincomed_>100k              -0.37208  0.25952  2.06   0.1516    
## marriage1                    -0.09106  0.18998  0.23   0.6317    
## marriage2                     0.11396  0.26340  0.19   0.6653    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.21   0.306
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.703  0.0991
## Number of clusters:   737  Maximum cluster size: 450
#education+employment+hhincome+marriage
#fitM2<-geeglm(score~age+I(age^2)+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthMale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitM2)

fitM3<-geeglm(score~age+I(age^2)+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthMale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitM3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + raceXsexo + education + 
##     employment + hhincome + marriage + livesParent2 + mom.educ + 
##     mom.pubass + closeMom + parentSupervise + religion + schoolconflict + 
##     schoolClubJoin + scale(schoolSeg) + scale(pre), data = addhealthMale, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                              Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)                   4.89408  1.24504 15.45  8.5e-05 ***
## age                          -0.17396  0.06265  7.71   0.0055 ** 
## I(age^2)                      0.00366  0.00118  9.63   0.0019 ** 
## raceXsexob_black.Straight    -0.07358  0.31467  0.05   0.8151    
## raceXsexoc_hispanic.Straight  0.05239  0.44042  0.01   0.9053    
## raceXsexoa_white.Gay         -0.53342  0.35785  2.22   0.1361    
## raceXsexob_black.Gay         -0.04096  0.73847  0.00   0.9558    
## raceXsexoc_hispanic.Gay       0.78890  1.03395  0.58   0.4455    
## raceXsexoa_white.Bisexual     0.21068  0.45279  0.22   0.6417    
## education1                    0.11304  0.17231  0.43   0.5118    
## education2                   -0.27092  0.20012  1.83   0.1758    
## employmentempNoBene          -0.15388  0.30473  0.25   0.6136    
## employmentempPartBene         0.05431  0.28196  0.04   0.8473    
## employmentunemployed          0.14372  0.36246  0.16   0.6917    
## hhincomeb_30k<50k            -0.33481  0.21120  2.51   0.1129    
## hhincomec_50k>100k           -0.10252  0.22246  0.21   0.6449    
## hhincomed_>100k              -0.37378  0.23604  2.51   0.1133    
## marriage1                    -0.05906  0.20750  0.08   0.7759    
## marriage2                     0.15954  0.28029  0.32   0.5692    
## livesParent2lives_1_parent   -0.05584  0.28728  0.04   0.8459    
## livesParent2lives_other      -0.23381  0.69942  0.11   0.7382    
## mom.educ1                    -0.61197  0.34954  3.07   0.0800 .  
## mom.educ2                    -0.60836  0.36604  2.76   0.0965 .  
## mom.pubass1                  -0.23801  0.35196  0.46   0.4989    
## closeMom                     -0.02296  0.05861  0.15   0.6953    
## parentSupervise               0.07596  0.06372  1.42   0.2332    
## religion1                    -0.30569  0.18808  2.64   0.1041    
## schoolconflict3              -0.03964  0.21790  0.03   0.8557    
## schoolconflict4              -0.19978  0.25949  0.59   0.4414    
## schoolClubJoin1               0.35567  0.32657  1.19   0.2761    
## scale(schoolSeg)              0.19222  0.09354  4.22   0.0399 *  
## scale(pre)                    0.03601  0.09228  0.15   0.6964    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.11   0.338
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.694   0.163
## Number of clusters:   737  Maximum cluster size: 450
#fitM4<-geeglm(score~age+I(age^2)+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoin+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthMale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitM4)
#library(huxtable)
allmodelsM<-huxreg(fitM,fitM3,statistics = character(0)) 
allmodelsM
(1)(2)
(Intercept)4.227 ***4.894 ***
(0.873)   (1.245)   
age-0.157 ** -0.174 ** 
(0.060)   (0.063)   
I(age^2)0.003 ** 0.004 ** 
(0.001)   (0.001)   
raceXsexob_black.Straight-0.012    -0.074    
(0.307)   (0.315)   
raceXsexoc_hispanic.Straight0.151    0.052    
(0.415)   (0.440)   
raceXsexoa_white.Gay-0.508    -0.533    
(0.341)   (0.358)   
raceXsexob_black.Gay-0.091    -0.041    
(0.717)   (0.738)   
raceXsexoc_hispanic.Gay1.010    0.789    
(0.936)   (1.034)   
raceXsexoa_white.Bisexual0.311    0.211    
(0.477)   (0.453)   
education10.199    0.113    
(0.139)   (0.172)   
education2-0.149    -0.271    
(0.157)   (0.200)   
employmentempNoBene-0.159    -0.154    
(0.307)   (0.305)   
employmentempPartBene0.023    0.054    
(0.293)   (0.282)   
employmentunemployed0.141    0.144    
(0.400)   (0.362)   
hhincomeb_30k<50k-0.427    -0.335    
(0.232)   (0.211)   
hhincomec_50k>100k-0.105    -0.103    
(0.242)   (0.222)   
hhincomed_>100k-0.372    -0.374    
(0.260)   (0.236)   
marriage1-0.091    -0.059    
(0.190)   (0.207)   
marriage20.114    0.160    
(0.263)   (0.280)   
livesParent2lives_1_parent        -0.056    
        (0.287)   
livesParent2lives_other        -0.234    
        (0.699)   
mom.educ1        -0.612    
        (0.350)   
mom.educ2        -0.608    
        (0.366)   
mom.pubass1        -0.238    
        (0.352)   
closeMom        -0.023    
        (0.059)   
parentSupervise        0.076    
        (0.064)   
religion1        -0.306    
        (0.188)   
schoolconflict3        -0.040    
        (0.218)   
schoolconflict4        -0.200    
        (0.259)   
schoolClubJoin1        0.356    
        (0.327)   
scale(schoolSeg)        0.192 *  
        (0.094)   
scale(pre)        0.036    
        (0.092)   
*** p < 0.001; ** p < 0.01; * p < 0.05.
addhealthFem<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo,raceW5min)

addhealthFema<-addhealthFem%>%
  subset(sexW5=='Female')


addhealthFemale<-addhealthFema%>%
  filter(complete.cases(.))
addhealthFemale$livesParent2<-factor(addhealthFemale$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthFemale$closeMom<-as.numeric(addhealthFemale$closeMom)
addhealthFemale$closeMom2<-Recode(addhealthFemale$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthFemale$parentSupervise<-as.numeric(addhealthFemale$parentSupervise)
#table(addhealthFemale$parentSupervise)
addhealthFemale$parentSupervise2<-Recode(addhealthFemale$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthFemale$teacherTrouble2<-as.numeric(addhealthFemale$teacherTrouble)
addhealthFemale$studentTrouble2<-as.numeric(addhealthFemale$studentTrouble)
addhealthFemale$schoolconflict<-addhealthFemale$studentTrouble2+addhealthFemale$teacherTrouble2
addhealthFemale$schoolconflict<-as.factor(addhealthFemale$schoolconflict)
addhealthFemale$raceXsexo<-factor(addhealthFemale$raceXsexo)
table(addhealthFemale$raceXsexo)
## 
##    a_white.Straight    b_black.Straight c_hispanic.Straight         a_white.Gay 
##                9466                3282                1081                 302 
##         b_black.Gay      c_hispanic.Gay    a_white.Bisexual    b_black.Bisexual 
##                 120                  30                 561                  70 
## c_hispanic.Bisexual 
##                  28
fitF<-geeglm(score~age+I(age^2)+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthFemale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitF)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + raceXsexo + education + 
##     employment + hhincome + marriage, data = addhealthFemale, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                               Estimate   Std.err  Wald Pr(>|W|)    
## (Intercept)                   3.078619  0.639292 23.19  1.5e-06 ***
## age                          -0.026879  0.032320  0.69  0.40561    
## I(age^2)                      0.000722  0.000679  1.13  0.28794    
## raceXsexob_black.Straight     0.643001  0.160909 15.97  6.4e-05 ***
## raceXsexoc_hispanic.Straight  0.259608  0.260898  0.99  0.31971    
## raceXsexoa_white.Gay         -0.389102  0.286384  1.85  0.17425    
## raceXsexob_black.Gay          0.651857  0.309356  4.44  0.03511 *  
## raceXsexoc_hispanic.Gay       2.241957  0.448893 24.94  5.9e-07 ***
## raceXsexoa_white.Bisexual     0.268287  0.206382  1.69  0.19362    
## raceXsexob_black.Bisexual     0.159300  0.386485  0.17  0.68021    
## raceXsexoc_hispanic.Bisexual  0.108363  0.332551  0.11  0.74453    
## education1                   -0.636224  0.370124  2.95  0.08562 .  
## education2                   -0.884396  0.408509  4.69  0.03039 *  
## employmentempNoBene           0.319713  0.246741  1.68  0.19506    
## employmentempPartBene         0.287783  0.226474  1.61  0.20383    
## employmentunemployed          0.679409  0.190497 12.72  0.00036 ***
## hhincomeb_30k<50k            -0.076757  0.173049  0.20  0.65736    
## hhincomec_50k>100k            0.201772  0.141433  2.04  0.15369    
## hhincomed_>100k               0.258528  0.192910  1.80  0.18020    
## marriage1                     0.102510  0.117089  0.77  0.38131    
## marriage2                     0.313539  0.302708  1.07  0.30030    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.79   0.361
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha     0.65  0.0897
## Number of clusters:   1326  Maximum cluster size: 324
#education+employment+hhincome+marriage
#fitF2<-geeglm(score~age+I(age^2)+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthFemale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitF2)

fitF3<-geeglm(score~age+I(age^2)+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthFemale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitF3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + raceXsexo + education + 
##     employment + hhincome + marriage + livesParent2 + mom.educ + 
##     mom.pubass + closeMom + parentSupervise + religion + schoolconflict + 
##     schoolClubJoin + scale(schoolSeg) + scale(pre), data = addhealthFemale, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                               Estimate   Std.err  Wald Pr(>|W|)    
## (Intercept)                   2.713856  0.670535 16.38  5.2e-05 ***
## age                          -0.017887  0.034476  0.27  0.60389    
## I(age^2)                      0.000568  0.000724  0.62  0.43270    
## raceXsexob_black.Straight     0.429745  0.181143  5.63  0.01767 *  
## raceXsexoc_hispanic.Straight -0.042884  0.313175  0.02  0.89108    
## raceXsexoa_white.Gay         -0.477348  0.320072  2.22  0.13586    
## raceXsexob_black.Gay          0.718911  0.263547  7.44  0.00638 ** 
## raceXsexoc_hispanic.Gay       1.802898  0.842873  4.58  0.03244 *  
## raceXsexoa_white.Bisexual     0.335023  0.220206  2.31  0.12816    
## raceXsexob_black.Bisexual    -0.517748  0.453800  1.30  0.25391    
## raceXsexoc_hispanic.Bisexual -0.117935  0.416777  0.08  0.77720    
## education1                   -0.659821  0.384451  2.95  0.08611 .  
## education2                   -0.836935  0.433242  3.73  0.05338 .  
## employmentempNoBene           0.329643  0.238160  1.92  0.16632    
## employmentempPartBene         0.101225  0.206244  0.24  0.62356    
## employmentunemployed          0.539713  0.197750  7.45  0.00635 ** 
## hhincomeb_30k<50k            -0.070950  0.179715  0.16  0.69300    
## hhincomec_50k>100k            0.249148  0.147010  2.87  0.09012 .  
## hhincomed_>100k               0.344520  0.205465  2.81  0.09359 .  
## marriage1                     0.120576  0.122367  0.97  0.32444    
## marriage2                     0.336581  0.310623  1.17  0.27856    
## livesParent2lives_1_parent    0.424961  0.167894  6.41  0.01137 *  
## livesParent2lives_other      -0.256504  0.377995  0.46  0.49740    
## mom.educ1                    -0.173575  0.194065  0.80  0.37110    
## mom.educ2                    -0.462578  0.219494  4.44  0.03508 *  
## mom.pubass1                   0.743083  0.264443  7.90  0.00495 ** 
## closeMom                      0.005114  0.051268  0.01  0.92054    
## parentSupervise               0.045179  0.043005  1.10  0.29345    
## religion1                     0.037083  0.081015  0.21  0.64715    
## schoolconflict3               0.211844  0.167829  1.59  0.20685    
## schoolconflict4               0.095816  0.181679  0.28  0.59792    
## schoolClubJoin1               0.276558  0.208396  1.76  0.18448    
## scale(schoolSeg)             -0.016519  0.073833  0.05  0.82297    
## scale(pre)                   -0.149921  0.043409 11.93  0.00055 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.94   0.387
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha     1.09   0.255
## Number of clusters:   1326  Maximum cluster size: 324
#fitF4<-geeglm(score~age+I(age^2)+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoin+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthFemale, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitF4)
#library(huxtable)
allmodelsF<-huxreg(fitF,fitF3,statistics = character(0)) 
allmodelsF
(1)(2)
(Intercept)3.079 ***2.714 ***
(0.639)   (0.671)   
age-0.027    -0.018    
(0.032)   (0.034)   
I(age^2)0.001    0.001    
(0.001)   (0.001)   
raceXsexob_black.Straight0.643 ***0.430 *  
(0.161)   (0.181)   
raceXsexoc_hispanic.Straight0.260    -0.043    
(0.261)   (0.313)   
raceXsexoa_white.Gay-0.389    -0.477    
(0.286)   (0.320)   
raceXsexob_black.Gay0.652 *  0.719 ** 
(0.309)   (0.264)   
raceXsexoc_hispanic.Gay2.242 ***1.803 *  
(0.449)   (0.843)   
raceXsexoa_white.Bisexual0.268    0.335    
(0.206)   (0.220)   
raceXsexob_black.Bisexual0.159    -0.518    
(0.386)   (0.454)   
raceXsexoc_hispanic.Bisexual0.108    -0.118    
(0.333)   (0.417)   
education1-0.636    -0.660    
(0.370)   (0.384)   
education2-0.884 *  -0.837    
(0.409)   (0.433)   
employmentempNoBene0.320    0.330    
(0.247)   (0.238)   
employmentempPartBene0.288    0.101    
(0.226)   (0.206)   
employmentunemployed0.679 ***0.540 ** 
(0.190)   (0.198)   
hhincomeb_30k<50k-0.077    -0.071    
(0.173)   (0.180)   
hhincomec_50k>100k0.202    0.249    
(0.141)   (0.147)   
hhincomed_>100k0.259    0.345    
(0.193)   (0.205)   
marriage10.103    0.121    
(0.117)   (0.122)   
marriage20.314    0.337    
(0.303)   (0.311)   
livesParent2lives_1_parent        0.425 *  
        (0.168)   
livesParent2lives_other        -0.257    
        (0.378)   
mom.educ1        -0.174    
        (0.194)   
mom.educ2        -0.463 *  
        (0.219)   
mom.pubass1        0.743 ** 
        (0.264)   
closeMom        0.005    
        (0.051)   
parentSupervise        0.045    
        (0.043)   
religion1        0.037    
        (0.081)   
schoolconflict3        0.212    
        (0.168)   
schoolconflict4        0.096    
        (0.182)   
schoolClubJoin1        0.277    
        (0.208)   
scale(schoolSeg)        -0.017    
        (0.074)   
scale(pre)        -0.150 ***
        (0.043)   
*** p < 0.001; ** p < 0.01; * p < 0.05.
addhealthBl<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo,raceW5min)

addhealthBla<-addhealthBl%>%
  subset(raceW5min=='b_black')

addhealthBlack<-addhealthBla%>%
  filter(complete.cases(.))
addhealthBlack$livesParent2<-factor(addhealthBlack$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthBlack$closeMom<-as.numeric(addhealthBlack$closeMom)
addhealthBlack$closeMom2<-Recode(addhealthBlack$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthBlack$parentSupervise<-as.numeric(addhealthBlack$parentSupervise)
#table(addhealthBlack$parentSupervise)
addhealthBlack$parentSupervise2<-Recode(addhealthBlack$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthBlack$teacherTrouble2<-as.numeric(addhealthBlack$teacherTrouble)
addhealthBlack$studentTrouble2<-as.numeric(addhealthBlack$studentTrouble)
addhealthBlack$schoolconflict<-addhealthBlack$studentTrouble2+addhealthBlack$teacherTrouble2
addhealthBlack$schoolconflict<-as.factor(addhealthBlack$schoolconflict)
addhealthBlack$raceXsexo<-factor(addhealthBlack$raceXsexo)
table(addhealthBlack$raceXsexo)
## 
## b_black.Straight      b_black.Gay b_black.Bisexual 
##             6169              212               70
fitB<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBlack, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitB)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage, data = addhealthBlack, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                           Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)                5.66537  1.37253 17.04  3.7e-05 ***
## age                       -0.25985  0.08813  8.69   0.0032 ** 
## I(age^2)                   0.00531  0.00164 10.50   0.0012 ** 
## sexW5Female                0.64609  0.31170  4.30   0.0382 *  
## raceXsexob_black.Gay      -0.33687  0.19282  3.05   0.0806 .  
## raceXsexob_black.Bisexual -0.56838  0.47390  1.44   0.2304    
## education1                -0.17947  0.10226  3.08   0.0793 .  
## education2                -0.15006  0.16925  0.79   0.3753    
## employmentempNoBene        0.10722  0.40546  0.07   0.7914    
## employmentempPartBene      0.49690  0.52581  0.89   0.3446    
## employmentunemployed       0.83273  0.46445  3.21   0.0730 .  
## hhincomeb_30k<50k         -0.45002  0.26110  2.97   0.0848 .  
## hhincomec_50k>100k        -0.44935  0.28290  2.52   0.1122    
## hhincomed_>100k           -0.44841  0.37636  1.42   0.2335    
## marriage1                  0.20755  0.23772  0.76   0.3826    
## marriage2                  0.40471  0.38797  1.09   0.2969    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.52   0.631
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.991   0.262
## Number of clusters:   367  Maximum cluster size: 450
#education+employment+hhincome+marriage
#fitB2<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBlack, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitB2)

fitB3<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBlack, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitB3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage + livesParent2 + 
##     mom.educ + mom.pubass + closeMom + parentSupervise + religion + 
##     schoolconflict + schoolClubJoin + scale(schoolSeg) + scale(pre), 
##     data = addhealthBlack, weights = W5BIOWGT/mean(W5BIOWGT, 
##         na.rm = T), id = aid, waves = wave, corstr = "exchangeable")
## 
##  Coefficients:
##                            Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)                 5.88242  1.36066 18.69  1.5e-05 ***
## age                        -0.25675  0.08454  9.22  0.00239 ** 
## I(age^2)                    0.00515  0.00152 11.51  0.00069 ***
## sexW5Female                 0.53456  0.30287  3.12  0.07757 .  
## raceXsexob_black.Gay       -0.44335  0.20348  4.75  0.02934 *  
## raceXsexob_black.Bisexual  -0.54684  0.45495  1.44  0.22937    
## education1                 -0.33077  0.16231  4.15  0.04156 *  
## education2                 -0.32161  0.28327  1.29  0.25623    
## employmentempNoBene        -0.16980  0.40870  0.17  0.67780    
## employmentempPartBene       0.36385  0.53327  0.47  0.49506    
## employmentunemployed        0.65870  0.49973  1.74  0.18746    
## hhincomeb_30k<50k          -0.41690  0.23746  3.08  0.07914 .  
## hhincomec_50k>100k         -0.44460  0.25908  2.94  0.08615 .  
## hhincomed_>100k            -0.42108  0.34947  1.45  0.22823    
## marriage1                   0.22272  0.24663  0.82  0.36649    
## marriage2                   0.43838  0.37559  1.36  0.24314    
## livesParent2lives_1_parent  0.39516  0.28252  1.96  0.16190    
## livesParent2lives_other    -0.78873  0.52457  2.26  0.13269    
## mom.educ1                  -0.46033  0.42895  1.15  0.28320    
## mom.educ2                  -0.26542  0.46733  0.32  0.57007    
## mom.pubass1                 0.24991  0.37810  0.44  0.50863    
## closeMom                    0.00985  0.08514  0.01  0.90790    
## parentSupervise             0.01692  0.07891  0.05  0.83019    
## religion1                  -0.26865  0.21513  1.56  0.21174    
## schoolconflict3             0.36945  0.33732  1.20  0.27341    
## schoolconflict4             0.11827  0.34471  0.12  0.73153    
## schoolClubJoin1             0.96260  0.46458  4.29  0.03827 *  
## scale(schoolSeg)           -0.08236  0.14231  0.33  0.56277    
## scale(pre)                 -0.02931  0.14565  0.04  0.84050    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.59    0.68
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.943   0.399
## Number of clusters:   367  Maximum cluster size: 450
#fitB4<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoin+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthBlack, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitB4)
#library(huxtable)
allmodelsB<-huxreg(fitB,fitB3,statistics = character(0)) 
allmodelsB
(1)(2)
(Intercept)5.665 ***5.882 ***
(1.373)   (1.361)   
age-0.260 ** -0.257 ** 
(0.088)   (0.085)   
I(age^2)0.005 ** 0.005 ***
(0.002)   (0.002)   
sexW5Female0.646 *  0.535    
(0.312)   (0.303)   
raceXsexob_black.Gay-0.337    -0.443 *  
(0.193)   (0.203)   
raceXsexob_black.Bisexual-0.568    -0.547    
(0.474)   (0.455)   
education1-0.179    -0.331 *  
(0.102)   (0.162)   
education2-0.150    -0.322    
(0.169)   (0.283)   
employmentempNoBene0.107    -0.170    
(0.405)   (0.409)   
employmentempPartBene0.497    0.364    
(0.526)   (0.533)   
employmentunemployed0.833    0.659    
(0.464)   (0.500)   
hhincomeb_30k<50k-0.450    -0.417    
(0.261)   (0.237)   
hhincomec_50k>100k-0.449    -0.445    
(0.283)   (0.259)   
hhincomed_>100k-0.448    -0.421    
(0.376)   (0.349)   
marriage10.208    0.223    
(0.238)   (0.247)   
marriage20.405    0.438    
(0.388)   (0.376)   
livesParent2lives_1_parent        0.395    
        (0.283)   
livesParent2lives_other        -0.789    
        (0.525)   
mom.educ1        -0.460    
        (0.429)   
mom.educ2        -0.265    
        (0.467)   
mom.pubass1        0.250    
        (0.378)   
closeMom        0.010    
        (0.085)   
parentSupervise        0.017    
        (0.079)   
religion1        -0.269    
        (0.215)   
schoolconflict3        0.369    
        (0.337)   
schoolconflict4        0.118    
        (0.345)   
schoolClubJoin1        0.963 *  
        (0.465)   
scale(schoolSeg)        -0.082    
        (0.142)   
scale(pre)        -0.029    
        (0.146)   
*** p < 0.001; ** p < 0.01; * p < 0.05.
addhealthHi<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo,raceW5min)

addhealthHis<-addhealthHi%>%
  subset(raceW5min=='c_hispanic')

addhealthHispanic<-addhealthHis%>%
  filter(complete.cases(.))
addhealthHispanic$livesParent2<-factor(addhealthHispanic$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthHispanic$closeMom<-as.numeric(addhealthHispanic$closeMom)
addhealthHispanic$closeMom2<-Recode(addhealthHispanic$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthHispanic$parentSupervise<-as.numeric(addhealthHispanic$parentSupervise)
#table(addhealthHispanic$parentSupervise)
addhealthHispanic$parentSupervise2<-Recode(addhealthHispanic$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthHispanic$teacherTrouble2<-as.numeric(addhealthHispanic$teacherTrouble)
addhealthHispanic$studentTrouble2<-as.numeric(addhealthHispanic$studentTrouble)
addhealthHispanic$schoolconflict<-addhealthHispanic$studentTrouble2+addhealthHispanic$teacherTrouble2
addhealthHispanic$schoolconflict<-as.factor(addhealthHispanic$schoolconflict)
addhealthHispanic$raceXsexo<-as.factor(addhealthHispanic$raceXsexo)
addhealthHispanic$raceXsexo <- droplevels(addhealthHispanic$raceXsexo)
table(addhealthHispanic$raceXsexo)
## 
## c_hispanic.Straight      c_hispanic.Gay c_hispanic.Bisexual 
##                1724                  43                  28
fitH<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthHispanic, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitH)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage, data = addhealthHispanic, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                               Estimate   Std.err Wald Pr(>|W|)   
## (Intercept)                   2.454657  0.876583 7.84   0.0051 **
## age                           0.023512  0.044766 0.28   0.5994   
## I(age^2)                     -0.000475  0.000913 0.27   0.6031   
## sexW5Female                   0.045089  0.516764 0.01   0.9305   
## raceXsexoc_hispanic.Gay       0.565429  0.491550 1.32   0.2500   
## raceXsexoc_hispanic.Bisexual  0.035445  0.209377 0.03   0.8656   
## education1                    0.272502  0.179368 2.31   0.1287   
## education2                   -0.017844  0.292044 0.00   0.9513   
## employmentempNoBene          -0.144671  0.777441 0.03   0.8524   
## employmentempPartBene        -0.865062  0.536248 2.60   0.1067   
## employmentunemployed          0.670840  0.852456 0.62   0.4313   
## hhincomeb_30k<50k            -0.192146  0.224673 0.73   0.3924   
## hhincomec_50k>100k           -0.103171  0.136918 0.57   0.4511   
## hhincomed_>100k              -0.314455  0.176674 3.17   0.0751 . 
## marriage1                     0.161644  0.385867 0.18   0.6753   
## marriage2                     0.203663  0.263185 0.60   0.4390   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.18   0.447
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.855    0.25
## Number of clusters:   164  Maximum cluster size: 144
#education+employment+hhincome+marriage
#fitH2<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthHispanic, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitH2)

fitH3<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthHispanic, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitH3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage + livesParent2 + 
##     mom.educ + mom.pubass + closeMom + parentSupervise + religion + 
##     schoolconflict + schoolClubJoin + scale(schoolSeg) + scale(pre), 
##     data = addhealthHispanic, weights = W5BIOWGT/mean(W5BIOWGT, 
##         na.rm = T), id = aid, waves = wave, corstr = "exchangeable")
## 
##  Coefficients:
##                               Estimate   Std.err Wald Pr(>|W|)  
## (Intercept)                   3.104386  1.274192 5.94    0.015 *
## age                           0.019011  0.043153 0.19    0.660  
## I(age^2)                     -0.000513  0.000895 0.33    0.567  
## sexW5Female                  -0.532197  0.461412 1.33    0.249  
## raceXsexoc_hispanic.Gay       0.905780  0.843065 1.15    0.283  
## raceXsexoc_hispanic.Bisexual  0.721257  0.643032 1.26    0.262  
## education1                    0.212933  0.175720 1.47    0.226  
## education2                   -0.121321  0.285889 0.18    0.671  
## employmentempNoBene           0.514646  0.642000 0.64    0.423  
## employmentempPartBene        -0.917241  0.894171 1.05    0.305  
## employmentunemployed          1.247867  0.700588 3.17    0.075 .
## hhincomeb_30k<50k            -0.099950  0.229453 0.19    0.663  
## hhincomec_50k>100k           -0.076310  0.153875 0.25    0.620  
## hhincomed_>100k              -0.329258  0.176977 3.46    0.063 .
## marriage1                     0.252930  0.375516 0.45    0.501  
## marriage2                     0.267995  0.265022 1.02    0.312  
## livesParent2lives_1_parent   -0.571748  0.536681 1.13    0.287  
## livesParent2lives_other      -0.896069  0.907206 0.98    0.323  
## mom.educ1                     0.684990  0.482822 2.01    0.156  
## mom.educ2                    -0.640828  0.604393 1.12    0.289  
## mom.pubass1                  -0.679167  0.637028 1.14    0.286  
## closeMom                     -0.099078  0.079702 1.55    0.214  
## parentSupervise               0.003106  0.163876 0.00    0.985  
## religion1                    -0.058741  0.121131 0.24    0.628  
## schoolconflict3               1.317662  0.536915 6.02    0.014 *
## schoolconflict4              -0.763895  0.615631 1.54    0.215  
## schoolClubJoin1              -0.161329  0.484262 0.11    0.739  
## scale(schoolSeg)              0.065268  0.284526 0.05    0.819  
## scale(pre)                    0.196067  0.156813 1.56    0.211  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     2.61   0.519
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.559   0.394
## Number of clusters:   164  Maximum cluster size: 144
#fitH4<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoi#n+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthHispanic, #weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitH4)
#library(huxtable)
allmodelsH<-huxreg(fitH,fitH3,statistics = character(0)) 
allmodelsH
(1)(2)
(Intercept)2.455 **3.104 *
(0.877)  (1.274) 
age0.024   0.019  
(0.045)  (0.043) 
I(age^2)-0.000   -0.001  
(0.001)  (0.001) 
sexW5Female0.045   -0.532  
(0.517)  (0.461) 
raceXsexoc_hispanic.Gay0.565   0.906  
(0.492)  (0.843) 
raceXsexoc_hispanic.Bisexual0.035   0.721  
(0.209)  (0.643) 
education10.273   0.213  
(0.179)  (0.176) 
education2-0.018   -0.121  
(0.292)  (0.286) 
employmentempNoBene-0.145   0.515  
(0.777)  (0.642) 
employmentempPartBene-0.865   -0.917  
(0.536)  (0.894) 
employmentunemployed0.671   1.248  
(0.852)  (0.701) 
hhincomeb_30k<50k-0.192   -0.100  
(0.225)  (0.229) 
hhincomec_50k>100k-0.103   -0.076  
(0.137)  (0.154) 
hhincomed_>100k-0.314   -0.329  
(0.177)  (0.177) 
marriage10.162   0.253  
(0.386)  (0.376) 
marriage20.204   0.268  
(0.263)  (0.265) 
livesParent2lives_1_parent       -0.572  
       (0.537) 
livesParent2lives_other       -0.896  
       (0.907) 
mom.educ1       0.685  
       (0.483) 
mom.educ2       -0.641  
       (0.604) 
mom.pubass1       -0.679  
       (0.637) 
closeMom       -0.099  
       (0.080) 
parentSupervise       0.003  
       (0.164) 
religion1       -0.059  
       (0.121) 
schoolconflict3       1.318 *
       (0.537) 
schoolconflict4       -0.764  
       (0.616) 
schoolClubJoin1       -0.161  
       (0.484) 
scale(schoolSeg)       0.065  
       (0.285) 
scale(pre)       0.196  
       (0.157) 
*** p < 0.001; ** p < 0.01; * p < 0.05.
addhealthL<-addhealthS1long%>%
  dplyr::select(wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,livesParent2,parentSupervise,mom.educ2,mom.educ,mom.employed,mom.nativity,mom.pubass,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,sexualorientation,religion,closeMom,score,raceXsexo,raceW5min)

#library(tidyr)
addhealthLG<-addhealthL%>%
  subset(sexualorientation=='Gay'|sexualorientation=="Bisexual")
addhealthLG$sexualorientation<-factor(addhealthLG$sexualorientation)

addhealthLG$raceXsexo<-interaction(addhealthLG$raceW5min,addhealthLG$sexualorientation)
addhealthLG$raceXsexo<-factor(addhealthLG$raceXsexo)

addhealthLGBTQ<-addhealthLG%>%
  filter(complete.cases(.))
addhealthLGBTQ$livesParent2<-factor(addhealthLGBTQ$livesParent2,levels=c("lives_2_parents","lives_1_parent","lives_other"))
addhealthLGBTQ$closeMom<-as.numeric(addhealthLGBTQ$closeMom)
addhealthLGBTQ$closeMom2<-Recode(addhealthLGBTQ$closeMom,recodes="1:3='a_notClose';4:5='b_close'")
addhealthLGBTQ$parentSupervise<-as.numeric(addhealthLGBTQ$parentSupervise)
#table(addhealthLGBTQ$parentSupervise)
addhealthLGBTQ$parentSupervise2<-Recode(addhealthLGBTQ$parentSupervise,recodes="1:3='a_easy';4:6='b_moderate';7:8='c_strict'")
addhealthLGBTQ$teacherTrouble2<-as.numeric(addhealthLGBTQ$teacherTrouble)
addhealthLGBTQ$studentTrouble2<-as.numeric(addhealthLGBTQ$studentTrouble)
addhealthLGBTQ$schoolconflict<-addhealthLGBTQ$studentTrouble2+addhealthLGBTQ$teacherTrouble2
addhealthLGBTQ$schoolconflict<-as.factor(addhealthLGBTQ$schoolconflict)
#install.packages("geepack")
#library(geepack)
#wave,aid,W5BIOWGT,age,sexW5,education,hhincome,employment,marriage,,,,,,,,pre,teacherTrouble,studentTrouble,schoolClubJoin,schoolSeg,

fitL<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthLGBTQ, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitL)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage, data = addhealthLGBTQ, 
##     weights = W5BIOWGT/mean(W5BIOWGT, na.rm = T), id = aid, waves = wave, 
##     corstr = "exchangeable")
## 
##  Coefficients:
##                              Estimate  Std.err Wald Pr(>|W|)   
## (Intercept)                   1.57212  2.28545 0.47   0.4915   
## age                           0.07130  0.14311 0.25   0.6183   
## I(age^2)                     -0.00127  0.00253 0.25   0.6147   
## sexW5Female                   0.00792  0.61050 0.00   0.9896   
## raceXsexob_black.Gay          1.70967  0.87584 3.81   0.0509 . 
## raceXsexoc_hispanic.Gay       1.43045  1.66016 0.74   0.3889   
## raceXsexoa_white.Bisexual     0.52741  0.29871 3.12   0.0775 . 
## raceXsexob_black.Bisexual     3.35179  1.18462 8.01   0.0047 **
## raceXsexoc_hispanic.Bisexual  1.20103  1.48668 0.65   0.4192   
## education1                   -0.55057  1.12586 0.24   0.6248   
## education2                   -1.08734  1.20407 0.82   0.3665   
## employmentempNoBene          -0.66919  0.86869 0.59   0.4411   
## employmentempPartBene         0.68322  0.73119 0.87   0.3501   
## employmentunemployed         -1.60702  0.71602 5.04   0.0248 * 
## hhincomeb_30k<50k            -0.23942  0.34515 0.48   0.4879   
## hhincomec_50k>100k            0.66625  0.25613 6.77   0.0093 **
## hhincomed_>100k               0.06250  0.36483 0.03   0.8640   
## marriage1                     0.48378  0.54472 0.79   0.3745   
## marriage2                    -0.42727  0.42582 1.01   0.3157   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     3.58   0.849
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.464   0.251
## Number of clusters:   107  Maximum cluster size: 108
#education+employment+hhincome+marriage
#fitL2<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthLGBTQ, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitL2)

fitL3<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+education+employment+hhincome+marriage+livesParent2+mom.educ+mom.pubass+closeMom+parentSupervise+religion+schoolconflict+schoolClubJoin+scale(schoolSeg)+scale(pre),id=aid , wave = wave, corstr ="exchangeable",   data=addhealthLGBTQ, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
summary(fitL3)
## 
## Call:
## geeglm(formula = score ~ age + I(age^2) + sexW5 + raceXsexo + 
##     education + employment + hhincome + marriage + livesParent2 + 
##     mom.educ + mom.pubass + closeMom + parentSupervise + religion + 
##     schoolconflict + schoolClubJoin + scale(schoolSeg) + scale(pre), 
##     data = addhealthLGBTQ, weights = W5BIOWGT/mean(W5BIOWGT, 
##         na.rm = T), id = aid, waves = wave, corstr = "exchangeable")
## 
##  Coefficients:
##                              Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)                  -1.49494  2.85539  0.27   0.6006    
## age                           0.07640  0.14128  0.29   0.5887    
## I(age^2)                     -0.00123  0.00249  0.24   0.6232    
## sexW5Female                   0.04740  0.58722  0.01   0.9357    
## raceXsexob_black.Gay          1.19851  0.95570  1.57   0.2098    
## raceXsexoc_hispanic.Gay       0.85910  1.59535  0.29   0.5902    
## raceXsexoa_white.Bisexual     0.17678  0.36867  0.23   0.6316    
## raceXsexob_black.Bisexual     3.44033  1.12022  9.43   0.0021 ** 
## raceXsexoc_hispanic.Bisexual -0.02877  1.36694  0.00   0.9832    
## education1                   -0.88524  1.41582  0.39   0.5318    
## education2                   -1.31648  1.50790  0.76   0.3826    
## employmentempNoBene          -1.68722  0.77474  4.74   0.0294 *  
## employmentempPartBene         0.54170  0.61893  0.77   0.3815    
## employmentunemployed         -1.34672  0.82643  2.66   0.1032    
## hhincomeb_30k<50k            -0.18042  0.45041  0.16   0.6887    
## hhincomec_50k>100k            0.67145  0.24065  7.78   0.0053 ** 
## hhincomed_>100k              -0.20798  0.37551  0.31   0.5797    
## marriage1                     0.45947  0.36391  1.59   0.2067    
## marriage2                    -0.18438  0.38379  0.23   0.6309    
## livesParent2lives_1_parent    0.16774  0.57984  0.08   0.7724    
## livesParent2lives_other       2.48493  0.94418  6.93   0.0085 ** 
## mom.educ1                     0.94091  0.70676  1.77   0.1831    
## mom.educ2                     0.85359  0.74858  1.30   0.2542    
## mom.pubass1                   0.09401  1.01127  0.01   0.9259    
## closeMom                     -0.06225  0.09768  0.41   0.5239    
## parentSupervise               0.44100  0.18368  5.76   0.0164 *  
## religion1                     1.11343  0.18262 37.17  1.1e-09 ***
## schoolconflict3              -0.23951  0.65018  0.14   0.7126    
## schoolconflict4               0.46511  0.68561  0.46   0.4975    
## schoolClubJoin1              -0.13494  0.96830  0.02   0.8892    
## scale(schoolSeg)             -0.06728  0.30982  0.05   0.8281    
## scale(pre)                   -0.71197  0.26115  7.43   0.0064 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = exchangeable 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.86   0.552
##   Link = identity 
## 
## Estimated Correlation Parameters:
##       Estimate Std.err
## alpha    0.244   0.139
## Number of clusters:   107  Maximum cluster size: 108
#fitL4<-geeglm(score~age+I(age^2)+sexW5+raceXsexo+livesParent2+mom.educ+mom.pubass+closeMom2+parentSupervise2+religion+schoolconflict+schoolClubJoin+schoolSeg+pre+education+employment+hhincome+marriage,id=aid , wave = wave, corstr ="exchangeable",   data=addhealthLGBTQ, weights=W5BIOWGT/mean(W5BIOWGT,na.rm=T))
#summary(fitL4)
#library(huxtable)
allmodelsL<-huxreg(fitL,fitL3,statistics = character(0)) 
allmodelsL
(1)(2)
(Intercept)1.572   -1.495    
(2.285)  (2.855)   
age0.071   0.076    
(0.143)  (0.141)   
I(age^2)-0.001   -0.001    
(0.003)  (0.002)   
sexW5Female0.008   0.047    
(0.611)  (0.587)   
raceXsexob_black.Gay1.710   1.199    
(0.876)  (0.956)   
raceXsexoc_hispanic.Gay1.430   0.859    
(1.660)  (1.595)   
raceXsexoa_white.Bisexual0.527   0.177    
(0.299)  (0.369)   
raceXsexob_black.Bisexual3.352 **3.440 ** 
(1.185)  (1.120)   
raceXsexoc_hispanic.Bisexual1.201   -0.029    
(1.487)  (1.367)   
education1-0.551   -0.885    
(1.126)  (1.416)   
education2-1.087   -1.316    
(1.204)  (1.508)   
employmentempNoBene-0.669   -1.687 *  
(0.869)  (0.775)   
employmentempPartBene0.683   0.542    
(0.731)  (0.619)   
employmentunemployed-1.607 * -1.347    
(0.716)  (0.826)   
hhincomeb_30k<50k-0.239   -0.180    
(0.345)  (0.450)   
hhincomec_50k>100k0.666 **0.671 ** 
(0.256)  (0.241)   
hhincomed_>100k0.062   -0.208    
(0.365)  (0.376)   
marriage10.484   0.459    
(0.545)  (0.364)   
marriage2-0.427   -0.184    
(0.426)  (0.384)   
livesParent2lives_1_parent       0.168    
       (0.580)   
livesParent2lives_other       2.485 ** 
       (0.944)   
mom.educ1       0.941    
       (0.707)   
mom.educ2       0.854    
       (0.749)   
mom.pubass1       0.094    
       (1.011)   
closeMom       -0.062    
       (0.098)   
parentSupervise       0.441 *  
       (0.184)   
religion1       1.113 ***
       (0.183)   
schoolconflict3       -0.240    
       (0.650)   
schoolconflict4       0.465    
       (0.686)   
schoolClubJoin1       -0.135    
       (0.968)   
scale(schoolSeg)       -0.067    
       (0.310)   
scale(pre)       -0.712 ** 
       (0.261)   
*** p < 0.001; ** p < 0.01; * p < 0.05.