Skip to content

Commit 0c40a2e

Browse files
Prefix OpenMx calls and remove library()
Remove direct library(OpenMx) usage and rely on requireNamespace checks, replacing unqualified OpenMx function calls with explicit OpenMx:: qualifiers across the file. Also make a small call-site clarity change by using named arguments when building the pedigree model. This avoids attaching the OpenMx namespace globally and prevents potential symbol conflicts or side effects from loading the package.
1 parent e87b256 commit 0c40a2e

1 file changed

Lines changed: 31 additions & 37 deletions

File tree

R/buildmxPedigrees.R

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ buildPedigreeModelCovariance <- function(
3535
) {
3636
if (!requireNamespace("OpenMx", quietly = TRUE)) {
3737
stop("OpenMx package is required for buildPedigreeModelCovariance function. Please install it.")
38-
} else {
39-
library(OpenMx)
4038
}
4139

4240
# Coerce to list so both c() vectors and list() inputs work with [[ ]]
@@ -45,49 +43,49 @@ buildPedigreeModelCovariance <- function(
4543
# Build the list of mxMatrix components conditionally
4644
mat_list <- list()
4745
if (Vad) {
48-
mat_list <- c(mat_list, list(mxMatrix(
46+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
4947
type = "Full", nrow = 1, ncol = 1, free = TRUE,
5048
values = vars[["ad2"]], labels = "vad", name = "Vad", lbound = 1e-10
5149
)))
5250
}
5351
if (Vdd) {
54-
mat_list <- c(mat_list, list(mxMatrix(
52+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
5553
type = "Full", nrow = 1, ncol = 1, free = TRUE,
5654
values = vars[["dd2"]], labels = "vdd", name = "Vdd", lbound = 1e-10
5755
)))
5856
}
5957
if (Vcn) {
60-
mat_list <- c(mat_list, list(mxMatrix(
58+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
6159
type = "Full", nrow = 1, ncol = 1, free = TRUE,
6260
values = vars[["cn2"]], labels = "vcn", name = "Vcn", lbound = 1e-10
6361
)))
6462
}
6563
if (Vce) {
66-
mat_list <- c(mat_list, list(mxMatrix(
64+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
6765
type = "Full", nrow = 1, ncol = 1, free = TRUE,
6866
values = vars[["ce2"]], labels = "vce", name = "Vce", lbound = 1e-10
6967
)))
7068
}
7169
if (Vmt) {
72-
mat_list <- c(mat_list, list(mxMatrix(
70+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
7371
type = "Full", nrow = 1, ncol = 1, free = TRUE,
7472
values = vars[["mt2"]], labels = "vmt", name = "Vmt", lbound = 1e-10
7573
)))
7674
}
7775
if (Vam) {
78-
mat_list <- c(mat_list, list(mxMatrix(
76+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
7977
type = "Full", nrow = 1, ncol = 1, free = TRUE,
8078
values = vars[["am2"]], labels = "vam", name = "Vam", lbound = 1e-10
8179
)))
8280
}
8381
if (Ver) {
84-
mat_list <- c(mat_list, list(mxMatrix(
82+
mat_list <- c(mat_list, list(OpenMx::mxMatrix(
8583
type = "Full", nrow = 1, ncol = 1, free = TRUE,
8684
values = vars[["ee2"]], labels = "ver", name = "Ver", lbound = 1e-10
8785
)))
8886
}
8987

90-
do.call(mxModel, c(list("ModelOne"), mat_list))
88+
do.call(OpenMx::mxModel, c(list("ModelOne"), mat_list))
9189
}
9290

9391
#' Build one family group model
@@ -124,8 +122,6 @@ buildOneFamilyGroup <- function(
124122
) {
125123
if (!requireNamespace("OpenMx", quietly = TRUE)) {
126124
stop("OpenMx package is required for buildOneFamilyGroup function. Please install it.")
127-
} else {
128-
library(OpenMx)
129125
}
130126

131127
# Determine family size from first available matrix
@@ -143,15 +139,15 @@ buildOneFamilyGroup <- function(
143139
# so we never reference a matrix or variance component that doesn't exist.
144140
# ------------------------------------------------------------------
145141
mat_list <- list(
146-
mxMatrix("Iden", nrow = fsize, ncol = fsize, name = "I"),
147-
mxMatrix("Unit", nrow = fsize, ncol = fsize, name = "U")
142+
OpenMx::mxMatrix("Iden", nrow = fsize, ncol = fsize, name = "I"),
143+
OpenMx::mxMatrix("Unit", nrow = fsize, ncol = fsize, name = "U")
148144
)
149145

150146
algebra_terms <- character(0)
151147

152148
if (!is.null(Addmat)) {
153149
mat_list <- c(mat_list, list(
154-
mxMatrix("Symm",
150+
OpenMx::mxMatrix("Symm",
155151
nrow = fsize, ncol = fsize,
156152
values = as.matrix(Addmat), name = "A"
157153
)
@@ -160,7 +156,7 @@ buildOneFamilyGroup <- function(
160156
}
161157
if (!is.null(Dmgmat)) {
162158
mat_list <- c(mat_list, list(
163-
mxMatrix("Symm",
159+
OpenMx::mxMatrix("Symm",
164160
nrow = fsize, ncol = fsize,
165161
values = as.matrix(Dmgmat), name = "D"
166162
)
@@ -169,7 +165,7 @@ buildOneFamilyGroup <- function(
169165
}
170166
if (!is.null(Nucmat)) {
171167
mat_list <- c(mat_list, list(
172-
mxMatrix("Symm",
168+
OpenMx::mxMatrix("Symm",
173169
nrow = fsize, ncol = fsize,
174170
values = as.matrix(Nucmat), name = "Cn"
175171
)
@@ -182,7 +178,7 @@ buildOneFamilyGroup <- function(
182178
}
183179
if (!is.null(Amimat)) {
184180
mat_list <- c(mat_list, list(
185-
mxMatrix("Symm",
181+
OpenMx::mxMatrix("Symm",
186182
nrow = fsize, ncol = fsize,
187183
values = as.matrix(Amimat), name = "Am"
188184
)
@@ -191,7 +187,7 @@ buildOneFamilyGroup <- function(
191187
}
192188
if (!is.null(Mtdmat)) {
193189
mat_list <- c(mat_list, list(
194-
mxMatrix("Symm",
190+
OpenMx::mxMatrix("Symm",
195191
nrow = fsize, ncol = fsize,
196192
values = as.matrix(Mtdmat), name = "Mt"
197193
)
@@ -209,20 +205,20 @@ buildOneFamilyGroup <- function(
209205
list(name = group_name),
210206
mat_list,
211207
list(
212-
mxData(observed = full_df_row, type = "raw", sort = FALSE),
213-
mxMatrix("Full",
208+
OpenMx::mxData(observed = full_df_row, type = "raw", sort = FALSE),
209+
OpenMx::mxMatrix("Full",
214210
nrow = 1, ncol = fsize, name = "M", free = TRUE,
215211
labels = "meanLI", dimnames = list(NULL, ytemp)
216212
),
217-
mxAlgebraFromString(algebra_str,
213+
OpenMx::mxAlgebraFromString(algebra_str,
218214
name = "V", dimnames = list(ytemp, ytemp)
219215
),
220-
mxExpectationNormal(covariance = "V", means = "M"),
221-
mxFitFunctionML()
216+
OpenMx::mxExpectationNormal(covariance = "V", means = "M"),
217+
OpenMx::mxFitFunctionML()
222218
)
223219
)
224220

225-
do.call(mxModel, model_args)
221+
do.call(OpenMx::mxModel, model_args)
226222
}
227223

228224
#' Build family group models
@@ -253,9 +249,7 @@ buildFamilyGroups <- function(
253249
prefix = "fam"
254250
) {
255251
if (!requireNamespace("OpenMx", quietly = TRUE)) {
256-
stop("OpenMx package is required for fitPedigreeModel function. Please install it.")
257-
} else {
258-
library(OpenMx)
252+
stop("OpenMx package is required for buildFamilyGroups function. Please install it.")
259253
}
260254

261255
numfam <- nrow(dat)
@@ -295,8 +289,6 @@ buildFamilyGroups <- function(
295289
buildPedigreeMx <- function(model_name, vars, group_models) {
296290
if (!requireNamespace("OpenMx", quietly = TRUE)) {
297291
stop("OpenMx package is required for buildPedigreeMx function. Please install it.")
298-
} else {
299-
library(OpenMx)
300292
}
301293

302294
group_names <- vapply(group_models, function(m) m$name, character(1))
@@ -326,7 +318,7 @@ buildPedigreeMx <- function(model_name, vars, group_models) {
326318

327319
flags <- lapply(vc_map, function(pat) grepl(pat, all_formulas, fixed = TRUE))
328320

329-
mxModel(
321+
OpenMx::mxModel(
330322
model_name,
331323
buildPedigreeModelCovariance(
332324
vars,
@@ -339,7 +331,7 @@ buildPedigreeMx <- function(model_name, vars, group_models) {
339331
Ver = isTRUE(flags$Ver)
340332
),
341333
group_models,
342-
mxFitFunctionMultigroup(group_names)
334+
OpenMx::mxFitFunctionMultigroup(group_names)
343335
)
344336
}
345337

@@ -388,8 +380,6 @@ fitPedigreeModel <- function(
388380
) {
389381
if (!requireNamespace("OpenMx", quietly = TRUE)) {
390382
stop("OpenMx package is required for fitPedigreeModel function. Please install it.")
391-
} else {
392-
library(OpenMx)
393383
}
394384

395385
if (is.null(group_models)) {
@@ -410,11 +400,15 @@ fitPedigreeModel <- function(
410400
)
411401
}
412402

413-
pedigree_model <- buildPedigreeMx(model_name, vars, group_models)
403+
pedigree_model <- buildPedigreeMx(
404+
model_name = model_name,
405+
vars = vars,
406+
group_models = group_models
407+
)
414408
if (tryhard) {
415-
fitted_model <- mxTryHard(pedigree_model, silent = TRUE, extraTries = 10, intervals = TRUE)
409+
fitted_model <- OpenMx::mxTryHard(pedigree_model, silent = TRUE, extraTries = 10, intervals = TRUE)
416410
} else {
417-
fitted_model <- mxRun(pedigree_model)
411+
fitted_model <- OpenMx::mxRun(pedigree_model)
418412
}
419413
fitted_model
420414
}

0 commit comments

Comments
 (0)