Skip to content

Commit

Permalink
Update rerun for osmasem2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewlcheung committed Jul 15, 2024
1 parent a13731f commit 801d5db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
12 changes: 7 additions & 5 deletions R/create.mxModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ create.mxModel <- function(model.name="mxModel", RAM=NULL, data=NULL,
## Create an identity matrix from the no. of columens of Fmatrix,
## including all latent and observed variables
Id <- as.mxMatrix(diag(ncol(Fmatrix$values)), name="Id")

## Expected covariance matrix and means of the observed and latent variables
Id_A <- mxAlgebra(solve(Id - Amatrix), name="Id_A")

## Note. expCov and expMean are NOT used in the fit function.
## They are included so the implied structures include the latent variables.
## It may be useful for future applications.
expCov <- mxAlgebra(Id_A %&% Smatrix, name="expCov")

expMean <- mxAlgebra(Mmatrix %*% t(Id_A), name="expMean")

## Add the mean structure only if there are means
if (!is.null(data) | !is.null(means)) {

Expand All @@ -153,8 +156,7 @@ create.mxModel <- function(model.name="mxModel", RAM=NULL, data=NULL,
M <- as.mxAlgebra(M, startvalues=startvalues, name="Mmatrix")
mx.model <- mxModel(mx.model, M$mxalgebra, M$parameters, M$list)
}

expMean <- mxAlgebra(Mmatrix %*% t(Id_A), name="expMean")

mx.model <- mxModel(mx.model, Fmatrix, Id, Id_A, expCov, expMean,
mxCI(c("Amatrix", "Smatrix", "Mmatrix")))
} else {
Expand Down
22 changes: 16 additions & 6 deletions R/rerun.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ rerun <- function(object, autofixtau2=FALSE, extraTries=10, ...) {
if (!is.element(class(object)[1], c("wls", "tssem1FEM", "tssem1REM", "meta",
"meta3LFIML", "reml",
"tssem1FEM.cluster", "wls.cluster",
"osmasem", "osmasem3L", "MxModel",
"mxRAMmodel")))
stop("\"object\" must be an object of neither class \"meta\", \"meta3LFIML\", \"wls\", \"reml\", \"tssem1FEM\", \"tssem1REM\", \"tssem1FEM.cluster\", \"wls.cluster\", \"osmasem\", \"osmasem3L\", \"MxModel\", or \"mxRAMModel\".")
"osmasem", "osmasem2", "osmasem3L",
"MxModel", "mxRAMmodel")))
stop("'object' must be an object of neither class 'meta', 'meta3LFIML',
'wls', 'reml', 'tssem1FEM', 'tssem1REM', 'tssem1FEM.cluster', 'wls.cluster',
'osmasem', 'osmasem2', 'osmasem3L', 'MxModel', or 'mxRAMModel'.")

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

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

Expand Down Expand Up @@ -78,6 +83,11 @@ rerun <- function(object, autofixtau2=FALSE, extraTries=10, ...) {
bestInitsOutput=FALSE,
intervals=TRUE, ...))
}
}
}
## Run it again as the SEs sometimes diasapper
fit <- out$mx.fit
fit <- mxOption(fit, "Calculate Hessian", "Yes")
fit <- mxOption(fit, "Standard Errors", "Yes")
out$mx.fit <- mxRun(fit, silent=TRUE, suppressWarnings=TRUE)
out
}

0 comments on commit 801d5db

Please sign in to comment.