LTS sample

read in sleep and psych and merge

HF_sleep<- fread("/Users/claire/Desktop/dissertation/ctc_sleep_psych/HF_sleep_dat.csv", header=T, data.table = F)
cadd<- fread("/Users/claire/Desktop/dissertation/ctc_sleep_psych/LTS_psych_dat.csv", header=T, data.table=F)

HF_sleep$project<- NULL
HF_sleep$family<- NULL
HF_sleep$bestbet<- NULL
HF_sleep$Trand<- NULL
HF_sleep$racecat<- NULL
HF_sleep$hispanic<- NULL
HF_sleep$work_schedule<- NULL


twindat<- merge(HF_sleep, cadd, by='twinid')

format to be wide

dat_long1<- twindat[ which(twindat$Trand=='1'), ]
head(dat_long1$Trand)


dat_long2<- twindat[ which(twindat$Trand=='2'), ]
head(dat_long1$Trand)

df1 <- data.frame(dat_long1)
df1<-df1 %>% dplyr::rename_all(function(x) paste0("T1", x))

df2 <- data.frame(dat_long2)
df2<-df2 %>% dplyr::rename_all(function(x) paste0("T2", x))

colnames(df1)[colnames(df1)=="T1family"] <- "family"
colnames(df2)[colnames(df2)=="T2family"] <- "family"

sleep_psych_wide<-merge(df1,df2, by="family", all = T)
nrow(sleep_psych_wide)


colnames(sleep_psych_wide)[colnames(sleep_psych_wide)=="T1bestbet"] <- "zyg"


head(sleep_psych_wide)
table(sleep_psych_wide$zyg) ### 557 MZ pairs, 255 DZ pairs 201 OS

select data

# Select continuous variables for Analysis
vars      <- c(colnames(HF_sleep[4:9]), colnames(cadd[c(4,6)]))            # list of variables names
nv        <- length(vars)                        # number of variables
ntv       <- nv*2                      # number of total variables
selVars   <- paste(c(rep("T1",nv),rep("T2",nv)),vars,sep="")

# Select Data for Analysis
mzData    <- subset(sleep_psych_wide, zyg=="MZ", selVars) 
dzData    <- subset(sleep_psych_wide, zyg=="DZ" | zyg=="OS", selVars) 

twin correlations

# Generate Descriptive Statistics
#colMeans(mzData,na.rm=TRUE)
#colMeans(dzData,na.rm=TRUE)
#cov(mzData,use="complete")[1:12,1:6]
#cov(dzData,use="complete")[1:12,1:6]
mzcors<- cor(mzData,use="complete")[1:ntv,1:length(vars)]
dzcors<- cor(dzData,use="complete")[1:ntv,1:length(vars)]


HF.mz.cors <- data.frame(row.names = vars)
IT<-length(vars)
for (i in 1:length(vars)) {
  IT<- IT+1
  HF.mz.cors$MZCorrs[i] <- mzcors[IT,i]
}

HF.dz.cors <- data.frame(row.names = vars)
IT<-length(vars)
for (i in 1:length(vars)) {
  IT<- IT+1
  HF.dz.cors$DZCorrs[i] <- dzcors[IT,i]
}

insomMZ<- tetrachoric(mzData[,c(1,9)])
insomDZ<- tetrachoric(dzData[,c(1,9)])
HF.mz.cors[1,1]<- insomMZ$rho[1,2]
HF.dz.cors[1,1]<- insomDZ$rho[1,2]

sleep_psych_twin_rs<- cbind(HF.mz.cors,HF.dz.cors)

sleep_psych_vars<- c("Insomnia", "Satisfaction", "Weekday Duration", "Weekend Duration", "Chronotype", "Alertness", "Internalizing", "Externalizing")

row.names(sleep_psych_twin_rs)<- sleep_psych_vars

kable(sleep_psych_twin_rs)
MZCorrs DZCorrs
Insomnia 0.43836253 0.21278156
Satisfaction 0.26589755 0.02182600
Weekday Duration 0.27882175 0.07679480
Weekend Duration 0.27275436 0.06201804
Chronotype 0.52677157 0.18425492
Alertness 0.32764485 0.15005823
Internalizing 0.42548577 0.27241989
Externalizing 0.60578897 0.26728028

twin script – to get rGs mostly

# Set Starting Values 
#set them close to means, and paths at whatever???
# imsom, satis, weekday dur, weekend dur, meq, alert, int, ext
svMe      <- c(.2, 2, 7, 8, 50, 17, 0, 0)                    # start value for means
svPa      <- .5                        # start value for path coefficient a
svPc      <- .5                        # start value for path coefficient c
svPe      <- .5                        # start value for path coefficient for e

# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL

# Create Algebra for expected Mean Matrices
meanG     <- mxMatrix( type="Full", nrow=1, ncol=ntv, free=TRUE, values=svMe, labels=labVars("mean",vars), name="meanG" )

# Create Matrices for Variance Components
covA      <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=valDiag(svPa,nv), labels=labLower("VA",nv), name="VA" ) 
covC      <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=valDiag(svPc,nv), labels=labLower("VC",nv), name="VC" )
covE      <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=valDiag(svPe,nv), labels=labLower("VE",nv), name="VE" )

# Create Algebra for expected Variance/Covariance Matrices in MZ & DZ twins
covP      <- mxAlgebra( expression= VA+VC+VE, name="V" )
covMZ     <- mxAlgebra( expression= VA+VC, name="cMZ" )
covDZ     <- mxAlgebra( expression= 0.5%x%VA+ VC, name="cDZ" )
expCovMZ  <- mxAlgebra( expression= rbind( cbind(V, cMZ), cbind(t(cMZ), V)), name="expCovMZ" )
expCovDZ  <- mxAlgebra( expression= rbind( cbind(V, cDZ), cbind(t(cDZ), V)), name="expCovDZ" )

# Create Data Objects for Multiple Groups
dataMZ    <- mxData( observed=mzData, type="raw" )
dataDZ    <- mxData( observed=dzData, type="raw" )

# Create Expectation Objects for Multiple Groups
expMZ     <- mxExpectationNormal( covariance="expCovMZ", means="meanG", dimnames=selVars )
expDZ     <- mxExpectationNormal( covariance="expCovDZ", means="meanG", dimnames=selVars )
funML     <- mxFitFunctionML()

# Create Model Objects for Multiple Groups
pars      <- list( meanG, covA, covC, covE, covP )
modelMZ   <- mxModel( pars, covMZ, expCovMZ, dataMZ, expMZ, funML, name="MZ" )
modelDZ   <- mxModel( pars, covDZ, expCovDZ, dataDZ, expDZ, funML, name="DZ" )
multi     <- mxFitFunctionMultigroup( c("MZ","DZ") )

# Create Algebra for Standardization
matI      <- mxMatrix( type="Iden", nrow=nv, ncol=nv, name="I" )
invSD     <- mxAlgebra( expression=solve(sqrt(I*V)), name="iSD" )

# Calculate standardized variance components
unstA     <- mxAlgebra( expression=diag2vec(VA), name="unstA")
unstC     <- mxAlgebra( expression=diag2vec(VC), name="unstC")
unstE     <- mxAlgebra( expression=diag2vec(VE), name="unstE")
stndA     <- mxAlgebra( expression=diag2vec(VA/V), name="stndA")
stndC     <- mxAlgebra( expression=diag2vec(VC/V), name="stndC")
stndE     <- mxAlgebra( expression=diag2vec(VE/V), name="stndE")


# Calculate genetic and environmental correlations
corA      <- mxAlgebra( expression=solve(sqrt(I*VA))%&%VA, name ="rA" ) #cov2cor()
corC      <- mxAlgebra( expression=solve(sqrt(I*VC))%&%VC, name ="rC" )
corE      <- mxAlgebra( expression=solve(sqrt(I*VE))%&%VE, name ="rE" )

# Calculate Phenotypic Correlation

corP      <- mxAlgebra (expression=solve(sqrt(I*V)) %*% V %*% solve(sqrt(I*V)), name="rP")

# Create matrix for Variance Components
colACEs     <- cbind('A','C','E','SA','SC','SE')
estACEs     <- mxAlgebra( expression=cbind(unstA,unstC,unstE,stndA,stndC,stndE), name="ACEs", dimnames=list(vars,colACEs))

# Create Confidence Interval Objects
ciACE     <-  mxCI( c("rA","rC","rE","ACEs" ) )


# Build Model with Confidence Intervals
calc      <- list( matI,invSD,corA,corC,corE,corP,unstA,unstC,unstE,stndA,stndC,stndE,estACEs,ciACE)
modelACE  <- mxModel( "twoACEvc", pars, modelMZ, modelDZ, multi, calc )

# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL


# Run ACE Model
#can turn off intervals, will still estimate model but not CIs
fitACE    <- mxRun( modelACE, intervals=F)
## Running twoACEvc with 116 parameters
# Print Covariance & Correlation Matrices

fitACE$ACEs$result
##                               A             C           E         SA           SC         SE
## insom               0.047140791  -0.005512313  0.12196351 0.28816075 -0.033695494 0.74553474
## sleep_satisfaction  0.605491846  -0.230994714  1.08556731 0.41470214 -0.158208574 0.74350644
## weekday_dur         0.636513642  -0.146860188  1.46166605 0.32619652 -0.075261989 0.74906546
## weekend_dur         1.104132484  -0.295656293  1.90765914 0.40650864 -0.108851827 0.70234318
## total_meq          54.542958368 -12.136471652 48.14937872 0.60231282 -0.134021928 0.53170911
## total_alertness     8.476385721  -3.357681577  8.62506996 0.61674367 -0.244305644 0.62756197
## INT                 0.058216855   0.041654872  0.13471960 0.24816286  0.177563564 0.57427358
## EXT                 0.201173373   0.041612861  0.16784500 0.48991250  0.101338764 0.40874874
fitACE$rA$result ### <--- genetic corrs
##             [,1]         [,2]         [,3]         [,4]         [,5]         [,6]         [,7]         [,8]
## [1,]  1.00000000 -0.514963341 -0.359492827 -0.332463691 -0.238145507  0.361108970  0.352018638  0.181048961
## [2,] -0.51496334  1.000000000  0.159382426  0.385519307  0.455572579 -0.046900779 -0.400283032 -0.272362402
## [3,] -0.35949283  0.159382426  1.000000000  0.767799969  0.093961427 -0.066893610 -0.068807884  0.050945750
## [4,] -0.33246369  0.385519307  0.767799969  1.000000000 -0.205844955  0.151024731 -0.391223014 -0.056276586
## [5,] -0.23814551  0.455572579  0.093961427 -0.205844955  1.000000000 -0.096584335 -0.322954057 -0.251043816
## [6,]  0.36110897 -0.046900779 -0.066893610  0.151024731 -0.096584335  1.000000000  0.051921480 -0.173847008
## [7,]  0.35201864 -0.400283032 -0.068807884 -0.391223014 -0.322954057  0.051921480  1.000000000  0.972699245
## [8,]  0.18104896 -0.272362402  0.050945750 -0.056276586 -0.251043816 -0.173847008  0.972699245  1.000000000
fitACE$rC$result
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [2,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [3,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [4,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [5,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [6,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [7,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
## [8,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
fitACE$rE$result
##             [,1]        [,2]          [,3]         [,4]          [,5]         [,6]         [,7]         [,8]
## [1,]  1.00000000 -0.47007815 -0.2687266340 -0.156722002 -0.1028984112 -0.239047436  0.224204211  0.139498405
## [2,] -0.47007815  1.00000000  0.2986625201  0.197692856  0.1335387460  0.140422685 -0.219085875 -0.147840141
## [3,] -0.26872663  0.29866252  1.0000000000  0.441807732  0.0068108633  0.098451987 -0.111711100 -0.091791093
## [4,] -0.15672200  0.19769286  0.4418077321  1.000000000 -0.1434117735  0.009628964 -0.057749656 -0.071051712
## [5,] -0.10289841  0.13353875  0.0068108633 -0.143411773  1.0000000000  0.068353387 -0.099954446 -0.092992379
## [6,] -0.23904744  0.14042269  0.0984519865  0.009628964  0.0683533869  1.000000000 -0.065507337 -0.094118741
## [7,]  0.22420421 -0.21908587 -0.1117111001 -0.057749656 -0.0999544463 -0.065507337  1.000000000  0.664411576
## [8,]  0.13949840 -0.14784014 -0.0917910926 -0.071051712 -0.0929923788 -0.094118741  0.664411576  1.000000000
fitACE$rP$result ### <--- pheno corrs
##             [,1]        [,2]         [,3]         [,4]         [,5]         [,6]         [,7]         [,8]
## [1,]  1.00000000 -0.53818127 -0.284295454 -0.177200617 -0.178530590 -0.157101275  0.289610977  0.185273019
## [2,] -0.53818127  1.00000000  0.342274095  0.242321493  0.213087108  0.152865731 -0.293661307 -0.202755195
## [3,] -0.28429545  0.34227409  1.000000000  0.522157289  0.020324900  0.118153159 -0.121184679 -0.070609511
## [4,] -0.17720062  0.24232149  0.522157289  1.000000000 -0.150266001  0.064081447 -0.136642150 -0.122087901
## [5,] -0.17853059  0.21308711  0.020324900 -0.150266001  1.000000000  0.072352701 -0.166330244 -0.153716005
## [6,] -0.15710128  0.15286573  0.118153159  0.064081447  0.072352701  1.000000000 -0.091357944 -0.080081185
## [7,]  0.28961098 -0.29366131 -0.121184679 -0.136642150 -0.166330244 -0.091357944  1.000000000  0.758886285
## [8,]  0.18527302 -0.20275520 -0.070609511 -0.122087901 -0.153716005 -0.080081185  0.758886285  1.000000000
### combine both into one matrix and plot

plot correlations

pheno<- as.matrix(fitACE$rP$result)
genetic<- as.matrix(fitACE$rA$result)


full <- matrix(NA, nrow = 8, ncol = 8)
full[upper.tri(full)]<- pheno[upper.tri(pheno)]
full[lower.tri(full)]<- genetic[lower.tri(genetic)]
diag(full)<- 1

colnames(full)<- sleep_psych_vars
row.names(full)<- colnames(full)

corrplot(full, method = "color",
         type = "full",  number.cex = 1,
         addCoef.col = "black", 
         tl.col = "black", tl.srt = 90) 

table of all descriptives

mean <- twindat %>%
  dplyr::summarize(meaninsom = mean(insom, na.rm = TRUE),
  meansatisfaction = mean(sleep_satisfaction, na.rm = TRUE),
  meanweekdaydur = mean(weekday_dur, na.rm = TRUE),
  meanweekenddur = mean(weekend_dur, na.rm = TRUE),
  meanmeq = mean(total_meq, na.rm = TRUE),
  meanalertness = mean(total_alertness, na.rm = TRUE),
  meanint = mean(INT, na.rm = TRUE),
  meanext = mean(EXT, na.rm = TRUE)) %>%
  t %>%
  as.data.frame %>%
  rename(Mean=V1)

sd <- twindat %>%
  dplyr::summarize(sdinsom = sd(insom, na.rm = TRUE),
  sdsatisfaction = sd(sleep_satisfaction, na.rm = TRUE),
  sdweekdaydur = sd(weekday_dur, na.rm = TRUE),
  sdweekenddur = sd(weekend_dur, na.rm = TRUE),
  sdmeq = sd(total_meq, na.rm = TRUE),
  sdalertness = sd(total_alertness, na.rm = TRUE),
  sdint = sd(INT, na.rm = TRUE),
  sdext = sd(EXT, na.rm = TRUE)) %>%
  t %>%
  as.data.frame %>%
  rename(SD=V1)

N <- twindat %>%
  dplyr::summarize(Ninsom = sum(!is.na(insom)),
  Nsatisfaction = sum(!is.na(sleep_satisfaction)),
  Nweekdaydur = sum(!is.na(weekday_dur)),
  Nweekenddur = sum(!is.na(weekend_dur)),
  Nmeq = sum(!is.na(total_meq)),
  Nalertness = sum(!is.na(total_alertness)),
  Nint = sum(!is.na(INT)),
  Next = sum(!is.na(EXT))) %>%
  t %>%
  as.data.frame %>%
  rename(N=V1)



descriptives_lts<- cbind(mean, sd,N,HF.mz.cors,HF.dz.cors)

row.names(descriptives_lts)<- sleep_psych_vars

knitr::kable(
  descriptives_lts
)
Mean SD N MZCorrs DZCorrs
Insomnia 0.20920304 0.40683614 2108 0.43836253 0.21278156
Satisfaction 2.15886376 1.21612481 1901 0.26589755 0.02182600
Weekday Duration 7.08201249 1.38261151 2082 0.27882175 0.07679480
Weekend Duration 8.00396029 1.65610902 1595 0.27275436 0.06201804
Chronotype 50.12500000 9.53205219 2112 0.52677157 0.18425492
Alertness 17.18741126 3.69196155 2113 0.32764485 0.15005823
Internalizing 0.03554821 0.48703977 2113 0.42548577 0.27241989
Externalizing 0.01253584 0.64264042 2113 0.60578897 0.26728028

ABCD sample

read in data and convert to wide format

abcd<- fread("/Users/claire/Desktop/dissertation/ctc_sleep_psych/ABCD_sleep_psych.csv", header=T, data.table=F)

abcd_long1<- abcd[ which(abcd$twin=='1'), ]
head(abcd_long1$twin)

abcd_long2<- abcd[ which(abcd$twin=='2'), ]
head(abcd_long2$twin)

df1 <- data.frame(abcd_long1)
df1<-df1 %>% dplyr::rename_all(function(x) paste0("T1", x))

df2 <- data.frame(abcd_long2)
df2<-df2 %>% dplyr::rename_all(function(x) paste0("T2", x))

colnames(df1)[colnames(df1)=="T1rel_family_id"] <- "family"
colnames(df2)[colnames(df2)=="T2rel_family_id"] <- "family"

abcd_wide<-merge(df1,df2, by="family", all = T)
nrow(abcd_wide)

colnames(abcd_wide)[colnames(abcd_wide)=="T1zyg"] <- "zyg"

abcd_wide$T2zyg<- NULL

table(abcd_wide$zyg) ### 301 MZ pairs, 397 DZ pairs 

select data

# Select Variables for Analysis
vars<- colnames(abcd)[c(3:6, 8:12, 16:18, 20)]
nv        <- length(vars)                        # number of variables
ntv       <- nv*2                      # number of total variables
selVars   <- paste(c(rep("T1",nv),rep("T2",nv)),vars,sep="")

# Select Data for Analysis
mzData    <- subset(abcd_wide, zyg==1, selVars) #this is selecting  MZ pairs
dzData    <- subset(abcd_wide, zyg==2, selVars) #this is selecting  DZ pairs

twin correlations

# Generate Descriptive Statistics
#colMeans(mzData,na.rm=TRUE)
#colMeans(dzData,na.rm=TRUE)
#cov(mzData,use="complete")[1:ntv,1:length(vars)]
#cov(dzData,use="complete")[1:ntv,1:length(vars)]
mzcors<- cor(mzData,use="complete")[1:ntv,1:length(vars)]
dzcors<- cor(dzData,use="complete")[1:ntv,1:length(vars)]

  
mz.cors <- data.frame(row.names = vars)
IT<-length(vars)
for (i in 1:length(vars)) {
  IT<- IT+1
  mz.cors$MZCorrs[i] <- mzcors[IT,i]
}

dz.cors <- data.frame(row.names = vars)
IT<-length(vars)
for (i in 1:length(vars)) {
  IT<- IT+1
  dz.cors$DZCorrs[i] <- dzcors[IT,i]
}

abcd_vars<- c("Weekend Duration", "Weekday Duration", "Chronotype", "Social Jet Lag", "Weekend Duration*", "Weekday Duration*", "Weekend Efficiency*", "Weekday Efficiency*", "Variability*", "Attention", "Internalizing", "Externalizing", "Prodromal")

row.names(mz.cors)<- abcd_vars
row.names(dz.cors)<- abcd_vars

abcd_twin_rs<- cbind(mz.cors, dz.cors)

kable(abcd_twin_rs)
MZCorrs DZCorrs
Weekend Duration 0.28100095 0.20758001
Weekday Duration 0.38994938 0.23888195
Chronotype 0.34241742 0.34005529
Social Jet Lag 0.63493913 0.53983471
Weekend Duration* 0.68461260 0.55797745
Weekday Duration* 0.67806418 0.49964729
Weekend Efficiency* 0.41163490 0.33735259
Weekday Efficiency* 0.25908447 0.22885810
Variability* 0.33078063 0.50273038
Attention 0.43146668 0.22926676
Internalizing 0.41527737 0.36062441
Externalizing 0.64468363 0.26270670
Prodromal 0.23694189 0.28529727

twin script

# Set Starting Values 
# set them close to means, and paths at whatever???
# weekend dur, weekday dur, chrono, jet lag, weekend dur act, weekday dur act, weekend eff, weekday eff, var, att, int, ext, prodromal

svMe      <- c(9, 8, 16, 2, 9, 8, 1, 1, 1, 2, 4, 4, 1)                    # start value for means
svPa      <- .5                        # start value for path coefficient a
svPc      <- .5                        # start value for path coefficient c
svPe      <- .5                        # start value for path coefficient for e

# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL

# Create Algebra for expected Mean Matrices
meanG     <- mxMatrix( type="Full", nrow=1, ncol=ntv, free=TRUE, values=svMe, labels=labVars("mean",vars), name="meanG" )

# Create Matrices for Variance Components
covA      <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=valDiag(svPa,nv), labels=labLower("VA",nv), name="VA" ) 
covC      <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=valDiag(svPc,nv), labels=labLower("VC",nv), name="VC" )
covE      <- mxMatrix( type="Symm", nrow=nv, ncol=nv, free=TRUE, values=valDiag(svPe,nv), labels=labLower("VE",nv), name="VE" )

# Create Algebra for expected Variance/Covariance Matrices in MZ & DZ twins
covP      <- mxAlgebra( expression= VA+VC+VE, name="V" )
covMZ     <- mxAlgebra( expression= VA+VC, name="cMZ" )
covDZ     <- mxAlgebra( expression= 0.5%x%VA+ VC, name="cDZ" )
expCovMZ  <- mxAlgebra( expression= rbind( cbind(V, cMZ), cbind(t(cMZ), V)), name="expCovMZ" )
expCovDZ  <- mxAlgebra( expression= rbind( cbind(V, cDZ), cbind(t(cDZ), V)), name="expCovDZ" )

# Create Data Objects for Multiple Groups
dataMZ    <- mxData( observed=mzData, type="raw" )
dataDZ    <- mxData( observed=dzData, type="raw" )

# Create Expectation Objects for Multiple Groups
expMZ     <- mxExpectationNormal( covariance="expCovMZ", means="meanG", dimnames=selVars )
expDZ     <- mxExpectationNormal( covariance="expCovDZ", means="meanG", dimnames=selVars )
funML     <- mxFitFunctionML()

# Create Model Objects for Multiple Groups
pars      <- list( meanG, covA, covC, covE, covP )
modelMZ   <- mxModel( pars, covMZ, expCovMZ, dataMZ, expMZ, funML, name="MZ" )
modelDZ   <- mxModel( pars, covDZ, expCovDZ, dataDZ, expDZ, funML, name="DZ" )
multi     <- mxFitFunctionMultigroup( c("MZ","DZ") )

# Create Algebra for Standardization
matI      <- mxMatrix( type="Iden", nrow=nv, ncol=nv, name="I" )
invSD     <- mxAlgebra( expression=solve(sqrt(I*V)), name="iSD" )

# Calculate standardized variance components
unstA     <- mxAlgebra( expression=diag2vec(VA), name="unstA")
unstC     <- mxAlgebra( expression=diag2vec(VC), name="unstC")
unstE     <- mxAlgebra( expression=diag2vec(VE), name="unstE")
stndA     <- mxAlgebra( expression=diag2vec(VA/V), name="stndA")
stndC     <- mxAlgebra( expression=diag2vec(VC/V), name="stndC")
stndE     <- mxAlgebra( expression=diag2vec(VE/V), name="stndE")


# Calculate genetic and environmental correlations
corA      <- mxAlgebra( expression=solve(sqrt(I*VA))%&%VA, name ="rA" ) #cov2cor()
corC      <- mxAlgebra( expression=solve(sqrt(I*VC))%&%VC, name ="rC" )
corE      <- mxAlgebra( expression=solve(sqrt(I*VE))%&%VE, name ="rE" )

# Calculate Phenotypic Correlation

corP      <- mxAlgebra (expression=solve(sqrt(I*V)) %*% V %*% solve(sqrt(I*V)), name="rP")

# Create matrix for Variance Components
colACEs     <- cbind('A','C','E','SA','SC','SE')
estACEs     <- mxAlgebra( expression=cbind(unstA,unstC,unstE,stndA,stndC,stndE), name="ACEs", dimnames=list(vars,colACEs))

# Create Confidence Interval Objects
ciACE     <-  mxCI( c("rA","rC","rE","ACEs" ) )


# Build Model with Confidence Intervals
calc      <- list( matI,invSD,corA,corC,corE,corP,unstA,unstC,unstE,stndA,stndC,stndE,estACEs,ciACE)
modelACE  <- mxModel( "twoACEvc", pars, modelMZ, modelDZ, multi, calc )

# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL


# Run ACE Model
#can turn off intervals, will still estimate model but not CIs
fitACE    <- mxRun( modelACE, intervals=F)
## Running twoACEvc with 286 parameters
## Warning: In model 'twoACEvc' Optimizer returned a non-zero status code 5. The Hessian at the solution does not appear
## to be convex. See ?mxCheckIdentification for possible diagnosis (Mx status RED).
# Print Covariance & Correlation Matrices

fitACE$ACEs$result
##                                          A          C          E         SA         SC         SE
## weekend_dur_mcq_wave_2          0.61010550 0.54871914 0.95491980 0.28863730 0.25959578 0.45176691
## weekday_dur_mcq_wave_2          0.62652821 0.56591279 0.96942186 0.28980941 0.26177090 0.44841968
## chronotype_wave_2               2.08360457 1.69307439 4.22957697 0.26024706 0.21146893 0.52828401
## social_jet_lag_wave_2           0.66063765 0.59552177 1.01649375 0.29069004 0.26203812 0.44727183
## avg_weekend_dur                 0.49578155 0.50096937 0.47904001 0.33594294 0.33945823 0.32459883
## avg_weekday_dur                 0.45227845 0.46156143 0.42755165 0.33717110 0.34409150 0.31873740
## avg_weekend_effic               0.38307131 0.40932678 0.29650991 0.35179401 0.37590575 0.27230024
## avg_weekday_effic               0.38082348 0.40800529 0.29082014 0.35272900 0.37790553 0.26936548
## variability                     0.42813835 0.44161693 0.38863565 0.34022683 0.35093778 0.30883539
## cbcl_scr_syn_attention_r_wave_2 0.94236597 0.67293503 1.61533960 0.29169632 0.20829771 0.50000597
## cbcl_scr_syn_internal_r_wave_2  1.36078595 1.16904569 2.28034590 0.28289724 0.24303587 0.47406689
## cbcl_scr_syn_external_r_wave_2  1.52536654 1.29029818 2.29867549 0.29825285 0.25229025 0.44945690
## total_score_wave_2              0.99518498 0.78155985 1.80059943 0.27819100 0.21847488 0.50333412
fitACE$rA$result ### <--- genetic corrs
##                [,1]        [,2]         [,3]        [,4]         [,5]         [,6]          [,7]          [,8]
##  [1,]  1.0000000000 0.248130026  0.060601892 0.112103187  0.170493149  0.255747355  0.1922970320  0.1906690682
##  [2,]  0.2481300264 1.000000000  0.190314711 0.115853137  0.185094551  0.288642725  0.1779975834  0.1827763204
##  [3,]  0.0606018924 0.190314711  1.000000000 0.144516662 -0.021519331 -0.028638666 -0.0667097884 -0.0643417494
##  [4,]  0.1121031868 0.115853137  0.144516662 1.000000000  0.120198674  0.068796955  0.1790883087  0.1808470681
##  [5,]  0.1704931485 0.185094551 -0.021519331 0.120198674  1.000000000  0.334438057  0.3574402755  0.3552221320
##  [6,]  0.2557473554 0.288642725 -0.028638666 0.068796955  0.334438057  1.000000000  0.3680295055  0.3761075563
##  [7,]  0.1922970320 0.177997583 -0.066709788 0.179088309  0.357440275  0.368029505  1.0000000000  0.5341027122
##  [8,]  0.1906690682 0.182776320 -0.064341749 0.180847068  0.355222132  0.376107556  0.5341027122  1.0000000000
##  [9,]  0.2136022757 0.131937044 -0.074638885 0.203286317  0.214224601  0.307996446  0.4391717105  0.4405826955
## [10,]  0.0514000051 0.104761352  0.067952235 0.087962818  0.058781268  0.061128308  0.0852108326  0.0841641032
## [11,]  0.0856880755 0.082965504  0.100283004 0.083635152 -0.021788280  0.018497903  0.0068194065  0.0067537154
## [12,] -0.0092212667 0.042189926  0.108311089 0.079354568  0.058643004 -0.019794877  0.0091058967  0.0105743959
## [13,] -0.0376863912 0.040145584  0.122230106 0.200704611  0.025836077  0.045676350  0.0937437711  0.0952596401
##                [,9]       [,10]         [,11]         [,12]        [,13]
##  [1,]  0.2136022757 0.051400005  0.0856880755 -0.0092212667 -0.037686391
##  [2,]  0.1319370437 0.104761352  0.0829655043  0.0421899262  0.040145584
##  [3,] -0.0746388852 0.067952235  0.1002830037  0.1083110892  0.122230106
##  [4,]  0.2032863170 0.087962818  0.0836351517  0.0793545680  0.200704611
##  [5,]  0.2142246006 0.058781268 -0.0217882801  0.0586430041  0.025836077
##  [6,]  0.3079964456 0.061128308  0.0184979025 -0.0197948769  0.045676350
##  [7,]  0.4391717105 0.085210833  0.0068194065  0.0091058967  0.093743771
##  [8,]  0.4405826955 0.084164103  0.0067537154  0.0105743959  0.095259640
##  [9,]  1.0000000000 0.080821393  0.0455421442 -0.0026594129  0.112751427
## [10,]  0.0808213931 1.000000000  0.1663297767  0.1445551668  0.136203735
## [11,]  0.0455421442 0.166329777  1.0000000000  0.1958705568  0.076128950
## [12,] -0.0026594129 0.144555167  0.1958705568  1.0000000000  0.133946009
## [13,]  0.1127514271 0.136203735  0.0761289500  0.1339460088  1.000000000
fitACE$rC$result
##               [,1]         [,2]         [,3]        [,4]         [,5]         [,6]         [,7]         [,8]
##  [1,]  1.000000000  0.321626568  0.093811451 0.132966039  0.182176400  0.274657178  0.200140060  0.199428459
##  [2,]  0.321626568  1.000000000  0.170156226 0.042693874  0.195108932  0.321661740  0.200192785  0.203467717
##  [3,]  0.093811451  0.170156226  1.000000000 0.101834570  0.025325281  0.021134575 -0.024655776 -0.022408638
##  [4,]  0.132966039  0.042693874  0.101834570 1.000000000  0.146132055  0.068696820  0.205819591  0.205903149
##  [5,]  0.182176400  0.195108932  0.025325281 0.146132055  1.000000000  0.296702276  0.321155427  0.320006065
##  [6,]  0.274657178  0.321661740  0.021134575 0.068696820  0.296702276  1.000000000  0.319324900  0.323041921
##  [7,]  0.200140060  0.200192785 -0.024655776 0.205819591  0.321155427  0.319324900  1.000000000  0.431414344
##  [8,]  0.199428459  0.203467717 -0.022408638 0.205903149  0.320006065  0.323041921  0.431414344  1.000000000
##  [9,]  0.235350991  0.155365389 -0.056043250 0.220820104  0.195524916  0.292119066  0.379007854  0.378428650
## [10,]  0.090775769  0.155075462  0.078681044 0.115605221  0.082676530  0.116895107  0.124999226  0.126071819
## [11,]  0.093524066  0.051703282  0.096095124 0.031396256 -0.014142353  0.044563347  0.021228561  0.020249001
## [12,] -0.010463913 -0.010292075  0.112963307 0.087500675  0.100585165 -0.017707366  0.020904897  0.022542547
## [13,]  0.038091146  0.070998079  0.096221851 0.222385361  0.056112229  0.096168334  0.137173719  0.137175431
##                [,9]       [,10]        [,11]         [,12]       [,13]
##  [1,]  0.2353509914 0.090775769  0.093524066 -0.0104639132 0.038091146
##  [2,]  0.1553653892 0.155075462  0.051703282 -0.0102920748 0.070998079
##  [3,] -0.0560432500 0.078681044  0.096095124  0.1129633074 0.096221851
##  [4,]  0.2208201037 0.115605221  0.031396256  0.0875006755 0.222385361
##  [5,]  0.1955249160 0.082676530 -0.014142353  0.1005851652 0.056112229
##  [6,]  0.2921190662 0.116895107  0.044563347 -0.0177073655 0.096168334
##  [7,]  0.3790078542 0.124999226  0.021228561  0.0209048965 0.137173719
##  [8,]  0.3784286498 0.126071819  0.020249001  0.0225425466 0.137175431
##  [9,]  1.0000000000 0.123821569  0.053812025  0.0054172996 0.158570321
## [10,]  0.1238215693 1.000000000  0.292185240  0.2953708544 0.169027192
## [11,]  0.0538120254 0.292185240  1.000000000  0.3006653408 0.114987042
## [12,]  0.0054172996 0.295370854  0.300665341  1.0000000000 0.114731966
## [13,]  0.1585703207 0.169027192  0.114987042  0.1147319664 1.000000000
fitACE$rE$result
##                [,1]         [,2]         [,3]         [,4]         [,5]         [,6]          [,7]          [,8]
##  [1,]  1.0000000000 0.0088438226 -0.034176136 -0.033321079  0.159893210  0.172485364  0.1739689281  0.1779318889
##  [2,]  0.0088438226 1.0000000000  0.235081925  0.361520126  0.105997896  0.163607535  0.0894530402  0.0989845797
##  [3,] -0.0341761356 0.2350819253  1.000000000  0.278635367 -0.155745118 -0.163866670 -0.2059871964 -0.2045110471
##  [4,] -0.0333210785 0.3615201261  0.278635367  1.000000000  0.013465890 -0.027737257  0.0537321191  0.0591482823
##  [5,]  0.1598932095 0.1059978959 -0.155745118  0.013465890  1.000000000  0.493801630  0.5356318645  0.5262911602
##  [6,]  0.1724853639 0.1636075348 -0.163866670 -0.027737257  0.493801630  1.000000000  0.6070440457  0.6289514273
##  [7,]  0.1739689281 0.0894530402 -0.205987196  0.053732119  0.535631865  0.607044046  1.0000000000  0.9989515727
##  [8,]  0.1779318889 0.0989845797 -0.204511047  0.059148282  0.526291160  0.628951427  0.9989515727  1.0000000000
##  [9,]  0.1324350760 0.0402741914 -0.168696403  0.097526390  0.325599616  0.373879093  0.7308904185  0.7411082468
## [10,] -0.0371242759 0.0965615594  0.049907928  0.146809567 -0.032979184 -0.044489677  0.0036173845  0.0039412555
## [11,]  0.0032276426 0.1597575459  0.078536178  0.188193900 -0.054399602 -0.046507648 -0.0582083660 -0.0552420281
## [12,] -0.0645675344 0.1898576241  0.129200787  0.247506067 -0.063868184 -0.066401285 -0.0738561966 -0.0722563288
## [13,] -0.1668259199 0.1345026818  0.226730740  0.340421653 -0.094482160 -0.078093421 -0.0594901109 -0.0599185374
##                [,9]         [,10]         [,11]        [,12]        [,13]
##  [1,]  0.1324350760 -0.0371242759  0.0032276426 -0.064567534 -0.166825920
##  [2,]  0.0402741914  0.0965615594  0.1597575459  0.189857624  0.134502682
##  [3,] -0.1686964030  0.0499079276  0.0785361775  0.129200787  0.226730740
##  [4,]  0.0975263902  0.1468095673  0.1881938996  0.247506067  0.340421653
##  [5,]  0.3255996156 -0.0329791838 -0.0543996023 -0.063868184 -0.094482160
##  [6,]  0.3738790925 -0.0444896771 -0.0465076478 -0.066401285 -0.078093421
##  [7,]  0.7308904185  0.0036173845 -0.0582083660 -0.073856197 -0.059490111
##  [8,]  0.7411082468  0.0039412555 -0.0552420281 -0.072256329 -0.059918537
##  [9,]  1.0000000000  0.0086079041 -0.0198583037 -0.070466847 -0.038369948
## [10,]  0.0086079041  1.0000000000  0.0717874903  0.045798928  0.122106397
## [11,] -0.0198583037  0.0717874903  1.0000000000 -0.029828975  0.139585408
## [12,] -0.0704668473  0.0457989280 -0.0298289748  1.000000000  0.224938731
## [13,] -0.0383699478  0.1221063967  0.1395854077  0.224938731  1.000000000
fitACE$rP$result ### <--- pheno corrs
##               [,1]        [,2]         [,3]        [,4]         [,5]           [,6]         [,7]         [,8]
##  [1,]  1.000000000 0.159587330  0.021893410 0.052173151  0.168399568  0.22732330812  0.184814143  0.185372010
##  [2,]  0.159587330 1.000000000  0.206718783 0.206713142  0.156355138  0.24861906834  0.150891252  0.156835226
##  [3,]  0.021893410 0.206718783  1.000000000 0.199163525 -0.064071911 -0.07002442084 -0.105262880 -0.102976437
##  [4,]  0.052173151 0.206713142  0.199163525 1.000000000  0.086276185  0.03169327040  0.140618166  0.143233919
##  [5,]  0.168399568 0.156355138 -0.064071911 0.086276185  1.000000000  0.37279398569  0.396846410  0.392516654
##  [6,]  0.227323308 0.248619068 -0.070024421 0.031693270  0.372793986  1.00000000000  0.420433812  0.430486209
##  [7,]  0.184814143 0.150891252 -0.105262880 0.140618166  0.396846410  0.42043381217  1.000000000  0.621290524
##  [8,]  0.185372010 0.156835226 -0.102976437 0.143233919  0.392516654  0.43048620881  0.621290524  1.000000000
##  [9,]  0.187441186 0.103507035 -0.105617112 0.167140517  0.243001158  0.32313131119  0.501548290  0.504195418
## [10,]  0.018378811 0.112393907  0.060886001 0.122049619  0.027099168  0.03270469018  0.063608638  0.063814628
## [11,]  0.049470628 0.110455422  0.088298276 0.118565411 -0.032118747  0.00052148602 -0.012345844 -0.011470539
## [12,] -0.034478269 0.094993245  0.119224808 0.156836297  0.023603537 -0.03662708328 -0.016450389 -0.014751115
## [13,] -0.081159258 0.092277899  0.170486203 0.271806041 -0.015010921  0.00907714554  0.046613056  0.047193019
##               [,9]       [,10]          [,11]        [,12]         [,13]
##  [1,]  0.187441186 0.018378811  0.04947062760 -0.034478269 -0.0811592584
##  [2,]  0.103507035 0.112393907  0.11045542160  0.094993245  0.0922778985
##  [3,] -0.105617112 0.060886001  0.08829827636  0.119224808  0.1704862031
##  [4,]  0.167140517 0.122049619  0.11856541081  0.156836297  0.2718060414
##  [5,]  0.243001158 0.027099168 -0.03211874730  0.023603537 -0.0150109214
##  [6,]  0.323131311 0.032704690  0.00052148602 -0.036627083  0.0090771455
##  [7,]  0.501548290 0.063608638 -0.01234584401 -0.016450389  0.0466130557
##  [8,]  0.504195418 0.063814628 -0.01147053880 -0.014751115  0.0471930189
##  [9,]  1.000000000 0.062321166  0.02224611397 -0.025489030  0.0634672394
## [10,]  0.062321166 1.000000000  0.14847203922  0.132059961  0.1361141050
## [11,]  0.022246114 0.148472039  1.00000000000  0.117576933  0.1160379244
## [12,] -0.025489030 0.132059961  0.11757693274  1.000000000  0.1725072738
## [13,]  0.063467239 0.136114105  0.11603792441  0.172507274  1.0000000000
### combine both into one matrix and plot

plot correlations

pheno<- as.matrix(fitACE$rP$result)
genetic<- as.matrix(fitACE$rA$result)


full <- matrix(NA, nrow = 13, ncol = 13)
full[upper.tri(full)]<- pheno[upper.tri(pheno)]
full[lower.tri(full)]<- genetic[lower.tri(genetic)]
diag(full)<- 1

colnames(full)<- abcd_vars
row.names(full)<- colnames(full)

corrplot(full, method = "color",
         type = "full",  number.cex = .6,
         addCoef.col = "black", 
         tl.col = "black", tl.srt = 90) 

table of all descriptives

means <- abcd %>%
  dplyr::summarize(meanweekend_dur_mcq_wave_2 = mean(weekend_dur_mcq_wave_2, na.rm = TRUE),
  meanweekday_dur_mcq_wave_2 = mean(weekday_dur_mcq_wave_2, na.rm = TRUE),
  meanchronotype_wave_2 = mean(chronotype_wave_2, na.rm = TRUE),
  meansocial_jet_lag_wave_2 = mean(social_jet_lag_wave_2, na.rm = TRUE),
  meanavg_weekend_dur = mean(avg_weekend_dur,na.rm=T),
  meanavg_weekday_dur = mean(avg_weekday_dur, na.rm=T),
  meanavg_weekend_effic = mean(avg_weekend_effic, na.rm=T),
  meanavg_weekday_effic = mean(avg_weekday_effic, na.rm=T),
  meanvariability = mean(variability, na.rm=T),
  meancbcl_scr_syn_attention_r_wave_2 = mean(cbcl_scr_syn_attention_r_wave_2, na.rm = TRUE),
  meancbcl_scr_syn_internal_r_wave_2 = mean(cbcl_scr_syn_internal_r_wave_2, na.rm = TRUE),
  meancbcl_scr_syn_external_r_wave_2 = mean(cbcl_scr_syn_external_r_wave_2, na.rm = TRUE),
  meantotal_score_wave_2 = mean(total_score_wave_2, na.rm = TRUE)) %>%
  t %>%
  as.data.frame %>%
  rename(Mean=V1)

sds <- abcd %>%
  dplyr::summarize(sdweekend_dur_mcq_wave_2 = sd(weekend_dur_mcq_wave_2, na.rm = TRUE),
  sdweekday_dur_mcq_wave_2 = sd(weekday_dur_mcq_wave_2, na.rm = TRUE),
  sdchronotype_wave_2 = sd(chronotype_wave_2, na.rm = TRUE),
  sdsocial_jet_lag_wave_2 = sd(social_jet_lag_wave_2, na.rm = TRUE),
  sd(avg_weekend_dur,na.rm=T),
  SDavg_weekday_dur = sd(avg_weekday_dur, na.rm=T),
  SDavg_weekend_effic = sd(avg_weekend_effic, na.rm=T),
  SDavg_weekday_effic = sd(avg_weekday_effic, na.rm=T),
  SDvariability = sd(variability, na.rm=T),
  SDcbcl_scr_syn_attention_r_wave_2 = sd(cbcl_scr_syn_attention_r_wave_2, na.rm = TRUE),
  SDcbcl_scr_syn_internal_r_wave_2 = sd(cbcl_scr_syn_internal_r_wave_2, na.rm = TRUE),
  SDcbcl_scr_syn_external_r_wave_2 = sd(cbcl_scr_syn_external_r_wave_2, na.rm = TRUE),
  SDtotal_score_wave_2 = sd(total_score_wave_2, na.rm = TRUE)) %>%
  t %>%
  as.data.frame %>%
  rename(SD=V1)

Ns <- abcd %>%
  dplyr::summarize(Nweekend_dur_mcq_wave_2 = sum(!is.na(weekend_dur_mcq_wave_2)),
  Nweekday_dur_mcq_wave_2 = sum(!is.na(weekday_dur_mcq_wave_2)),
  Nchronotype_wave_2 = sum(!is.na(chronotype_wave_2)),
  Nsocial_jet_lag_wave_2 = sum(!is.na(social_jet_lag_wave_2)),
  Navg_weekend_dur = sum(!is.na(avg_weekend_dur)),
  Navg_weekday_dur = sum(!is.na(avg_weekday_dur)),
  Navg_weekend_effic = sum(!is.na(avg_weekend_effic)),
  Navg_weekday_effic = sum(!is.na(avg_weekday_effic)),
  Nvariability = sum(!is.na(variability)),
  Ncbcl_scr_syn_attention_r_wave_2 = sum(!is.na(cbcl_scr_syn_attention_r_wave_2)),
  Ncbcl_scr_syn_internal_r_wave_2 = sum(!is.na(cbcl_scr_syn_internal_r_wave_2)),
  Ncbcl_scr_syn_external_r_wave_2 = sum(!is.na(cbcl_scr_syn_external_r_wave_2)),
  Ntotal_score_wave_2 = sum(!is.na(total_score_wave_2))) %>%
  t %>%
  as.data.frame %>%
  rename(N=V1)



descriptives_abcd<- cbind(means,sds, Ns, mz.cors, dz.cors)

row.names(descriptives_abcd)<- abcd_vars

knitr::kable(
  descriptives_abcd
)
Mean SD N MZCorrs DZCorrs
Weekend Duration 9.32673976 1.91352646 10054 0.28100095 0.20758001
Weekday Duration 8.54452457 1.41427930 10054 0.38994938 0.23888195
Chronotype 15.32381480 11.34404908 8771 0.34241742 0.34005529
Social Jet Lag 1.85889661 1.60919017 10054 0.63493913 0.53983471
Weekend Duration* 8.41128426 0.97318906 4755 0.68461260 0.55797745
Weekday Duration* 8.36232514 0.76430909 4793 0.67806418 0.49964729
Weekend Efficiency* 0.97527427 0.02128015 4755 0.41163490 0.33735259
Weekday Efficiency* 0.97635205 0.01506695 4793 0.25908447 0.22885810
Variability* 1.23736890 0.54335484 4788 0.33078063 0.50273038
Attention 2.70018553 3.30929242 8085 0.43146668 0.22926676
Internalizing 4.94038343 5.61664207 8085 0.41527737 0.36062441
Externalizing 3.92875696 5.52128001 8085 0.64468363 0.26270670
Prodromal 1.55905512 2.78768162 10414 0.23694189 0.28529727