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)
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)
)
lam <- reg.results$lambda.min
lavPredictY(fit, newdata = PoliticalDemocracy[idx,],
ynames = ynames,
xnames = xnames,
lambda = lam)
Run the code above in your browser using DataLab