SingleCoreOptimRegModel: SingleCoreOptimRegModel

View source: R/SingleCoreOptimRegModel.R

SingleCoreOptimRegModelR Documentation

SingleCoreOptimRegModel

Description

Note: regmx is based on the R package regsem. Because of the early status of regmx, it is recommended to use regsem instead! SingleCoreOptimRegModel creates a range of regularized models from an mxModel. It automatically tests different penalty values and returns the best model. It only uses a single core.

Usage

SingleCoreOptimRegModel(
  mxModelObject,
  alpha = 1,
  gamma = 0,
  regOn,
  regIndicators,
  regValues,
  criterion = "BIC",
  autoCV = FALSE,
  k = 5,
  Boot = FALSE,
  manualCV = NULL,
  zeroThresh = 0.001,
  scaleCV = TRUE,
  cores = 1
)

Arguments

mxModelObject

an already run mxModel

alpha

alpha controls the type of penalty. For lasso regularization, set alpha = 1, for ridge alpha = 0. Values between 0 and 1 implement elastic net regularization

gamma

gamma sets the power in the denominator of parameter specific weights when using adaptive lasso regularization. Make sure to set alpha to 1 when using a gamma other than 0.

regOn

string vector with matrices that should be regularized. The matrices must have the same name as the ones provided in the mxModelObject (e.g., "A")

regIndicators

list of matrices indicating which parameters to regularize in the matrices provided in regOn. The matrices in regIndicators must to have the same names as the matrices they correspond to (e.g., regIndicators = list("A" = diag(10))). 1 Indicates a parameter that will be regularized, 0 an unregularized parameter

criterion

Criterion for chosing the best final model. Possible are: AIC, BIC, CV.m2LL (only if manualCV is provided)

autoCV

logical indicating if cross-validation should be performed automatically

k

number of splits performed when autoCV = TRUE

Boot

logical indicating if Bootstrap should be performed. Not yet implemented!

manualCV

if cross-validation should be performed manually, provide a cross-validation sample (has to be of the same class as the data in the mxModelObject; e.g., mxData)

zeroThresh

Threshold at which parameters are evaluated as being zero (necessary for AIC and BIC)

scaleCV

indicate if the CV samples should be scaled automatically

regValue

numeric value depicting the penalty size

regValue_start

initial penalty value (recommended: 0)

regValue_end

highest penalty value tested

regValue_stepsize

increase in penValue between iterations

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)
# 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)

# 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

# Run the models:

reg_model <- optimRegModel(mxModelObject = fit_myModel, alpha = 1, gamma = 0, regOn  = regOn, regIndicators = regIndicators)

reg_model$`fit measures`

reg_model$`best penalty`

# Run the same model with 5-fold cross-validation

CV_reg_model <- optimRegModel(mxModelObject = fit_myModel, alpha = 1, gamma = 0, regOn  = regOn, regIndicators = regIndicators,
                              autoCV = T, k = 5)
CV_reg_model$`CV results`


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