Skip to content

Commit 801d5db

Browse files
committed
Update rerun for osmasem2
1 parent a13731f commit 801d5db

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

R/create.mxModel.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,14 @@ create.mxModel <- function(model.name="mxModel", RAM=NULL, data=NULL,
139139
## Create an identity matrix from the no. of columens of Fmatrix,
140140
## including all latent and observed variables
141141
Id <- as.mxMatrix(diag(ncol(Fmatrix$values)), name="Id")
142-
143-
## Expected covariance matrix and means of the observed and latent variables
144142
Id_A <- mxAlgebra(solve(Id - Amatrix), name="Id_A")
143+
144+
## Note. expCov and expMean are NOT used in the fit function.
145+
## They are included so the implied structures include the latent variables.
146+
## It may be useful for future applications.
145147
expCov <- mxAlgebra(Id_A %&% Smatrix, name="expCov")
146-
148+
expMean <- mxAlgebra(Mmatrix %*% t(Id_A), name="expMean")
149+
147150
## Add the mean structure only if there are means
148151
if (!is.null(data) | !is.null(means)) {
149152

@@ -153,8 +156,7 @@ create.mxModel <- function(model.name="mxModel", RAM=NULL, data=NULL,
153156
M <- as.mxAlgebra(M, startvalues=startvalues, name="Mmatrix")
154157
mx.model <- mxModel(mx.model, M$mxalgebra, M$parameters, M$list)
155158
}
156-
157-
expMean <- mxAlgebra(Mmatrix %*% t(Id_A), name="expMean")
159+
158160
mx.model <- mxModel(mx.model, Fmatrix, Id, Id_A, expCov, expMean,
159161
mxCI(c("Amatrix", "Smatrix", "Mmatrix")))
160162
} else {

R/rerun.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ rerun <- function(object, autofixtau2=FALSE, extraTries=10, ...) {
22
if (!is.element(class(object)[1], c("wls", "tssem1FEM", "tssem1REM", "meta",
33
"meta3LFIML", "reml",
44
"tssem1FEM.cluster", "wls.cluster",
5-
"osmasem", "osmasem3L", "MxModel",
6-
"mxRAMmodel")))
7-
stop("\"object\" must be an object of neither class \"meta\", \"meta3LFIML\", \"wls\", \"reml\", \"tssem1FEM\", \"tssem1REM\", \"tssem1FEM.cluster\", \"wls.cluster\", \"osmasem\", \"osmasem3L\", \"MxModel\", or \"mxRAMModel\".")
5+
"osmasem", "osmasem2", "osmasem3L",
6+
"MxModel", "mxRAMmodel")))
7+
stop("'object' must be an object of neither class 'meta', 'meta3LFIML',
8+
'wls', 'reml', 'tssem1FEM', 'tssem1REM', 'tssem1FEM.cluster', 'wls.cluster',
9+
'osmasem', 'osmasem2', 'osmasem3L', 'MxModel', or 'mxRAMModel'.")
810

911
## Run a rerun without autofixtau2 to minimize over-fixing
1012
## Many of the NA in SEs may disappear after rerunning it.
11-
if (autofixtau2 & is.element(class(object)[1], c("tssem1REM", "meta", "osmasem"))) {
13+
if (autofixtau2 & is.element(class(object)[1], c("tssem1REM", "meta",
14+
"osmasem", "osmasem2"))) {
1215
object <- rerun(object, autofixtau2=FALSE, extraTries=extraTries, ...)
1316
}
1417

1518
## Automatically fix the problematic Tau2 into 0 for tssem1REM and meta objects
16-
if (autofixtau2 & is.element(class(object)[1], c("tssem1REM", "meta"))) {
19+
## osmasem2 object is similar to meta object as it uses Tau2
20+
if (autofixtau2 & is.element(class(object)[1], c("tssem1REM", "meta",
21+
"osmasem2"))) {
1722
## Get the Tau2 with NA is SE
1823
tau2nan <- suppressWarnings(sqrt(diag(vcov(object, select="random"))))
1924

@@ -78,6 +83,11 @@ rerun <- function(object, autofixtau2=FALSE, extraTries=10, ...) {
7883
bestInitsOutput=FALSE,
7984
intervals=TRUE, ...))
8085
}
81-
}
86+
}
87+
## Run it again as the SEs sometimes diasapper
88+
fit <- out$mx.fit
89+
fit <- mxOption(fit, "Calculate Hessian", "Yes")
90+
fit <- mxOption(fit, "Standard Errors", "Yes")
91+
out$mx.fit <- mxRun(fit, silent=TRUE, suppressWarnings=TRUE)
8292
out
8393
}

0 commit comments

Comments
 (0)