regStandardizedModel: regStandardizedModel

View source: R/regStandardizedModel.R

regStandardizedModelR Documentation

regStandardizedModel

Description

Note: regmx is based on the R package regsem. Because of the early status of regmx, it is recommended to use regsem instead! regStandardizedModel creates a regularized model from an mxModel in RAM notation. WARNING: Can only handle models, where the "A" matrix contains directeded paths, the "S" matrix undirected paths and "F" is the filter matrix. Only the " matrix can be regularized. The penalty will be introduced to standardized paths. Standardized paths are calculated unsing the formulas provided in Bollen (1989, p.349)

Usage

regStandardizedModel(
  mxModelObject,
  regType = "lasso",
  regIndicator,
  regValues = 0
)

Arguments

mxModelObject

an already run mxModel

regType

so far only "lasso" and "ridge" implemented

regIndicator

matrix indicating which parameters to regularize in "A"

regValues

numeric value depicting the penalty size

Author(s)

Jannik Orzek

Examples

# The following example is adapted from the regsem help to demonstrate the equivalence of both methods:

library(lavaan)
library(OpenMx)
library(regmx)
# put variables on same scale for regsem
HS <- data.frame(scale(HolzingerSwineford1939[,7:15]))

# define variables:
latent = c("f1")
manifest = c("x1","x2","x3","x4","x5", "x6", "x7", "x8", "x9")

# define paths:
loadings <- mxPath(from = latent, to = manifest, free = c(F,T,T,T,T,T,T,T,T), values = 1)
lcov <- mxPath(from = latent, arrows = 2, free = T, values = 1)
lmanif <- mxPath(from = manifest, arrows =2 , free =T, values = 1)

# define model:
myModel <- mxModel(name = "myModel", latentVars = latent, manifestVars = manifest, type = "RAM",
                   mxData(observed = HS, type = "raw"), loadings, lcov, lmanif,
                   mxPath(from = "one", to = manifest, free = T)
)

fit_myModel <- mxRun(myModel)

# Show the names of the matrices in the model:
names(fit_myModel$matrices)
# Show the values of the directional paths:
mxStandardizeRAMpaths(fit_myModel)

# Penalize specific parameters from the A matrix (directional paths):
regOn <- c("A")

selectedA <- matrix(0, ncol = ncol(fit_myModel$A$values), nrow = nrow(fit_myModel$A$values))
selectedA[c(2,3,7,8,9),10] <-1 # parameters that should be regularized have to be marked with 1
regIndicators <- list("A" = selectedA) # save in a list. Note the naming of the list element

# size of the penalty:
regValues = .2

reg_model <- regStandardizedModel(mxModelObject = fit_myModel, regType = "lasso",regIndicator = selectedA, regValues = regValues)
fit_reg_model <- mxRun(reg_model)
fit_reg_model$standardizedPaths


jhorzek/regmx documentation built on Sept. 19, 2022, 2:30 a.m.