lavPredictY_cv.RdThis function can be used to determine an optimal lambda value for the
lavPredictY function. based on cross-validation.
An object of class lavaan.
A data.frame, containing the same variables as the data.frame that
was used when fitting the model in object.
The names of the observed variables that should be treated as the x-variables. Can also be a list to allow for a separate set of variable names per group (or block).
The names of the observed variables that should be treated as the y-variables. It is for these variables that the function will predict the (model-based) values for each observation. Can also be a list to allow for a separate set of variable names per group (or block).
Integer. The number of folds to be used during cross-validation.
An R seq() containing the range of lambda penalty
values to be tested during cross-validation.
This function is used to generate an optimal lambda value for
lavPredictY predictions to improve prediction accuracy.
lavPredictY to predict the values of (observed) y-variables given
the values of (observed) x-variables in a structural equation model.
de Rooij, M., Karch, J.D., Fokkema, M., Bakk, Z., Pratiwi, B.C, and Kelderman, H. (2022) SEM-Based Out-of-Sample Predictions, Structural Equation Modeling: A Multidisciplinary Journal. DOI:10.1080/10705511.2022.2061494
Molina, M. D., Molina, L., & Zappaterra, M. W. (2024). Aspects of Higher Consciousness: A Psychometric Validation and Analysis of a New Model of Mystical Experience. doi:10.31219/osf.io/cgb6e
colnames(PoliticalDemocracy) <- c("z1", "z2", "z3", "z4",
"y1", "y2", "y3", "y4",
"x1", "x2", "x3")
model <- '
# latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ z1 + z2 + z3 + z4
dem65 =~ y1 + y2 + y3 + y4
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual correlations
z1 ~~ y1
z2 ~~ z4 + y2
z3 ~~ y3
z4 ~~ y4
y2 ~~ y4
'
fit <- sem(model, data = PoliticalDemocracy, meanstructure = TRUE)
#> Warning: lavaan->lav_model_vcov():
#> The variance-covariance matrix of the estimated parameters (vcov) does not
#> appear to be positive definite! The smallest eigenvalue (= -1.239244e+00)
#> is smaller than zero. This may be a symptom that the model is not
#> identified.
percent <- 0.5
nobs <- lavInspect(fit, "ntotal")
idx <- sort(sample(x = nobs, size = floor(percent*nobs)))
xnames = c("z1", "z2", "z3", "z4", "x1", "x2", "x3")
ynames = c("y1", "y2", "y3", "y4")
reg.results <- lavPredictY_cv(
fit,
PoliticalDemocracy[-idx, ],
xnames = xnames,
ynames = ynames,
n.folds = 10L,
lambda.seq = seq(from = .6, to = 2.5, by = .1)
)
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 1
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 2
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 3
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 4
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 5
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 6
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 7
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 8
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 9
#> Warning: lavaan->lavPredictY_cv():
#> failed fit in fold %s 10
lam <- reg.results$lambda.min
lavPredictY(fit, newdata = PoliticalDemocracy[idx,],
ynames = ynames,
xnames = xnames,
lambda = lam)
#> y1 y2 y3 y4
#> [1,] NA NA NA NA
#> [2,] NA NA NA NA
#> [3,] NA NA NA NA
#> [4,] NA NA NA NA
#> [5,] NA NA NA NA
#> [6,] NA NA NA NA
#> [7,] NA NA NA NA
#> [8,] NA NA NA NA
#> [9,] NA NA NA NA
#> [10,] NA NA NA NA
#> [11,] NA NA NA NA
#> [12,] NA NA NA NA
#> [13,] NA NA NA NA
#> [14,] NA NA NA NA
#> [15,] NA NA NA NA
#> [16,] NA NA NA NA
#> [17,] NA NA NA NA
#> [18,] NA NA NA NA
#> [19,] NA NA NA NA
#> [20,] NA NA NA NA
#> [21,] NA NA NA NA
#> [22,] NA NA NA NA
#> [23,] NA NA NA NA
#> [24,] NA NA NA NA
#> [25,] NA NA NA NA
#> [26,] NA NA NA NA
#> [27,] NA NA NA NA
#> [28,] NA NA NA NA
#> [29,] NA NA NA NA
#> [30,] NA NA NA NA
#> [31,] NA NA NA NA
#> [32,] NA NA NA NA
#> [33,] NA NA NA NA
#> [34,] NA NA NA NA
#> [35,] NA NA NA NA
#> [36,] NA NA NA NA
#> [37,] NA NA NA NA