#Example 1: Factor Analysis Model#
#create a P x M population factor loading matrix#
Ld0 <- diag(1, 4) %x% matrix(c(.8, .65, .75, .65, .8), 5, 1)
Ld0[2, 2] = Ld0[7, 3] = Ld0[12, 4] = Ld0[17, 1] = .5
Ld0[9, 1] = Ld0[14, 2] = Ld0[19, 3] = Ld0[4, 4] = .5
#create a M x M population factor covariance matrix#
Ph0 <- 0.3 * matrix(1, 4, 1) %*% matrix(1, 1, 4) + diag( .7, 4)
#create a P x P population covariance matrix#
Sg0 <- Ld0 %*% Ph0 %*% t(Ld0)
diag(Sg0) <- 1
#create a P x P population measurement error covariance matrix#
Ps0 <- Sg0 - Ld0 %*% Ph0 %*% t(Ld0)
#create a P x M pattern matrix for factor loadings#
Ldp <- 1*(Ld0!=0)
Ldp[Ld0 < 0.6] = -1
Ldp[1, 1] = Ldp[6, 2] = Ldp[11, 3] = Ldp[16, 4] = 0
#create a M x M pattern matrix for factor covariance#
Php <- matrix(1, 4, 4)
#specify field pattern, value, and penalty#
pattern <- list(Ldp = Ldp, Php = Php)
value <- list(Ld = Ld0, Ps = Ps0, Ph = Ph0)
penalty <- list(type = "mcp", gm_all = seq(0.03, .12, .03), dt_all = 1.5)
#generate data with N = 400 and P = 20#
Z <- matrix(rnorm(400 * 20, 0, 1), 400, 20)
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 Ld of final model#
rc_sem$fit_value$Ld
#see the plot for likelihood, aic, and bic under the given gamma and delta values#
rc_sem$plot_validation()
#################################################################################
#Example 2: A general SEM Model#
#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.03, .12, .03), 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 <- 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