Skip to content

Commit dd2abd6

Browse files
Merge pull request #144 from R-Computing-Lab/dev_main
Add trimming functionality and update related documentation
2 parents 053eace + 66077f2 commit dd2abd6

37 files changed

Lines changed: 20790 additions & 926 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
*.ASOIAF.ged
33
*.Rproj
44
*.[rR][dD]ata
5-
*.[rR]ds
65
*.dbf
76
*.doc*
87
*.eps
@@ -52,5 +51,7 @@ tests/testthat/Rplots.pdf
5251
vignettes/articles/paper.html
5352
vignettes/rewritten_relatedness_vignette.Rmd
5453
vignettes/rewritten_relatedness_vignette.Xmd
54+
revdep/
55+
/.claude
5556
vignettes/understanding_relatedness.Rmd
5657
vignettes/understanding_relatedness.Xmd

.lintr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
linters: linters_with_defaults(
22
line_length_linter = NULL,
33
commented_code_linter = NULL,
4+
whitespace_linter = NULL,
5+
indentation_linter = NULL,
46
object_name_linter= NULL) # see vignette("lintr")
57
encoding: "UTF-8"
8+
exclusions: list(
9+
"renv",
10+
"raw_data"
11+
)

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export(computeParentAdjacency)
2121
export(createGenDataFrame)
2222
export(determineSex)
2323
export(dropLink)
24+
export(findLeaves)
2425
export(fitComponentModel)
2526
export(fitPedigreeModel)
2627
export(getWikiTreeSummary)
@@ -57,6 +58,7 @@ export(summarizeMatrilines)
5758
export(summarizePatrilines)
5859
export(summarizePedigrees)
5960
export(traceTreePaths)
61+
export(trimPedigree)
6062
export(vech)
6163
importFrom(Matrix,Diagonal)
6264
importFrom(Matrix,sparseMatrix)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* Fixed missing checkpoint for ram_checkpoint
77
* Try a chunk_size argument for ped2com to reduce memory usage during transpose
88
* Try filter method for whose relatedness to return by individual ID
9+
* Renamed `ytemp` parameter to `obs_ids` in `buildOneFamilyGroup()` and `buildFamilyGroups()` for clarity
10+
* Expanded v6 vignettes with data requirements reference and real-data workflow using the `hazard` dataset
11+
* Allow confidence intervals for pedigree mx wrappers
912

1013
# BGmisc 1.6.0.1
1114
## CRAN submission

R/buildComponent.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,26 @@ ped2com <- function(ped, component,
222222
}
223223

224224
# isPar is the adjacency matrix. 'A' matrix from RAM
225-
226225
if (config$component %in% c("common nuclear")) {
227226
Matrix::diag(isPar) <- 1
227+
228+
if (!is.null(config$keep_ids)) {
229+
isPar <- .subsetKeepIds(
230+
component = isPar,
231+
keep_ids = keep_ids,
232+
available_ids = rownames(isPar),
233+
config = config,
234+
drop = FALSE,
235+
verbose_message = "Subsetting adjacency matrix to %d target individuals\n"
236+
) # also need to drop columns here because the adjacency matrix is used in the path tracing and we want to make sure the paths are correct for the target individuals. We will keep all columns for the path tracing but subset to the target rows so that the relatedness values are correct for the target individuals.
237+
238+
if (length(rownames(isPar)) > 1) {
239+
isPar <- isPar[, rownames(isPar), drop = FALSE]
240+
} # else {
241+
# isPar <- isPar[rownames(isPar)]
242+
# }
243+
# isPar <- isPar[, rownames(isPar)] #
244+
}
228245
if (config$sparse == FALSE) {
229246
isPar <- as.matrix(isPar)
230247
}

R/buildmxPedigrees.R

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

241245
buildFamilyGroups <- 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

Comments
 (0)