@@ -20,7 +20,8 @@ buildPedigreeModelCovariance <- function(
2020 vars = list (
2121 ad2 = 0.5 ,
2222 dd2 = 0.3 ,
23- cn2 = 0.2 , ce2 = 0.4 ,
23+ cn2 = 0.2 ,
24+ ce2 = 0.4 ,
2425 mt2 = 0.1 ,
2526 am2 = 0.25 ,
2627 ee2 = 0.6
@@ -104,8 +105,10 @@ buildPedigreeModelCovariance <- function(
104105# ' @param Mtdmat Mitochondrial genetic relatedness matrix (from \code{\link{ped2mit}}).
105106# ' @param Amimat Additive by mitochondrial interaction relatedness matrix.
106107# ' @param Dmgmat Dominance genetic relatedness matrix.
107- # ' @param full_df_row A 1-row matrix of observed data with column names matching \code{ytemp}.
108- # ' @param ytemp A character vector of variable names corresponding to the observed data columns.
108+ # ' @param full_df_row A 1-row matrix of observed data with column names matching \code{obs_ids}.
109+ # ' @param obs_ids A character vector of individual IDs corresponding to the columns of
110+ # ' \code{full_df_row} and the rows/columns of the relatedness matrices. Must be in the
111+ # ' same order as the relatedness matrix rows.
109112# ' @return An OpenMx model for the specified family group.
110113# ' @export
111114
@@ -118,7 +121,7 @@ buildOneFamilyGroup <- function(
118121 Amimat = NULL ,
119122 Dmgmat = NULL ,
120123 full_df_row ,
121- ytemp
124+ obs_ids
122125) {
123126 if (! requireNamespace(" OpenMx" , quietly = TRUE )) {
124127 stop(" OpenMx package is required for buildOneFamilyGroup function. Please install it." )
@@ -208,10 +211,10 @@ buildOneFamilyGroup <- function(
208211 OpenMx :: mxData(observed = full_df_row , type = " raw" , sort = FALSE ),
209212 OpenMx :: mxMatrix(" Full" ,
210213 nrow = 1 , ncol = fsize , name = " M" , free = TRUE ,
211- labels = " meanLI" , dimnames = list (NULL , ytemp )
214+ labels = " meanLI" , dimnames = list (NULL , obs_ids )
212215 ),
213216 OpenMx :: mxAlgebraFromString(algebra_str ,
214- name = " V" , dimnames = list (ytemp , ytemp )
217+ name = " V" , dimnames = list (obs_ids , obs_ids )
215218 ),
216219 OpenMx :: mxExpectationNormal(covariance = " V" , means = " M" ),
217220 OpenMx :: mxFitFunctionML()
@@ -227,7 +230,8 @@ buildOneFamilyGroup <- function(
227230# ' provided relatedness matrices and observed data.
228231# '
229232# ' @param dat A data frame where each row represents a family group and columns correspond to observed variables.
230- # ' @param ytemp A vector of variable names corresponding to the observed data.
233+ # ' @param obs_ids A character vector of individual IDs corresponding to the columns of \code{dat}
234+ # ' and the rows/columns of the relatedness matrices.
231235# ' @param Addmat Additive genetic relatedness matrix.
232236# ' @param Nucmat Nuclear family shared environment relatedness matrix.
233237# ' @param Extmat Extended family shared environment relatedness matrix.
@@ -239,7 +243,7 @@ buildOneFamilyGroup <- function(
239243# ' @export
240244
241245buildFamilyGroups <- function (
242- dat , ytemp ,
246+ dat , obs_ids ,
243247 Addmat = NULL ,
244248 Nucmat = NULL ,
245249 Extmat = NULL ,
@@ -256,17 +260,17 @@ buildFamilyGroups <- function(
256260 groups <- vector(" list" , numfam )
257261
258262 for (afam in seq_len(numfam )) {
259- full_df_row <- matrix (dat [afam , ], nrow = 1 , dimnames = list (NULL , ytemp ))
263+ full_df_row <- matrix (dat [afam , ], nrow = 1 , dimnames = list (NULL , obs_ids ))
260264 groups [[afam ]] <- buildOneFamilyGroup(
261- group_name = paste0(prefix , afam ),
262- Addmat = Addmat ,
263- Nucmat = Nucmat ,
264- Extmat = Extmat ,
265- Mtdmat = Mtdmat ,
266- Amimat = Amimat ,
267- Dmgmat = Dmgmat ,
265+ group_name = paste0(prefix , afam ),
266+ Addmat = Addmat ,
267+ Nucmat = Nucmat ,
268+ Extmat = Extmat ,
269+ Mtdmat = Mtdmat ,
270+ Amimat = Amimat ,
271+ Dmgmat = Dmgmat ,
268272 full_df_row = full_df_row ,
269- ytemp = ytemp
273+ obs_ids = obs_ids
270274 )
271275 }
272276
@@ -283,10 +287,12 @@ buildFamilyGroups <- function(
283287# ' @param model_name Name of the overall pedigree model.
284288# ' @param vars A named list or vector of initial variance component values.
285289# ' @param group_models A list of OpenMx models for each family group.
290+ # ' @param ci Logical. If TRUE, include confidence interval computations for the variance components. Default is FALSE
286291# ' @return An OpenMx pedigree model combining variance components and family groups.
287292# ' @export
288293
289- buildPedigreeMx <- function (model_name , vars , group_models ) {
294+ buildPedigreeMx <- function (model_name , vars , group_models ,
295+ ci = FALSE ) {
290296 if (! requireNamespace(" OpenMx" , quietly = TRUE )) {
291297 stop(" OpenMx package is required for buildPedigreeMx function. Please install it." )
292298 }
@@ -331,7 +337,12 @@ buildPedigreeMx <- function(model_name, vars, group_models) {
331337 Ver = isTRUE(flags $ Ver )
332338 ),
333339 group_models ,
334- OpenMx :: mxFitFunctionMultigroup(group_names )
340+ OpenMx :: mxFitFunctionMultigroup(group_names ),
341+ ci = if (ci & any(flags $ Vad , flags $ Vdd , flags $ Vcn , flags $ Vce , flags $ Vmt , flags $ Vam , flags $ Ver )) {
342+ OpenMx :: mxCI(c(" vad" , " vdd" , " vcn" , " vce" , " vmt" , " vam" , " ver" )[c(flags $ Vad , flags $ Vdd , flags $ Vcn , flags $ Vce , flags $ Vmt , flags $ Vam , flags $ Ver )])
343+ } else {
344+ NULL
345+ }
335346 )
336347}
337348
@@ -347,6 +358,7 @@ buildPedigreeMx <- function(model_name, vars, group_models) {
347358# ' @param group_models Optional list of pre-built OpenMx family group models
348359# ' (from \code{\link{buildOneFamilyGroup}}). If NULL, they are generated from \code{data}
349360# ' using the provided relatedness matrices.
361+ # ' @param intervals Logical. If TRUE (default), compute confidence intervals for the parameters using \code{mxSE} and \code{mxCI}.
350362# ' @param Addmat Additive genetic relatedness matrix. Required when \code{group_models} is NULL.
351363# ' @param Nucmat Common nuclear environment relatedness matrix. Optional.
352364# ' @param Extmat Common extended environment relatedness matrix. Optional.
@@ -363,14 +375,16 @@ fitPedigreeModel <- function(
363375 vars = list (
364376 ad2 = 0.5 ,
365377 dd2 = 0.3 ,
366- cn2 = 0.2 , ce2 = 0.4 ,
378+ cn2 = 0.2 ,
379+ ce2 = 0.4 ,
367380 mt2 = 0.1 ,
368381 am2 = 0.25 ,
369382 ee2 = 0.6
370383 ),
371384 data = NULL ,
372385 group_models = NULL ,
373386 tryhard = TRUE ,
387+ intervals = TRUE ,
374388 Addmat = NULL ,
375389 Nucmat = NULL ,
376390 Extmat = NULL ,
@@ -387,10 +401,10 @@ fitPedigreeModel <- function(
387401 if (is.null(data )) {
388402 stop(" Either 'group_models' or 'data' must be provided." )
389403 }
390- ytemp <- colnames(data )
404+ obs_ids <- colnames(data )
391405 group_models <- buildFamilyGroups(
392406 dat = data ,
393- ytemp = ytemp ,
407+ obs_ids = obs_ids ,
394408 Addmat = Addmat ,
395409 Nucmat = Nucmat ,
396410 Extmat = Extmat ,
@@ -405,10 +419,10 @@ fitPedigreeModel <- function(
405419 vars = vars ,
406420 group_models = group_models
407421 )
408- if (tryhard ) {
409- fitted_model <- OpenMx :: mxTryHard(pedigree_model , silent = TRUE , extraTries = 10 , intervals = TRUE )
422+ if (tryhard == TRUE ) {
423+ fitted_model <- OpenMx :: mxTryHard(pedigree_model , silent = TRUE , extraTries = 10 , intervals = intervals )
410424 } else {
411- fitted_model <- OpenMx :: mxRun(pedigree_model )
425+ fitted_model <- OpenMx :: mxRun(pedigree_model , intervals = intervals )
412426 }
413427 fitted_model
414428}
0 commit comments