#create a P x M population factor loading matrix#
Ld0 <- diag(1, 9) %x% matrix(c(.8,.75,.8), 3, 1)
#create a M x M population path coefficients matrix#
Bt0 <- matrix(0, 9, 9)
Bt0[2, 1] = Bt0[3, 2] = Bt0[5, 4] = Bt0[6, 5] = Bt0[8, 7] = Bt0[9, 8] =.45
Bt0[4, 1] = Bt0[5, 2] = Bt0[6, 3] = Bt0[7, 4] = Bt0[8, 5] = Bt0[9, 6]= .55
Bt0iv <- solve(diag(1, 9) - Bt0)
#create a M x M population residual covariance matrix#
Ph0 <- diag(0, 9)
Ph0[1, 1] <- 1
for (m in 2:9) {Ph0[m, m] <- (1 - sum((Bt0iv[m,] ^ 2) * diag(Ph0)))}
#create a P x P population measurement error matrix#
Ps0 <- diag(c(.36, 0.4375, .36), 27)
#create a P x M population covariance matrix#
Sg0 <- Ld0 %*% Bt0iv %*% Ph0 %*% t(Bt0iv) %*% t(Ld0) + Ps0
#create a P x M pattern matrix for factor loadings#
Ldp <- (Ld0 != 0)
Ldp[1, 1] = Ldp[4, 2] = Ldp[7, 3] = Ldp[10, 4] = Ldp[13, 5] = 0
Ldp[16, 6] = Ldp[19, 7] = Ldp[22, 8] = Ldp[25, 9] = 0
#create a P x M pattern matrix for path coefficients#
Btp <- matrix(0, 9, 9)
Btp[lower.tri(Btp)] <- -1
#specify field pattern, value, and penalty#
pattern <- list(Ldp = Ldp, Btp = Btp)
value <- list(Ld = Ld0, Bt = Bt0)
penalty <- list(type = "mcp", gm_all = seq(0.025, .15, .025), dt_all = 2)
#generate data with N = 400 and P = 27#
Z <- matrix(rnorm(400 * 27, 0, 1), 400, 27)
Y <- Z %*% eigen(Sg0)$vectors %*% diag(sqrt(eigen(Sg0)$values)) %*% t(eigen(Sg0)$vectors)
Y <- as.data.frame(Y)
#create lslSEM object#
rc_sem <- lsl:::lslSEM(data = Y, pattern = pattern, value = value, penalty = penalty)
#check the specification through method check()#
rc_sem$check()
#obtain the estimates under each pair of gamma and dt through method learn()#
rc_sem$learn()
#obtain the final model based on bic through method fit()#
rc_sem$fit(criterion = "bic")
#see overall model information and fit indices of final model#
rc_sem$fit_summary
#see estimated Bt of final model#
rc_sem$fit_value$Bt
#see the solution path parameters in Bt#
rc_sem$plot_path(mat_name = "Bt")
Run the code above in your browser using DataLab