Skip to content

Commit c1b4474

Browse files
Refactor .pedigreeRelatednessMatrix for matrix handling
Updated the .pedigreeRelatednessMatrix function to ensure that the matrix is always coerced to a base dense matrix after symmetrization. Added error handling for matrix creation.
1 parent 8d609b2 commit c1b4474

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

R/buildmxPedigrees.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,21 @@ buildPedigreeModelCovariance <- function(
122122
#' @keywords internal
123123
.pedigreeRelatednessMatrix <- function(mat, fsize, name, condense = TRUE, symmetrize = FALSE) {
124124
if (is.null(mat)) stop("Relatedness matrix cannot be NULL.")
125-
# keep dense if dense, or sparse if sparse, but symmetrize if requested
126-
if (inherits(mat, "Matrix")) {
127-
values <- if (symmetrize) make_symmetric(mat) else mat
128-
} else {
129-
values <- if (symmetrize) make_symmetric(mat) else as.matrix(mat)
130-
}
131-
m <- OpenMx::mxMatrix(
125+
# Symmetrize if requested, then coerce to a base dense matrix. OpenMx::mxMatrix()
126+
# accepts only a scalar, vector, or base R matrix for 'values'; a Matrix-package
127+
# sparse object (e.g. the dsCMatrix returned by ped2add(sparse = TRUE)) is rejected
128+
# by matrixCheckArgument(), so it must be densified here.
129+
values <- if (symmetrize) make_symmetric(mat) else mat
130+
values <- as.matrix(values)
131+
#todo allow this to be sparse and use sparse algebra in OpenMx
132+
m <- tryCatch( OpenMx::mxMatrix(
132133
type = "Symm", nrow = fsize, ncol = fsize, free = FALSE,
133134
values = values, name = name
134-
)
135+
), error = function(e) {
136+
print(values)
137+
stop("Error creating mxMatrix for ", name, ": ", e$message)
138+
})
139+
135140
if (condense) m <- condenseMatrixSlots(m)
136141
m
137142
}

0 commit comments

Comments
 (0)