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
# Select Variables for Analysis
<- 1 # number of traits
nv <- 2 # number of individuals
nt <- nv * nt # number of total variables
ntv <- c("Twin1", "Twin2") # name of traits
selVars <- c("age1", "age2", "sex1", "sex2") # list of covariates
covVars
# Select Data for Analysis
<- subset(df, zygosity == 1, c(selVars, covVars))
mzData <- subset(df, zygosity == 2, c(selVars, covVars))
dzData
# Check Descriptives
colMeans(mzData[, c(selVars, covVars)], na.rm = T)
## Twin1 Twin2 age1 age2 sex1 sex2
## 0.2063185 0.1923213 19.9514978 19.9514978 0.4950000 0.4950000
colMeans(dzData[, c(selVars, covVars)], na.rm = T)
## Twin1 Twin2 age1 age2 sex1 sex2
## 0.2239943 0.3360167 20.0053464 20.0053464 0.5000000 0.5030000
cov(mzData, use = "pairwise.complete.obs")
## Twin1 Twin2 age1 age2 sex1 sex2
## Twin1 8.8515304 6.5239037 0.1261025811 0.1261025811 0.1094463581 0.1094463581
## Twin2 6.5239037 8.6506656 0.1163547599 0.1163547599 0.1070644625 0.1070644625
## age1 0.1261026 0.1163548 0.4845264527 0.4845264527 0.0005952069 0.0005952069
## age2 0.1261026 0.1163548 0.4845264527 0.4845264527 0.0005952069 0.0005952069
## sex1 0.1094464 0.1070645 0.0005952069 0.0005952069 0.2502252252 0.2502252252
## sex2 0.1094464 0.1070645 0.0005952069 0.0005952069 0.2502252252 0.2502252252
cov(dzData, use = "pairwise.complete.obs")
## Twin1 Twin2 age1 age2 sex1
## Twin1 8.07787519 4.15272695 0.330321792 0.330321792 0.0436080682
## Twin2 4.15272695 8.79148695 0.333837419 0.333837419 0.0157968951
## age1 0.33032179 0.33383742 0.513803535 0.513803535 -0.0148824163
## age2 0.33032179 0.33383742 0.513803535 0.513803535 -0.0148824163
## sex1 0.04360807 0.01579690 -0.014882416 -0.014882416 0.2502502503
## sex2 -0.04706772 0.04208049 0.005280718 0.005280718 -0.0005005005
## sex2
## Twin1 -0.0470677249
## Twin2 0.0420804866
## age1 0.0052807180
## age2 0.0052807180
## sex1 -0.0005005005
## sex2 0.2502412412
# Set Starting Values
<- 0 # start value for means
sMu <- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = TRUE,
intercept values = sMu, 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,
# 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")
# 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 dimnames = selVars)
<- mxExpectationNormal(covariance = "expCovDZ", means = "expMean",
expDZ dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex)
defs <- list(intercept, betaS, betaA, covA, covC, covE, covP)
pars <- 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)
# ----------------------------------------------------------------------------------------------------------------------
# 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 interC intercept 1 1 -9.3256687 1.63631862
## 2 betaS bS 1 1 0.2753476 0.09439997
## 3 betaA bA 1 1 0.4720194 0.08180316
## 4 VA11 VA 1 1 4.2031247 0.42144351
## 5 VC11 VC 1 1 2.0183948 0.40546428
## 6 VE11 VE 1 1 2.2134076 0.09780964
##
## confidence intervals:
## lbound estimate ubound note
## ACEvc.VarC[1,4] 0.4031709 0.4983001 0.5990074
## ACEvc.VarC[1,5] 0.1443103 0.2392901 0.3269656
## ACEvc.VarC[1,6] 0.2384267 0.2624098 0.2889128
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 3994 18823.12
## Saturated: NA NA NA
## Independence: NA NA NA
## Number of observations/statistics: 2000/4000
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: 10835.12 18835.12 18835.16
## BIC: -11534.88 18868.73 18849.66
## 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:28:38
## Wall clock time: 6.07047 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
# Some different ways to extract output
$output$estimate fitACE
## interC betaS betaA VA11 VC11 VE11
## -9.3256687 0.2753476 0.4720194 4.2031247 2.0183948 2.2134076
$VarC$result fitACE
## VA VC VE SA SC SE
## VC 4.203125 2.018395 2.213408 0.4983001 0.2392901 0.2624098
$output$confidenceIntervals fitACE
## lbound estimate ubound
## ACEvc.VarC[1,4] 0.4031709 0.4983001 0.5990074
## ACEvc.VarC[1,5] 0.1443103 0.2392901 0.3269656
## ACEvc.VarC[1,6] 0.2384267 0.2624098 0.2889128
= 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
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 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
# Select Data for Analysis
<- subset(df, zygosity == 1, c(selVars, covVars))
mzData <- subset(df, zygosity == 2, c(selVars, covVars))
dzData cov(mzData[, selVars], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.851530 6.523904 5.745641
## Twin2 6.523904 8.650666 5.558020
## Sib 5.745641 5.558020 9.304954
cov(dzData[, selVars], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.077875 4.152727 3.695605
## Twin2 4.152727 8.791487 4.301196
## Sib 3.695605 4.301196 8.325628
# Set Starting Values
<- 0 # start value for means
sMu <- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = TRUE,
intercept values = sMu, 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,
# 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")
# 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 dimnames = selVars)
<- mxExpectationNormal(covariance = "expCovDZ", means = "expMean",
expDZ dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex)
defs <- list(intercept, betaS, betaA, covA, covC, covE, covP)
pars <- 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)
# ----------------------------------------------------------------------------------------------------------------------
# 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 interC intercept 1 1 -5.5920306 0.83974410
## 2 betaS bS 1 1 0.3613256 0.06485551
## 3 betaA bA 1 1 0.2814054 0.04186418
## 4 VA11 VA 1 1 3.2101782 0.27289307
## 5 VC11 VC 1 1 3.0337892 0.25834607
## 6 VE11 VE 1 1 2.3026430 0.10473313
##
## confidence intervals:
## lbound estimate ubound note
## ACEsib.VarC[1,4] 0.3121488 0.3756084 0.4378840
## ACEsib.VarC[1,5] 0.3028703 0.3549699 0.4054639
## ACEsib.VarC[1,6] 0.2443557 0.2694218 0.2972491
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 5994 27891.1
## Saturated: NA NA NA
## Independence: NA NA NA
## Number of observations/statistics: 2000/6000
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: 15903.10 27903.10 27903.14
## BIC: -17668.71 27936.71 27917.64
## 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:28:44
## Wall clock time: 6.56052 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
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 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
# Select Data for Analysis
<- subset(df, zygosity == 1, c(selVars, covVars))
mzData <- subset(df, zygosity == 2, c(selVars, covVars))
dzData cov(mzData[, selVars], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.851530 6.523904 5.745641
## Twin2 6.523904 8.650666 5.558020
## Sib 5.745641 5.558020 9.304954
cov(dzData[, selVars], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.077875 4.152727 3.695605
## Twin2 4.152727 8.791487 4.301196
## Sib 3.695605 4.301196 8.325628
# Set Starting Values
<- 0 # start value for means
sMu <- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = TRUE,
intercept values = sMu, 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,
# 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")
# 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 dimnames = selVars)
<- mxExpectationNormal(covariance = "expCovDZ", means = "expMean",
expDZ dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex)
defs <- list(intercept, betaS, betaA, covA, covC, covE, covP,
pars
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)
# ----------------------------------------------------------------------------------------------------------------------
# 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 interC intercept 1 1 -5.5920306 0.83974410
## 2 betaS bS 1 1 0.3613256 0.06485551
## 3 betaA bA 1 1 0.2814054 0.04186418
## 4 VA11 VA 1 1 3.2101782 0.27289307
## 5 VC11 VC 1 1 3.0337892 0.25834607
## 6 VE11 VE 1 1 2.3026430 0.10473313
##
## confidence intervals:
## lbound estimate ubound note
## ACEsib_alt.VarC[1,4] 0.3121488 0.3756084 0.4378840
## ACEsib_alt.VarC[1,5] 0.3028703 0.3549699 0.4054639
## ACEsib_alt.VarC[1,6] 0.2443557 0.2694218 0.2972491
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 5994 27891.1
## Saturated: NA NA NA
## Independence: NA NA NA
## Number of observations/statistics: 2000/6000
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: 15903.10 27903.10 27903.14
## BIC: -17668.71 27936.71 27917.64
## 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:28:51
## Wall clock time: 6.55051 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
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 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
# Check Correlations of Data for Analysis
cov(df[df$zygT == 1, selVars], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.851530 6.523904 5.745641
## Twin2 6.523904 8.650666 5.558020
## Sib 5.745641 5.558020 9.304954
cov(df[df$zygT == 0.5, selVars], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.077875 4.152727 3.695605
## Twin2 4.152727 8.791487 4.301196
## Sib 3.695605 4.301196 8.325628
# Set Starting Values
<- 0 # start value for means
sMu <- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = TRUE,
intercept values = sMu, 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,
# 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")
# 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 = ntv, free = FALSE,
relA labels = c("data.zygT", "data.zygS", "data.zygS"), name = "rA")
<- mxMatrix(type = "Unit", nrow = ntv, ncol = ntv, free = FALSE,
relC name = "rC")
<- mxMatrix(type = "Iden", nrow = ntv, ncol = ntv, free = FALSE,
relE name = "rE")
<- mxAlgebra(expression = rA %x% VA + rC %x% VC + rE %x%
expCov name = "expCov")
VE,
# Create Data Object
<- mxData(observed = df, type = "raw")
dataTW
# Create Expectation Objects
<- mxExpectationNormal(covariance = "expCov", means = "expMean",
expTW dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects
<- list(defAge, defSex, relA)
defs <- list(intercept, betaS, betaA, covA, covC, covE, covP,
pars
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)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T)
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of zygdef
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 interC intercept 1 1 -5.5920305 0.83979380
## 2 betaS bS 1 1 0.3613256 0.06493829
## 3 betaA bA 1 1 0.2814054 0.04185934
## 4 VA11 VA 1 1 3.2101782 0.27126856
## 5 VC11 VC 1 1 3.0337892 0.25695191
## 6 VE11 VE 1 1 2.3026429 0.10436147
##
## confidence intervals:
## lbound estimate ubound note
## zygdef.VarC[1,4] 0.3121488 0.3756084 0.4378840
## zygdef.VarC[1,5] 0.3028703 0.3549699 0.4054639
## zygdef.VarC[1,6] 0.2443557 0.2694218 0.2972491
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 5994 27891.1
## Saturated: 9 5991 NA
## Independence: 6 5994 NA
## Number of observations/statistics: 2000/6000
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: 15903.10 27903.10 27903.14
## BIC: -17668.71 27936.71 27917.64
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:29:00
## Wall clock time: 9.101765 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
# Select Variables for Analysis
<- 1 # number of traits
nv <- 3 # number of individuals
nt <- 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
# Check Correlations of Data for Analysis
cov(df[, c(selVars)], use = "pairwise.complete.obs")
## Twin1 Twin2 Sib
## Twin1 8.534289 4.319766 4.292122
## Twin2 4.319766 8.595571 4.365393
## Sib 4.292122 4.365393 8.704640
# Set Starting Values
<- 0 # start value for means
sMu <- 0.3 # start value for A
sVa <- 0.3 # start value for C
sVc <- 0.3 # start value for E
sVe
# ----------------------------------------------------------------------------------------------------------------------
# PREPARE MODEL
# Create Algebra for expected Mean Matrices
<- mxMatrix(type = "Full", nrow = 1, ncol = ntv, free = TRUE,
intercept values = sMu, 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,
# 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")
# 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 = df, type = "raw")
dataTW
# Create Expectation Objects for Multiple Groups
<- mxExpectationNormal(covariance = "expCov", means = "expMean",
expTW dimnames = selVars)
<- mxFitFunctionML()
funML
# Create Model Objects for Multiple Groups
<- list(defAge, defSex, relA)
defs <- list(intercept, betaS, betaA, covA, covC, covE, covP,
pars
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", pars, defs, expMean, expCov, expTW,
modelACE
dataTW, funML, estVC, ciACE)
# ----------------------------------------------------------------------------------------------------------------------
# RUN MODEL
# Run ACE Model
<- mxRun(modelACE, intervals = T)
fitACE <- summary(fitACE)
sumACE sumACE
## Summary of DZonly
##
## The Hessian at the solution does not appear to be convex. See ?mxCheckIdentification for possible diagnosis (Mx status RED).
##
## free parameters:
## name matrix row col Estimate Std.Error A
## 1 interC intercept 1 1 -9.2148352 0.36918169
## 2 betaS bS 1 1 0.1247789 0.02677999
## 3 betaA bA 1 1 0.4588013 0.01837662
## 4 VA11 VA 1 1 0.3358636 NA
## 5 VC11 VC 1 1 3.9003608 NA
## 6 VE11 VE 1 1 4.0855831 NA
##
## confidence intervals:
## lbound estimate ubound note
## DZonly.VarC[1,4] -0.2001905 0.04035946 0.2814802
## DZonly.VarC[1,5] 0.3472009 0.46869154 0.5891498
## DZonly.VarC[1,6] 0.3701761 0.49094900 0.6127423
##
## Model Statistics:
## | Parameters | Degrees of Freedom | Fit (-2lnL units)
## Model: 6 29994 142099.8
## Saturated: 9 29991 NA
## Independence: 6 29994 NA
## Number of observations/statistics: 10000/30000
##
##
## ** Information matrix is not positive definite (not at a candidate optimum).
## Be suspicious of these results. At minimum, do not trust the standard errors.
##
## Information Criteria:
## | df Penalty | Parameters Penalty | Sample-Size Adjusted
## AIC: 82111.82 142111.8 142111.8
## BIC: -134155.13 142155.1 142136.0
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2024-03-05 09:30:37
## Wall clock time: 83.06414 secs
## optimizer: SLSQP
## OpenMx version number: 2.21.11
## Need help? See help(mxSummary)
= 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.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 A_ub
## [1,] "Twin" "0.403170946807557" "0.498300061082194" "0.599007353663766"
## [2,] "Twin_sib" "0.312148768490362" "0.375608350283173" "0.437883993042671"
## [3,] "Twin_sib2" "0.312148768490362" "0.375608350283173" "0.437883993042671"
## [4,] "ZygVar" "0.312148762145735" "0.375608351833094" "0.437883993032954"
## [5,] "Twin_GRM" "0.31232964872899" "0.375578874300066" "0.437641948870637"
## [6,] "DZonly_GRM" "-0.200190470850726" "0.0403594584376225" "0.281480163423856"
## [7,] "GREML" "-0.157638196233984" "0.157523670059722" "0.459854590400124"
## C_lb C C_ub
## [1,] "0.144310315683289" "0.239290130920058" "0.326965639095612"
## [2,] "0.302870332983627" "0.354969872444485" "0.40546393501121"
## [3,] "0.302870332983627" "0.354969872444485" "0.40546393501121"
## [4,] "0.302870332546734" "0.354969871241579" "0.405463933962984"
## [5,] "0.303223504106537" "0.355189767536258" "0.405526105479709"
## [6,] "0.347200873643996" "0.468691540576436" "0.589149814869066"
## [7,] NA NA NA
## E_lb E E_ub
## [1,] "0.238426708822721" "0.262409807997747" "0.288912818053576"
## [2,] "0.244355651981887" "0.269421777272342" "0.29724908252969"
## [3,] "0.244355651981887" "0.269421777272342" "0.29724908252969"
## [4,] "0.244355651400181" "0.269421776925327" "0.297249082347298"
## [5,] "0.244302306626096" "0.269231358163676" "0.29698910698171"
## [6,] "0.370176113774172" "0.490949000985941" "0.612742289647207"
## [7,] NA "0.842476329940278" NA
A work by Baptiste Couvy-Duchesne