library(OpenMx)
# Create table to store all ACE estimates
= NULL res
# ----------------------------------------------------------------------------------------------------------------------
# Script: 00_ACEvc.R Author: Sarah Medland Date: 29 02 2020
# Twin Univariate ACE model to estimate causes of variation
# across multiple groups Matrix style model - Raw data -
# Continuous data
# -------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
# Set Working Directory
# Clear Workspace rm(list=ls())
# Load Libraries & Options
library(OpenMx)
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE DATA
# Load Data
<- read.table("twinsibData.txt", header = T)
df
<- df
dfBin $Twin1 <- ifelse(dfBin$Twin1 > 0, 1, 0)
dfBin$Twin2 <- ifelse(dfBin$Twin2 > 0, 1, 0)
dfBin$Sib <- ifelse(dfBin$Sib > 0, 1, 0)
dfBin
# Check the data
table(dfBin$Twin1)
##
## 0 1
## 921 1079
table(dfBin$Twin2)
##
## 0 1
## 940 1060
table(dfBin$Sib)
##
## 0 1
## 949 1051
# Ensure that the data are encoded as an ordered factor
$Twin1 <- mxFactor(dfBin$Twin1, levels = 0:1)
dfBin$Twin2 <- mxFactor(dfBin$Twin2, levels = 0:1)
dfBin$Sib <- mxFactor(dfBin$Sib, levels = 0:1)
dfBin
<- subset(dfBin, zygosity == 1)
mzData <- subset(dfBin, zygosity == 2)
dzData
# Select Variables for Analysis
<- 1 # number of traits
nv <- 2 # number of individuals
nt <- 1 # number of thresholds
nTH <- nv * nt # number of total variables
ntv <- c("Twin1", "Twin2") # name of traits
selVars <- c("age1", "age2", "sex1", "sex2") # list of covariates
covVars
# Set Starting Values
<- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe <- 0 # start for the threshold
ThrVals
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = FALSE,
intercept values = 0, labels = "interC", name = "intercept")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaS values = 0, labels = "betaS", name = "bS")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaA values = 0, labels = "betaA", name = "bA")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defSex labels = c("data.sex1", "data.sex2"), name = "Sex")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defAge labels = c("data.age1", "data.age2"), name = "Age")
<- mxAlgebra(expression = intercept + Sex %x% bS + Age %x%
expMean name = "expMean")
bA, <- mxMatrix(type = "Full", nrow = nTH, ncol = ntv, free = TRUE,
expThr values = ThrVals, labels = paste("th", 1:nTH, sep = ""),
name = "expThr")
# Create Matrices for Variance Components
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covA values = sVa, label = "VA11", name = "VA")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covC values = sVc, label = "VC11", name = "VC")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covE values = sVe, label = "VE11", name = "VE")
<- mxConstraint(VA + VC + VE == 1, name = "cons") # Constraint so that the variance equals 1 (necessary for identification)
cons
# Create Algebra for expected Variance/Covariance Matrices
# in MZ & DZ twins
<- mxAlgebra(expression = VA + VC + VE, name = "V")
covP <- mxAlgebra(expression = VA + VC, name = "cMZ")
covMZ <- mxAlgebra(expression = 0.5 %x% VA + VC, name = "cDZ")
covDZ <- mxAlgebra(expression = rbind(cbind(V, cMZ), cbind(t(cMZ),
expCovMZ name = "expCovMZ")
V)), <- mxAlgebra(expression = rbind(cbind(V, cDZ), cbind(t(cDZ),
expCovDZ name = "expCovDZ")
V)),
# Create Data Objects for Multiple Groups
<- mxData(observed = mzData, type = "raw")
dataMZ <- mxData(observed = dzData, type = "raw")
dataDZ
# Create Expectation Objects for Multiple Groups
<- mxExpectationNormal(covariance = "expCovMZ", means = "expMean",
expMZ thresholds = "expThr", dimnames = selVars)
<- mxExpectationNormal(covariance = "expCovDZ", means = "expMean",
expDZ thresholds = "expThr", dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex)
defs <- list(intercept, betaS, betaA, expThr, covA, covC, covE,
pars
covP)<- mxModel(defs, pars, expMean, covMZ, expCovMZ, dataMZ,
modelMZ name = "MZ")
expMZ, funML, <- mxModel(defs, pars, expMean, covDZ, expCovDZ, dataDZ,
modelDZ name = "DZ")
expDZ, funML, <- mxFitFunctionMultigroup(c("MZ", "DZ"))
multi
# Create Algebra for Variance Components
<- rep("VC", nv)
rowVC <- rep(c("VA", "VC", "VE", "SA", "SC", "SE"), each = nv)
colVC <- mxAlgebra(expression = cbind(VA, VC, VE, VA/V, VC/V,
estVC /V), name = "VarC", dimnames = list(rowVC, colVC))
VE
# Create Confidence Interval Objects
<- mxCI("VarC[1,4:6]")
ciACE
# Build Model with Confidence Intervals
<- mxModel("ACEvc", modelMZ, modelDZ, multi, pars, estVC,
modelACE
ciACE, cons)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T)
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of ACEvc
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 betaS bS 1 1 0.08950213 0.04254163
## 2 betaA bA 1 1 0.19623496 0.03348751
## 3 th1 expThr 1 1 3.87646753 0.66993395
## 4 VA11 VA 1 1 0.53811726 0.10498124
## 5 VC11 VC 1 1 0.17754657 0.09121863
## 6 VE11 VE 1 1 0.28433617 0.02999880
##
## confidence intervals:
## lbound estimate ubound note
## ACEvc.VarC[1,4] 0.333397704 0.5381173 0.7456443
## ACEvc.VarC[1,5] -0.006892831 0.1775466 0.3512009
## ACEvc.VarC[1,6] 0.230272307 0.2843362 0.3471491
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 3995 5119.589
## Saturated: NA NA NA
## Independence: NA NA NA
## Number of observations/statistics: 2000/4001
##
## Constraint 'cons' contributes 1 observed statistic.
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: -2870.411 5131.589 5131.631
## BIC: -25246.016 5165.195 5146.132
## CFI: NA
## TLI: 1 (also known as NNFI)
## RMSEA: 0 [95% CI (NA, NA)]
## Prob(RMSEA <= 0.05): NA
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:32:22
## Wall clock time: 13.05658 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
# Some different ways to extract output
$output$estimate fitACE
## betaS betaA th1 VA11 VC11 VE11
## 0.08950213 0.19623496 3.87646753 0.53811726 0.17754657 0.28433617
$VarC$result fitACE
## VA VC VE SA SC SE
## VC 0.5381173 0.1775466 0.2843362 0.5381173 0.1775466 0.2843362
$output$confidenceIntervals fitACE
## lbound estimate ubound
## ACEvc.VarC[1,4] 0.333397704 0.5381173 0.7456443
## ACEvc.VarC[1,5] -0.006892831 0.1775466 0.3512009
## ACEvc.VarC[1,6] 0.230272307 0.2843362 0.3471491
= rbind(res, c("Twin", fitACE$output$confidenceIntervals[1,
res $output$confidenceIntervals[2, ], fitACE$output$confidenceIntervals[3,
], fitACE ]))
# ----------------------------------------------------------------------------------------------------------------------
# Script: 01_extrasib.R Author: Sarah Medland Date: 29 02
# 2020 Twin Univariate ACE model to estimate causes of
# variation across multiple groups Matrix style model - Raw
# data - Continuous data
# -------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
# Set Working Directory
# Clear Workspace rm(list=ls())
# Load Libraries & Options
library(OpenMx)
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE DATA
# Load Data
<- read.table("twinsibData.txt", header = T)
df
<- df
dfBin $Twin1 <- ifelse(dfBin$Twin1 > 0, 1, 0)
dfBin$Twin2 <- ifelse(dfBin$Twin2 > 0, 1, 0)
dfBin$Sib <- ifelse(dfBin$Sib > 0, 1, 0)
dfBin
$Twin1 <- mxFactor(dfBin$Twin1, levels = 0:1)
dfBin$Twin2 <- mxFactor(dfBin$Twin2, levels = 0:1)
dfBin$Sib <- mxFactor(dfBin$Sib, levels = 0:1)
dfBin
<- subset(dfBin, zygosity == 1)
mzData <- subset(dfBin, zygosity == 2)
dzData
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 1 # number of thresholds
nTH <- nv * nt # number of total variables
ntv <- c("Twin1", "Twin2", "Sib") # name of traits
selVars <- c("age1", "age2", "age3", "sex1", "sex2", "sex3") # list of covariates
covVars
# Set Starting Values
<- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe <- 0 # start for the threshold
ThrVals
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = FALSE,
intercept values = 0, labels = "interC", name = "intercept")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaS values = 0, labels = "betaS", name = "bS")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaA values = 0, labels = "betaA", name = "bA")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defSex labels = c("data.sex1", "data.sex2", "data.sex3"), name = "Sex")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defAge labels = c("data.age1", "data.age2", "data.age3"), name = "Age")
<- mxAlgebra(expression = intercept + Sex %x% bS + Age %x%
expMean name = "expMean")
bA, <- mxMatrix("Full", nTH, ntv, free = TRUE, values = ThrVals,
expThr labels = paste("th", 1:nTH, sep = ""), name = "expThr")
# Create Matrices for Variance Components
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covA values = sVa, label = "VA11", name = "VA")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covC values = sVc, label = "VC11", name = "VC")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covE values = sVe, label = "VE11", name = "VE")
<- mxConstraint(VA + VC + VE == 1, name = "cons") # Constraint so that the variance equals 1 (necessary for identification)
cons
# Create Algebra for expected Variance/Covariance Matrices
# in MZ & DZ twins
<- mxAlgebra(expression = VA + VC + VE, name = "V")
covP <- mxAlgebra(expression = VA + VC, name = "cMZ")
covMZ <- mxAlgebra(expression = 0.5 %x% VA + VC, name = "cDZ")
covDZ <- mxAlgebra(expression = rbind(cbind(V, cMZ, cDZ),
expCovMZ cbind(t(cMZ), V, cDZ), cbind(t(cDZ), t(cDZ), V)), name = "expCovMZ")
<- mxAlgebra(expression = rbind(cbind(V, cDZ, cDZ),
expCovDZ cbind(t(cDZ), V, cDZ), cbind(t(cDZ), t(cDZ), V)), name = "expCovDZ")
# Create Data Objects for Multiple Groups
<- mxData(observed = mzData, type = "raw")
dataMZ <- mxData(observed = dzData, type = "raw")
dataDZ
# Create Expectation Objects for Multiple Groups
<- mxExpectationNormal(covariance = "expCovMZ", means = "expMean",
expMZ thresholds = "expThr", dimnames = selVars)
<- mxExpectationNormal(covariance = "expCovDZ", means = "expMean",
expDZ thresholds = "expThr", dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex)
defs <- list(intercept, betaS, betaA, expThr, covA, covC, covE,
pars
covP)<- mxModel(defs, pars, expMean, covMZ, covDZ, expCovMZ,
modelMZ name = "MZ")
dataMZ, expMZ, funML, <- mxModel(defs, pars, expMean, covDZ, expCovDZ, dataDZ,
modelDZ name = "DZ")
expDZ, funML, <- mxFitFunctionMultigroup(c("MZ", "DZ"))
multi
# Create Algebra for Variance Components
<- rep("VC", nv)
rowVC <- rep(c("VA", "VC", "VE", "SA", "SC", "SE"), each = nv)
colVC <- mxAlgebra(expression = cbind(VA, VC, VE, VA/V, VC/V,
estVC /V), name = "VarC", dimnames = list(rowVC, colVC))
VE
# Create Confidence Interval Objects
<- mxCI("VarC[1,4:6]")
ciACE
# Build Model with Confidence Intervals
<- mxModel("ACEsib", modelMZ, modelDZ, multi, pars,
modelACE
estVC, ciACE, cons)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T)
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of ACEsib
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 betaS bS 1 1 0.1179450 0.030981258
## 2 betaA bA 1 1 0.1049937 0.007750931
## 3 th1 expThr 1 1 2.0778973 0.156340424
## 4 VA11 VA 1 1 0.2543580 0.071152596 !
## 5 VC11 VC 1 1 0.4331444 0.049126864
## 6 VE11 VE 1 1 0.3124976 0.031818950
##
## confidence intervals:
## lbound estimate ubound note
## ACEsib.VarC[1,4] 0.1130752 0.2543580 0.3913480
## ACEsib.VarC[1,5] 0.3349206 0.4331444 0.5283482
## ACEsib.VarC[1,6] 0.2537166 0.3124976 0.3784697
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 5995 7428.427
## Saturated: NA NA NA
## Independence: NA NA NA
## Number of observations/statistics: 2000/6001
##
## Constraint 'cons' contributes 1 observed statistic.
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: -4561.573 7440.427 7440.469
## BIC: -38138.983 7474.032 7454.970
## CFI: NA
## TLI: 1 (also known as NNFI)
## RMSEA: 0 [95% CI (NA, NA)]
## Prob(RMSEA <= 0.05): NA
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:32:57
## Wall clock time: 35.15035 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
= rbind(res, c("Twin_sib", fitACE$output$confidenceIntervals[1,
res $output$confidenceIntervals[2, ], fitACE$output$confidenceIntervals[3,
], fitACE ]))
# ----------------------------------------------------------------------------------------------------------------------
# Script: extrasib2.R Author: Sarah Medland Date: 29 02
# 2020 Twin Univariate ACE model to estimate causes of
# variation across multiple groups Matrix style model - Raw
# data - Continuous data
# -------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
# Set Working Directory
# Clear Workspace rm(list=ls())
# Load Libraries & Options
library(OpenMx)
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE DATA
# Load Data
<- read.table("twinsibData.txt", header = T)
df
<- df
dfBin $Twin1 <- ifelse(dfBin$Twin1 > 0, 1, 0)
dfBin$Twin2 <- ifelse(dfBin$Twin2 > 0, 1, 0)
dfBin$Sib <- ifelse(dfBin$Sib > 0, 1, 0)
dfBin
$Twin1 <- mxFactor(dfBin$Twin1, levels = 0:1)
dfBin$Twin2 <- mxFactor(dfBin$Twin2, levels = 0:1)
dfBin$Sib <- mxFactor(dfBin$Sib, levels = 0:1)
dfBin
<- subset(dfBin, zygosity == 1)
mzData <- subset(dfBin, zygosity == 2)
dzData
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 1 # number of thresholds
nTH <- nv * nt # number of total variables
ntv <- c("Twin1", "Twin2", "Sib") # name of traits
selVars <- c("age1", "age2", "age3", "sex1", "sex2", "sex3") # list of covariates
covVars
# Set Starting Values
<- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe <- 0 # start for the threshold
ThrVals
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = FALSE,
intercept values = 0, labels = "interC", name = "intercept")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaS values = 0, labels = "betaS", name = "bS")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaA values = 0, labels = "betaA", name = "bA")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defSex labels = c("data.sex1", "data.sex2", "data.sex3"), name = "Sex")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defAge labels = c("data.age1", "data.age2", "data.age3"), name = "Age")
<- mxAlgebra(expression = intercept + Sex %x% bS + Age %x%
expMean name = "expMean")
bA, <- mxMatrix("Full", nrow = nTH, ncol = ntv, free = TRUE,
expThr values = ThrVals, labels = paste("th", 1:nTH, sep = ""),
name = "expThr")
# Create Matrices for Variance Components
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covA values = sVa, label = "VA11", name = "VA")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covC values = sVc, label = "VC11", name = "VC")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covE values = sVe, label = "VE11", name = "VE")
<- mxConstraint(VA + VC + VE == 1, name = "cons") # Constraint so that the variance equals 1 (necessary for identification)
cons
# Create Algebra for expected Variance/Covariance Matrices
# in MZ & DZ twins
<- mxAlgebra(expression = VA + VC + VE, name = "V")
covP <- mxMatrix(type = "Symm", nrow = nt, ncol = nt, free = FALSE,
relMZ values = c(1, 1, 0.5, 1, 0.5, 1), name = "rAmz")
<- mxMatrix(type = "Symm", nrow = nt, ncol = nt, free = FALSE,
relDZ values = c(1, 0.5, 0.5, 1, 0.5, 1), name = "rAdz")
<- mxMatrix(type = "Unit", nrow = nt, ncol = nt, free = FALSE,
relC name = "rC")
<- mxMatrix(type = "Iden", nrow = nt, ncol = nt, free = FALSE,
relE name = "rE")
<- mxAlgebra(expression = rAmz %x% VA + rC %x% VC +
expCovMZ %x% VE, name = "expCovMZ")
rE <- mxAlgebra(expression = rAdz %x% VA + rC %x% VC +
expCovDZ %x% VE, name = "expCovDZ")
rE
# Create Data Objects for Multiple Groups
<- mxData(observed = mzData, type = "raw")
dataMZ <- mxData(observed = dzData, type = "raw")
dataDZ
# Create Expectation Objects for Multiple Groups
<- mxExpectationNormal(covariance = "expCovMZ", means = "expMean",
expMZ thresholds = "expThr", dimnames = selVars)
<- mxExpectationNormal(covariance = "expCovDZ", means = "expMean",
expDZ thresholds = "expThr", dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex)
defs <- list(intercept, betaS, betaA, expThr, covA, covC, covE,
pars
covP, relMZ, relDZ, relC, relE)<- mxModel(defs, pars, expMean, expCovMZ, dataMZ, expMZ,
modelMZ name = "MZ")
funML, <- mxModel(defs, pars, expMean, expCovDZ, dataDZ, expDZ,
modelDZ name = "DZ")
funML, <- mxFitFunctionMultigroup(c("MZ", "DZ"))
multi
# Create Algebra for Variance Components
<- rep("VC", nv)
rowVC <- rep(c("VA", "VC", "VE", "SA", "SC", "SE"), each = nv)
colVC <- mxAlgebra(expression = cbind(VA, VC, VE, VA/V, VC/V,
estVC /V), name = "VarC", dimnames = list(rowVC, colVC))
VE
# Create Confidence Interval Objects
<- mxCI("VarC[1,4:6]")
ciACE
# Build Model with Confidence Intervals
<- mxModel("ACEsib_alt", modelMZ, modelDZ, multi, pars,
modelACE
estVC, ciACE, cons)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T)
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of ACEsib_alt
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 betaS bS 1 1 0.1179450 0.030981258
## 2 betaA bA 1 1 0.1049937 0.007750931
## 3 th1 expThr 1 1 2.0778973 0.156340424
## 4 VA11 VA 1 1 0.2543580 0.071152596 !
## 5 VC11 VC 1 1 0.4331444 0.049126864
## 6 VE11 VE 1 1 0.3124976 0.031818950
##
## confidence intervals:
## lbound estimate ubound note
## ACEsib_alt.VarC[1,4] 0.1130752 0.2543580 0.3913480
## ACEsib_alt.VarC[1,5] 0.3349206 0.4331444 0.5283482
## ACEsib_alt.VarC[1,6] 0.2537166 0.3124976 0.3784697
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 5995 7428.427
## Saturated: NA NA NA
## Independence: NA NA NA
## Number of observations/statistics: 2000/6001
##
## Constraint 'cons' contributes 1 observed statistic.
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: -4561.573 7440.427 7440.469
## BIC: -38138.983 7474.032 7454.970
## CFI: NA
## TLI: 1 (also known as NNFI)
## RMSEA: 0 [95% CI (NA, NA)]
## Prob(RMSEA <= 0.05): NA
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:33:33
## Wall clock time: 35.12386 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
= rbind(res, c("Twin_sib2", fitACE$output$confidenceIntervals[1,
res $output$confidenceIntervals[2, ], fitACE$output$confidenceIntervals[3,
], fitACE ]))
# ----------------------------------------------------------------------------------------------------------------------
# Script: zygdef.R Author: Sarah Medland Date: 29 02 2020
# Twin Univariate ACE model to estimate causes of variation
# across multiple groups Matrix style model - Raw data -
# Continuous data
# -------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
# Set Working Directory
# Clear Workspace rm(list=ls())
# Load Libraries & Options
library(OpenMx)
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE DATA
# Load Data
<- read.table("twinsibData.txt", header = T)
df
<- df
dfBin $Twin1 <- ifelse(dfBin$Twin1 > 0, 1, 0)
dfBin$Twin2 <- ifelse(dfBin$Twin2 > 0, 1, 0)
dfBin$Sib <- ifelse(dfBin$Sib > 0, 1, 0)
dfBin
$Twin1 <- mxFactor(dfBin$Twin1, levels = 0:1)
dfBin$Twin2 <- mxFactor(dfBin$Twin2, levels = 0:1)
dfBin$Sib <- mxFactor(dfBin$Sib, levels = 0:1)
dfBin
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 1 # number of thresholds
nTH <- nv * nt # number of total variables
ntv <- c("Twin1", "Twin2", "Sib") # name of traits
selVars <- c("age1", "age2", "age3", "sex1", "sex2", "sex3") # list of covariates
covVars
# Set Starting Values
<- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe <- 0 # start for the threshold
ThrVals
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = FALSE,
intercept values = 0, labels = "interC", name = "intercept")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaS values = 0, labels = "betaS", name = "bS")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaA values = 0, labels = "betaA", name = "bA")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defSex labels = c("data.sex1", "data.sex2", "data.sex3"), name = "Sex")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defAge labels = c("data.age1", "data.age2", "data.age3"), name = "Age")
<- mxAlgebra(expression = intercept + Sex %x% bS + Age %x%
expMean name = "expMean")
bA, <- mxMatrix("Full", nrow = nTH, ncol = ntv, free = TRUE,
expThr values = ThrVals, labels = paste("th", 1:nTH, sep = ""),
name = "expThr")
# Create Matrices for Variance Components
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covA values = sVa, label = "VA11", name = "VA")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covC values = sVc, label = "VC11", name = "VC")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covE values = sVe, label = "VE11", name = "VE")
<- mxConstraint(VA + VC + VE == 1, name = "cons") # Constraint so that the variance equals 1 (necessary for identification)
cons
# Create Algebra for expected Variance/Covariance Matrices
# in MZ & DZ twins
<- mxAlgebra(expression = VA + VC + VE, name = "V")
covP <- mxMatrix(type = "Stand", nrow = ntv, ncol = nt, free = FALSE,
relA labels = c("data.zygT", "data.zygS", "data.zygS"), name = "rA")
<- mxMatrix(type = "Unit", nrow = ntv, ncol = nt, free = FALSE,
relC name = "rC")
<- mxMatrix(type = "Iden", nrow = ntv, ncol = nt, free = FALSE,
relE name = "rE")
<- mxAlgebra(expression = rA %x% VA + rC %x% VC + rE %x%
expCov name = "expCov")
VE,
# Create Data Object
<- mxData(observed = dfBin, type = "raw")
dataTW
# Create Expectation Objects
<- mxExpectationNormal(covariance = "expCov", means = "expMean",
expTW thresholds = "expThr", dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects
<- list(defAge, defSex, relA)
defs <- list(intercept, betaS, betaA, expThr, covA, covC, covE,
pars
covP, relC, relE)
# Create Algebra for Variance Components
<- rep("VC", nv)
rowVC <- rep(c("VA", "VC", "VE", "SA", "SC", "SE"), each = nv)
colVC <- mxAlgebra(expression = cbind(VA, VC, VE, VA/V, VC/V,
estVC /V), name = "VarC", dimnames = list(rowVC, colVC))
VE
# Create Confidence Interval Objects
<- mxCI("VarC[1,4:6]")
ciACE
# Build Model with Confidence Intervals
<- mxModel("zygdef", defs, pars, expMean, expCov, dataTW,
modelACE
expTW, funML, estVC, ciACE, cons)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T)
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of zygdef
##
## The model does not satisfy the first-order optimality conditions to the required accuracy, and no improved point for the merit function could be found during the final linesearch (Mx status RED)
##
## Your ordinal model may converge if you reduce mvnRelEps
## try: model <- mxOption(model, 'mvnRelEps', mxOption(model, 'mvnRelEps')/5)
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 betaS bS 1 1 0.1175745 0.032170065
## 2 betaA bA 1 1 0.1038757 0.008782796
## 3 th1 expThr 1 Twin1 2.0547080 0.175246885
## 4 VA11 VA 1 1 0.2460526 0.084154822 !
## 5 VC11 VC 1 1 0.4379598 0.055726869
## 6 VE11 VE 1 1 0.3159876 0.036761189 !
##
## confidence intervals:
## lbound estimate ubound note
## zygdef.VarC[1,4] 0.1123027 0.2460526 0.3914871
## zygdef.VarC[1,5] 0.3356056 0.4379598 0.5286203
## zygdef.VarC[1,6] 0.2536727 0.3159876 0.3789370
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 5995 7428.444
## Saturated: 9 5992 NA
## Independence: 6 5995 NA
## Number of observations/statistics: 2000/6001
##
## Constraint 'cons' contributes 1 observed statistic.
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: -4561.556 7440.444 7440.486
## BIC: -38138.966 7474.049 7454.987
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:34:12
## Wall clock time: 38.71569 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
= rbind(res, c("ZygVar", fitACE$output$confidenceIntervals[1,
res $output$confidenceIntervals[2, ], fitACE$output$confidenceIntervals[3,
], fitACE ]))
# ----------------------------------------------------------------------------------------------------------------------
# Script: 05_relatednessDZonlu.R Author: Sarah Medland
# Date: 29 02 2020 Twin Univariate ACE model to estimate
# causes of variation across multiple groups Matrix style
# model - Raw data - Continuous data
# -------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
# Set Working Directory
# Clear Workspace rm(list=ls())
# Load Libraries & Options
library(OpenMx)
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE DATA
# Load Data
<- read.table("DZonly10k.txt", header = T)
df
<- df
dfBin $Twin1 <- ifelse(dfBin$Twin1 > 0, 1, 0)
dfBin$Twin2 <- ifelse(dfBin$Twin2 > 0, 1, 0)
dfBin$Sib <- ifelse(dfBin$Sib > 0, 1, 0)
dfBin
$Twin1 <- mxFactor(dfBin$Twin1, levels = 0:1)
dfBin$Twin2 <- mxFactor(dfBin$Twin2, levels = 0:1)
dfBin$Sib <- mxFactor(dfBin$Sib, levels = 0:1)
dfBin
table(dfBin$Twin1)
##
## 0 1
## 4993 5007
table(dfBin$Twin2)
##
## 0 1
## 4997 5003
table(dfBin$Sib)
##
## 0 1
## 4982 5018
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 1 # number of thresholds
nTH <- nv * nt # number of total variables
ntv <- c("Twin1", "Twin2", "Sib") # name of traits
selVars <- c("age1", "age2", "age3", "sex1", "sex2", "sex3") # list of covariates
covVars
# Set Starting Values
<- 0.4 # start value for A
sVa <- 0.2 # start value for C
sVc <- 0.3 # start value for E
sVe <- 4 # start for the threshold
ThrVals
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = FALSE,
intercept values = 0, labels = "interC", name = "intercept")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaS values = 0.01, labels = "betaS", name = "bS")
<- mxMatrix(type = "Full", nrow = 1, ncol = nv, free = TRUE,
betaA values = 0.1, labels = "betaA", name = "bA")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defSex labels = c("data.sex1", "data.sex2", "data.sex3"), name = "Sex")
<- mxMatrix(type = "Full", nrow = 1, ncol = nt, free = FALSE,
defAge labels = c("data.age1", "data.age2", "data.age3"), name = "Age")
<- mxAlgebra(expression = intercept + Sex %x% bS + Age %x%
expMean name = "expMean")
bA, <- mxMatrix("Full", nrow = nTH, ncol = ntv, free = TRUE,
expThr values = ThrVals, labels = paste("th", 1:nTH, sep = ""),
name = "expThr")
# Create Matrices for Variance Components
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covA values = sVa, label = "VA11", name = "VA")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covC values = sVc, label = "VC11", name = "VC")
<- mxMatrix(type = "Symm", nrow = nv, ncol = nv, free = TRUE,
covE values = sVe, label = "VE11", name = "VE")
<- mxConstraint(VA + VC + VE == 1, name = "cons") # Constraint so that the variance equals 1 (necessary for identification)
cons
# Create Algebra for expected Variance/Covariance Matrices
# in MZ & DZ twins
<- mxAlgebra(expression = VA + VC + VE, name = "V")
covP <- mxMatrix(type = "Stand", nrow = nt, ncol = nt, free = FALSE,
relA labels = c("data.s1", "data.s2", "data.s3"), name = "rA")
<- mxMatrix(type = "Unit", nrow = nt, ncol = nt, free = FALSE,
relC name = "rC")
<- mxMatrix(type = "Iden", nrow = nt, ncol = nt, free = FALSE,
relE name = "rE")
<- mxAlgebra(expression = rA %x% VA + rC %x% VC + rE %x%
expCov name = "expCov")
VE,
# Create Data Objects for Multiple Groups
<- mxData(observed = dfBin, type = "raw")
dataTW
# Create Expectation Objects for Multiple Groups
<- mxExpectationNormal(covariance = "expCov", means = "expMean",
expTW thresholds = "expThr", dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex, relA)
defs <- list(intercept, betaS, betaA, expThr, covA, covC, covE,
pars
covP, relC, relE)
# Create Algebra for Variance Components
<- rep("VC", nv)
rowVC <- rep(c("VA", "VC", "VE", "SA", "SC", "SE"), each = nv)
colVC <- mxAlgebra(expression = cbind(VA, VC, VE, VA/V, VC/V,
estVC /V), name = "VarC", dimnames = list(rowVC, colVC))
VE
# Create Confidence Interval Objects
<- mxCI("VarC[1,4:6]")
ciACE
# Build Model with Confidence Intervals
<- mxModel("DZonly", defs, pars, expMean, expCov, dataTW,
modelACE
expTW, funML, estVC, ciACE, cons)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T) # This can take several minutes to run with CIs
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of DZonly
##
## The model does not satisfy the first-order optimality conditions to the required accuracy, and no improved point for the merit function could be found during the final linesearch (Mx status RED)
##
## Your ordinal model may converge if you reduce mvnRelEps
## try: model <- mxOption(model, 'mvnRelEps', mxOption(model, 'mvnRelEps')/5)
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 betaS bS 1 1 0.03054091 0.013243369
## 2 betaA bA 1 1 0.18054482 0.005609056
## 3 th1 expThr 1 Twin1 3.62451105 0.112415848
## 4 VA11 VA 1 1 0.01074084 0.236192647
## 5 VC11 VC 1 1 0.48503394 0.118483642
## 6 VE11 VE 1 1 0.50422521 0.118418721
##
## confidence intervals:
## lbound estimate ubound note
## DZonly.VarC[1,4] -0.4515270 0.01074084 0.4775872
## DZonly.VarC[1,5] 0.2502554 0.48503394 0.7158699
## DZonly.VarC[1,6] 0.2699095 0.50422521 0.7376346
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 29995 38192.36
## Saturated: 9 29992 NA
## Independence: 6 29995 NA
## Number of observations/statistics: 10000/30001
##
## Constraint 'cons' contributes 1 observed statistic.
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: -21797.64 38204.36 38204.36
## BIC: -238071.80 38247.62 38228.55
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:39:30
## Wall clock time: 275.1607 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
# mxOption(NULL,'Default optimizer', 'CSOLNP') fitACE <-
# mxTryHardOrdinal( modelACE, intervals=T )
= rbind(res, c("DZonly_GRM", fitACE$output$confidenceIntervals[1,
res $output$confidenceIntervals[2, ], fitACE$output$confidenceIntervals[3,
], fitACE ]))
<- function(x, y, upper, lower=upper, length=0.1,...){
error.bar arrows(x,upper, x, lower, angle=90, code=3, length=length, ...)
}
#png("A_C_estimates_allMethods_binary.png", width = 20, height = 15, units = "cm", res = 400 )
par(mfrow=c(1,2), mar=c(10,4,1,1))
#layout(matrix(c(1,2,2), 1, 2, byrow = TRUE))
plot( res[,3], pch=20, cex=2, ylim=c(-0.1,1), ylab="A", xaxt='n', xlab="", col="darkblue")
error.bar(1:7, y = as.numeric(res[,3]), upper = as.numeric(res[,4]), lower = as.numeric(res[,2]), col="darkblue" )
grid()
abline(h=0, lwd=2, col="black")
text(x = 1:7,
y = par("usr")[3] - 0.1,
labels = c("ACE twins", "Twins and Sibs", "Alternate twins and sibs", "Zygosity as definition variable", "Measured genetic relationship", "DZ only", "AE unrelated - GREML"),
xpd = NA,
## Rotate the labels by 35 degrees.
srt = 45,
cex = 0.9, adj=1)
#axis(1, at = c(1:7), las=2, labels = c("ACE twins", "Twins and Sibs", "Alternate twins and sibs", "Zygosity as definition variable", "Measured genetic relationship", "DZ only", "AE unrelated - GREML"), )
plot( res[,6], pch=20, cex=2, ylim=c(-0.1,1), ylab="C", xaxt='n', xlab="", col="goldenrod4")
error.bar(1:7, y = as.numeric(res[,6]), upper = as.numeric(res[,7]), lower = as.numeric(res[,5]), col="goldenrod4" )
grid()
abline(h=0, lwd=2, col= "black")
text(x = 1:7,
y = par("usr")[3] - 0.1,
labels = c("ACE twins", "Twins and Sibs", "Alternate twins and sibs", "Zygosity as definition variable", "Measured genetic relationship", "DZ only", "AE unrelated - GREML"),
xpd = NA,
## Rotate the labels by 35 degrees.
srt = 45,
cex = 0.9, adj=1)
#dev.off()
colnames(res) = c("modelName", "A_lb", "A", "A_ub", "C_lb", "C",
"C_ub", "E_lb", "E", "E_ub")
print(res)
## modelName A_lb A
## [1,] "Twin" "0.333397703530672" "0.538117255044524"
## [2,] "Twin_sib" "0.113075218713683" "0.254357985817382"
## [3,] "Twin_sib2" "0.113075218713683" "0.254357985817382"
## [4,] "ZygVar" "0.112302738969462" "0.24605261509722"
## [5,] "Twin_GRM" "0.126042709921443" "0.266002675893346"
## [6,] "DZonly_GRM" "-0.451527015889042" "0.0107408443839886"
## [7,] "GREML" "-0.0797402587557465" "0.165562638441246"
## A_ub C_lb C
## [1,] "0.745644310199822" "-0.00689283099769655" "0.177546570650915"
## [2,] "0.391347994284373" "0.334920580378078" "0.433144405860925"
## [3,] "0.391347994284373" "0.334920580378078" "0.433144405860925"
## [4,] "0.391487111614681" "0.335605611403928" "0.437959812834118"
## [5,] "0.401586171160483" "0.328167879544956" "0.425688194317904"
## [6,] "0.477587200459368" "0.250255415046221" "0.485033941163498"
## [7,] "0.569265283933939" NA NA
## C_ub E_lb E
## [1,] "0.35120094557072" "0.230272306513428" "0.284336174304561"
## [2,] "0.528348151293424" "0.253716590615379" "0.312497608321694"
## [3,] "0.528348151293424" "0.253716590615379" "0.312497608321694"
## [4,] "0.528620281482147" "0.253672740785762" "0.315987572068662"
## [5,] "0.520009506808218" "0.250188076195774" "0.30830912978875"
## [6,] "0.715869909033963" "0.269909534739407" "0.504225214452513"
## [7,] NA NA "0.834437361558754"
## E_ub
## [1,] "0.347149090163806"
## [2,] "0.378469731714189"
## [3,] "0.378469731714189"
## [4,] "0.378936977026472"
## [5,] "0.372945020062481"
## [6,] "0.737634648712551"
## [7,] NA
A work by Baptiste Couvy-Duchesne