### Install development version from GitHub Source: https://wjschne.github.io/simstandard/index.html Installs the development version of simstandard from GitHub using the remotes package. ```R remotes::install_github("wjschne/simstandard") ``` -------------------------------- ### Install simstandard from CRAN Source: https://wjschne.github.io/simstandard/index.html Installs the most recent stable release of simstandard from CRAN. ```R install.packages("simstandard") ``` -------------------------------- ### Example: specifying matrices and generating lavaan syntax Source: https://wjschne.github.io/simstandard/reference/matrix2lavaan.html Complete example showing how to define measurement, structural, and covariance matrices in different formats (data.frame, matrix) and then call matrix2lavaan to generate lavaan syntax. The output is displayed as comments. ```r library(simstandard) # Specifying the measurement model: # For a data.frame, the column names are latent variables, # and the indicators can be specified as rownames. m <- data.frame(X = c(0.7,0.8,0,0), Y = c(0,0,0.8,0.9)) rownames(m) <- c("A", "B", "C", "D") # Indicator variables can also be specified # as the first column variable # with subsequent column names as latent variables m <- data.frame(Indicators = c("A", "B", "C", "D"), X = c(0.7,0.8,0,0), Y = c(0,0,0.8,0.9)) # Alternately, a matrix can be used: m <- matrix(c(0.7,0.8,0,0, 0,0,0.8,0.9), ncol = 2, dimnames = list(c("A", "B", "C", "D"), c("X", "Y"))) # Specifying the structural coefficients: # The regression coefficients of the structural model can be # specified as either a data.frame or a matrix. Column names # are the predictors and row names are the criterion variables. # With a data.frame, criterion variables can alternataly be # specified with as the first column. s <- matrix(0.5, nrow = 1, ncol = 1, dimnames = list("Y", "X")) # The covariance matrix must be symmetric. Can also be specified # as a data. frame. Sigma <- matrix(c(1, 0.3, 0.3, 1), nrow = 2, ncol = 2, dimnames = list(c("B","C"), c("B","C")) ) model <- matrix2lavaan(measurement_model = m, structural_model = s, covariances = Sigma) cat(model) #> X =~ 0.7 * A + 0.8 * B #> Y =~ 0.8 * C + 0.9 * D #> Y ~ 0.5 * X #> B ~~ 0.3 * C ``` -------------------------------- ### Install remotes package Source: https://wjschne.github.io/simstandard/index.html Installs the remotes package, which is required to install the development version of simstandard from GitHub. ```R install.packages("remotes") ``` -------------------------------- ### Example: sim_standardized_matrices with lavaan model Source: https://wjschne.github.io/simstandard/reference/sim_standardized_matrices.html Example usage of sim_standardized_matrices with a simple lavaan model. The output shows the RAM matrices (A, S, filter_matrix, iA) and correlation matrices (R and R_all) returned by the function. ```r library(simstandard) # lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" sim_standardized_matrices(m) #> $RAM_matrices #> $RAM_matrices$A #> Ob_1 Ob_2 Ob_3 Latent_1 #> Ob_1 0 0 0 0.8 #> Ob_2 0 0 0 0.7 #> Ob_3 0 0 0 0.4 #> Latent_1 0 0 0 0.0 #> #> $RAM_matrices$S #> Ob_1 Ob_2 Ob_3 Latent_1 #> Ob_1 0.36 0.00 0.00 0 #> Ob_2 0.00 0.51 0.00 0 #> Ob_3 0.00 0.00 0.84 0 #> Latent_1 0.00 0.00 0.00 1 #> #> $RAM_matrices$filter_matrix #> Ob_1 Ob_2 Ob_3 Latent_1 #> Ob_1 1 0 0 0 #> Ob_2 0 1 0 0 #> Ob_3 0 0 1 0 #> Latent_1 0 0 0 0 #> #> $RAM_matrices$iA #> Ob_1 Ob_2 Ob_3 Latent_1 #> Ob_1 1 0 0 0.8 #> Ob_2 0 1 0 0.7 #> Ob_3 0 0 1 0.4 #> Latent_1 0 0 0 1.0 #> #> #> $Correlations #> $Correlations$R #> Ob_1 Ob_2 Ob_3 Latent_1 #> Ob_1 1.00 0.56 0.32 0.8 #> Ob_2 0.56 1.00 0.28 0.7 #> Ob_3 0.32 0.28 1.00 0.4 #> Latent_1 0.80 0.70 0.40 1.0 #> #> $Correlations$R_all #> Ob_1 Ob_2 Ob_3 Latent_1 e_Ob_1 #> Ob_1 1.000000e+00 5.600000e-01 3.200000e-01 0.8000000 0.6000000 #> Ob_2 5.600000e-01 1.000000e+00 2.800000e-01 0.7000000 0.0000000 #> Ob_3 3.200000e-01 2.800000e-01 1.000000e+00 0.4000000 0.0000000 #> Latent_1 8.000000e-01 7.000000e-01 4.000000e-01 1.0000000 0.0000000 #> e_Ob_1 6.000000e-01 0.000000e+00 0.000000e+00 0.0000000 1.0000000 #> e_Ob_2 0.000000e+00 7.141428e-01 0.000000e+00 0.0000000 0.0000000 #> e_Ob_3 0.000000e+00 0.000000e+00 9.165151e-01 0.0000000 0.0000000 #> Latent_1_FS 9.265537e-01 8.107345e-01 4.632768e-01 0.8634146 0.3930367 #> e_Ob_1_FS 8.108637e-01 8.545002e-17 0.000000e+00 0.4586159 0.7399517 #> e_Ob_2_FS -8.130540e-17 8.216327e-01 1.596661e-17 0.2870247 -0.3826996 #> e_Ob_3_FS 3.202639e-17 6.033007e-17 9.395729e-01 0.1138741 -0.1518321 #> Latent_1_Composite 8.150834e-01 7.977412e-01 6.936880e-01 0.8237545 0.2601330 #> e_Ob_2 e_Ob_3 Latent_1_FS e_Ob_1_FS #> Ob_1 0.0000000 0.0000000 0.9265537 8.108637e-01 #> Ob_2 0.7141428 0.0000000 0.8107345 8.545002e-17 #> Ob_3 0.0000000 0.9165151 0.4632768 0.000000e+00 #> Latent_1 0.0000000 0.0000000 0.8634146 4.586159e-01 #> e_Ob_1 0.0000000 0.0000000 0.3930367 7.399517e-01 #> e_Ob_2 1.0000000 0.0000000 0.2889398 -4.495335e-01 #> e_Ob_3 0.0000000 1.0000000 0.1286515 -2.001564e-01 #> Latent_1_FS 0.2889398 0.1286515 1.0000000 5.311653e-01 #> e_Ob_1_FS -0.4495335 -0.2001564 0.5311653 1.000000e+00 #> e_Ob_2_FS 0.8691753 -0.1252679 0.3324298 -5.171954e-01 #> e_Ob_3_FS -0.1116189 0.9754593 0.1318881 -2.051919e-01 #> Latent_1_Composite 0.3096202 0.3973597 0.9540659 3.515540e-01 #> e_Ob_2_FS e_Ob_3_FS Latent_1_Composite #> Ob_1 -8.130540e-17 3.202639e-17 0.8150834 #> Ob_2 8.216327e-01 6.033007e-17 0.7977412 #> Ob_3 1.596661e-17 9.395729e-01 0.6936880 #> Latent_1 2.870247e-01 1.138741e-01 0.8237545 #> e_Ob_1 -3.826996e-01 -1.518321e-01 0.2601330 #> e_Ob_2 8.691753e-01 -1.116189e-01 0.3096202 #> e_Ob_3 -1.252679e-01 9.754593e-01 0.3973597 #> Latent_1_FS 3.324298e-01 1.318881e-01 0.9540659 #> e_Ob_1_FS -5.171954e-01 -2.051919e-01 0.3515540 #> e_Ob_2_FS 1.000000e+00 -1.284194e-01 0.3562229 ``` -------------------------------- ### model_complete: Example with lavaan Syntax and Output Source: https://wjschne.github.io/simstandard/reference/model_complete.html Complete example: load simstandard, define a lavaan model with standardized loadings and a path, call model_complete(), and print the result with cat(). The output shows the original model plus a # Variances section with the implied standardized variances. Note that the output lines beginning with #> are the printed response. ```R library(simstandard) # lavaan model m = " Latent_1 =~ 0.9 * Ob_11 + 0.8 * Ob_12 + 0.7 * Ob_13 Latent_2 =~ 0.9 * Ob_21 + 0.6 * Ob_22 + 0.4 * Ob_23 Latent_2 ~ 0.6 * Latent_1 " # Same lavaan syntax, but with standardized variances m_complete <- model_complete(m) cat(m_complete) #> #> Latent_1 =~ 0.9 * Ob_11 + 0.8 * Ob_12 + 0.7 * Ob_13 #> Latent_2 =~ 0.9 * Ob_21 + 0.6 * Ob_22 + 0.4 * Ob_23 #> Latent_2 ~ 0.6 * Latent_1 #> #> # Variances #> Ob_11 ~~ 0.19 * Ob_11 #> Ob_12 ~~ 0.36 * Ob_12 #> Ob_13 ~~ 0.51 * Ob_13 #> Ob_21 ~~ 0.19 * Ob_21 #> Ob_22 ~~ 0.64 * Ob_22 #> Ob_23 ~~ 0.84 * Ob_23 #> Latent_1 ~~ 1 * Latent_1 #> Latent_2 ~~ 0.64 * Latent_2 ``` -------------------------------- ### Example: compute model-implied correlations from lavaan model Source: https://wjschne.github.io/simstandard/reference/get_model_implied_correlations.html Example usage of get_model_implied_correlations with a simple lavaan model. Shows the input model and the output correlation matrix. ```r library(simstandard) # lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" get_model_implied_correlations(m) #> Ob_1 Ob_2 Ob_3 #> Ob_1 1.00 0.56 0.32 #> Ob_2 0.56 1.00 0.28 #> Ob_3 0.32 0.28 1.00 ``` -------------------------------- ### Build RAM matrix from lavaan model and compute total effects Source: https://wjschne.github.io/simstandard/articles/simstandard_tutorial.html This comprehensive R example defines a lavaan model, converts it to a parameter table with lavaanify(), and manually constructs a RAM matrix (A, S, F) to compute total effects (direct + indirect) via matrix algebra. It includes filtering and reshaping of the parameter table, building extended matrices, and plotting the squared total effects. The example also shows the output of eigen() on the exogenous covariance matrix. ```R # lavaan syntax for model m <- " A =~ 0.7 * A1 + 0.8 * A2 + 0.9 * A3 + 0.3 * B1 B =~ 0.7 * B1 + 0.8 * B2 + 0.9 * B3 B ~ 0.6 * A # Variances A1 ~~ 0.51 * A1 A2 ~~ 0.36 * A2 A3 ~~ 0.19 * A3 B1 ~~ 0.168 * B1 B2 ~~ 0.36 * B2 B3 ~~ 0.19 * B3 A ~~ 1 * A B ~~ 0.64 * B " m_fit <- lavaanify(m) # mRAM <- lavMatrixRepresentation(m_fit, # representation = "RAM") v <- list( v_latent_exogenous = lavNames(m_fit, "lv.x"), v_latent_endogenous = lavNames(m_fit, "lv.nox"), v_observed_exogenous = lavNames(m_fit, "ov.x"), v_observed_endogenous = lavNames(m_fit, "ov.nox"), v_observed_indicator = lavNames(m_fit, "ov.ind"), v_disturbance = paste0("d_", lavNames(m_fit, "lv.nox")), v_error = paste0("e_", lavNames(m_fit, "ov.nox")) ) v$v_residual <- c(v$v_error, v$v_disturbance) v$v_exogenous <- c(v$v_residual, v$v_observed_exogenous, v$v_latent_exogenous) v$v_endogenous <- c(v$v_observed_endogenous, v$v_latent_endogenous) v$v_modeled <- c(v$v_endogenous, v$v_latent_exogenous, v$v_observed_exogenous) v$v_observed <- c(v$v_observed_exogenous, v$v_observed_endogenous) v$v_latent <- c(v$v_latent_exogenous, v$v_latent_endogenous) # Reduced ram rram <- list() rram$A <- matrix(0, nrow = length(v$v_modeled), ncol = length(v$v_modeled), dimnames = list(v$v_modeled, v$v_modeled)) rram$S <- rram$A rram$F <- matrix( 0, nrow = length(v$v_observed), ncol = length(v$v_modeled), dimnames = list(v$v_observed, v$v_modeled) ) m_fit |> filter(op == "=~") |> select(rhs, lhs, ustart) #> rhs lhs ustart #> 1 A1 A 0.7 #> 2 A2 A 0.8 #> 3 A3 A 0.9 #> 4 B1 A 0.3 #> 5 B1 B 0.7 #> 6 B2 B 0.8 #> 7 B3 B 0.9 m_fit |> filter(op == "~") |> select(lhs, rhs, ustart) #> lhs rhs ustart #> 1 B A 0.6 d_A <- m_fit |> filter(op %in% c("=~", "~")) |> mutate(cause = if_else(op == "~", rhs, lhs), effect = if_else(op == "~", lhs, rhs)) d_S <- m_fit |> filter(op %in% c("~~")) rram$A[cbind(d_A$effect, d_A$cause)] <- d_A$ustart rram$S[cbind(d_S$lhs, d_S$rhs)] <- d_S$ustart rram$S[cbind(d_S$rhs, d_S$lhs)] <- d_S$ustart rram$F[cbind(v$v_observed, v$v_observed)] <- 1 dimnames(rram$S) <- list(v$v_exogenous, v$v_exogenous) extended_diag <- matrix(0, nrow = nrow(rram$A), ncol = length(v$v_residual)) |> `dimnames<-`(list(rownames(rram$A), v$v_residual)) |> `diag<-`(sqrt(diag(rram$S[v$v_residual, v$v_residual]))) extended_A <- cbind(rram$A, extended_diag) |> rbind(matrix(0, nrow = length(v$v_residual), ncol = length(v$v_residual) + nrow(rram$A))) %>% `rownames<-`(colnames(.)) # Identity matrix the same size as A and S I <- diag(nrow(extended_A)) # Inverted I - A for total effects (direct + indirect) iA <- solve(I - extended_A) # Exogenous covariance matrix extended_S <- matrix(0, nrow = nrow(extended_A), ncol = ncol(extended_A)) |> `dimnames<-`(dimnames(extended_A)) # diag(extended_S) <- 1 extended_S[cbind(rownames(rram$S), colnames(rram$S))] <- 1 extended_cov <- iA %*% extended_S %*% t(iA) extended_mu <- rep(0, nrow(extended_A)) |> `names<-`(rownames(extended_A)) extended_mu[v$v_residual] <- 0 eigen(rram$S, symmetric = T) #> eigen() decomposition #> $values #> [1] 1.000 0.640 0.510 0.360 0.360 0.190 0.190 0.168 #> #> $vectors #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #> [1,] 0 0 1 0 0 0 0 0 #> [2,] 0 0 0 1 0 0 0 0 #> [3,] 0 0 0 0 0 0 1 0 #> [4,] 0 0 0 0 0 0 0 1 #> [5,] 0 0 0 0 1 0 0 0 #> [6,] 0 0 0 0 0 1 0 0 #> [7,] 0 1 0 0 0 0 0 0 #> [8,] 1 0 0 0 0 0 0 0 ((iA %*% extended_S %*% t(iA))[,v$v_exogenous] ^ 2) |> as.data.frame() |> rownames_to_column("variable") |> pivot_longer(-variable, names_to = "exogenous") |> filter(value != 0) |> arrange(exogenous) |> ggplot(aes(variable, value)) + geom_col(aes(fill = exogenous)) ``` -------------------------------- ### Example: Retrieve model variable names Source: https://wjschne.github.io/simstandard/reference/get_model_names.html Defines a lavaan syntax model, calls `get_model_names(m)`, and shows the returned list with variable groups. ```r m <- " A =~ 0.5 * A1 + 0.8 * A2 + 0.8 * A3 B =~ 0.5 * B1 + 0.8 * B2 + 0.8 * B3 B ~ 0.5 * A " get_model_names(m) ``` ```r #> $v_observed #> [1] "A1" "A2" "A3" "B1" "B2" "B3" #> #> $v_latent #> [1] "A" "B" #> #> $v_latent_exogenous #> [1] "A" #> #> $v_latent_endogenous #> [1] "B" #> #> $v_observed_exogenous #> character(0) #> #> $v_observed_endogenous #> [1] "A1" "A2" "A3" "B1" "B2" "B3" #> #> $v_observed_indicator #> [1] "A1" "A2" "A3" "B1" "B2" "B3" #> #> $v_disturbance #> [1] "d_B" #> #> $v_error #> [1] "e_A1" "e_A2" "e_A3" "e_B1" "e_B2" "e_B3" #> #> $v_residual #> [1] "d_B" "e_A1" "e_A2" "e_A3" "e_B1" "e_B2" "e_B3" #> #> $v_factor_score #> [1] "A_FS" "B_FS" #> #> $v_factor_score_disturbance #> [1] "d_B_FS" #> #> $v_factor_score_error #> [1] "e_A1_FS" "e_A2_FS" "e_A3_FS" "e_B1_FS" "e_B2_FS" "e_B3_FS" #> #> $v_composite_score #> [1] "A_Composite" "B_Composite" ``` -------------------------------- ### Example: get_factor_score_coefficients with lavaan syntax Source: https://wjschne.github.io/simstandard/reference/get_factor_score_coefficients.html Example of using get_factor_score_coefficients with a lavaan model string. The model defines two latent variables A and B with loadings, and a regression B ~ 0.5 * A. The output shows the factor score coefficients matrix with columns A_FS and B_FS. ```R m <- " A =~ 0.5 * A1 + 0.8 * A2 + 0.8 * A3 B =~ 0.5 * B1 + 0.8 * B2 + 0.8 * B3 B ~ 0.5 * A " get_factor_score_coefficients(m) #> A_FS B_FS #> A1 0.12977451 0.01656696 #> A2 0.43258168 0.05522319 #> A3 0.43258168 0.05522319 #> B1 0.01656696 0.12977451 #> B2 0.05522319 0.43258168 #> B3 0.05522319 0.43258168 ``` -------------------------------- ### Example: Compute composite scores for two cases Source: https://wjschne.github.io/simstandard/reference/add_composite_scores.html Example from the documentation. It loads simstandard, defines a lavaan model with one latent variable X, creates a data.frame with two cases, and calls add_composite_scores. The output shows the original observed scores plus the X_Composite column. ```r library(simstandard) # lavaan model m = " X =~ 0.9 * X1 + 0.8 * X2 + 0.7 * X3 " # Make data.frame for two cases d <- data.frame( X1 = c(1.2, -1.2), X2 = c(1.5, -1.8), X3 = c(1.8, -1.1)) # Compute composite scores for two cases add_composite_scores(d, m) #> X1 X2 X3 X_Composite #> 1 1.2 1.5 1.8 1.723139 #> 2 -1.2 -1.8 -1.1 -1.569971 ``` -------------------------------- ### Inspect RAM matrices with sim_standardized_matrices Source: https://wjschne.github.io/simstandard/articles/simstandard_tutorial.html Calls sim_standardized_matrices to obtain the RAM matrices. The A matrix contains asymmetric path coefficients (loadings and structural coefficients); the S matrix contains symmetric coefficients (variances and correlations). The example uses the model object m. ```r matrices <- sim_standardized_matrices(m) ``` ```r matrices$RAM_matrices$A %>% ggcor() ``` ```r matrices$RAM_matrices$S %>% ggcor() ``` -------------------------------- ### Example: get_factor_score_validity_se with lavaan model Source: https://wjschne.github.io/simstandard/reference/get_factor_score_validity_se.html Example of using `get_factor_score_validity_se` with a lavaan model string. The function returns a named vector with standard errors for factor scores A_FS and B_FS. The output is shown as comments in the source. ```r m <- " A =~ 0.5 * A1 + 0.8 * A2 + 0.8 * A3 B =~ 0.5 * B1 + 0.8 * B2 + 0.8 * B3 B ~ 0.5 * A " get_factor_score_validity_se(m) #> A_FS B_FS #> 0.4412049 0.4412049 ``` -------------------------------- ### Example: Compute factor scores for two cases Source: https://wjschne.github.io/simstandard/reference/add_factor_scores.html Loads the simstandard library, defines a lavaan model with a single latent variable X, creates a data frame with observed scores for two cases, and calls add_factor_scores to compute factor scores. The output shows the factor score column X_FS. ```r library(simstandard) # lavaan model m = " X =~ 0.9 * X1 + 0.8 * X2 + 0.7 * X3 " # Make data.frame for two cases d <- data.frame( X1 = c(1.2, -1.2), X2 = c(1.5, -1.8), X3 = c(1.8, -1.1)) # Compute factor scores for two cases add_factor_scores(d, m) #> X1 X2 X3 X_FS #> 1 1.2 1.5 1.8 1.435708 #> 2 -1.2 -1.8 -1.1 -1.398951 ``` -------------------------------- ### Use matrix2lavaan to convert matrices to lavaan syntax Source: https://wjschne.github.io/simstandard/articles/simstandard_tutorial.html The matrix2lavaan function combines measurement, structural, and covariance matrices into a lavaan model syntax. Any of the three matrices can be omitted. The example shows the resulting output. ```R model <- matrix2lavaan(measurement_model = m_meas, structural_model = m_struct, covariances = m_cov) cat(model) #> Vocabulary =~ 0.8 * VC1 + 0.9 * VC2 + 0.7 * VC3 #> WorkingMemory =~ 0.6 * WM1 + 0.7 * WM2 + 0.8 * WM3 #> Reading =~ 0.9 * RD1 + 0.7 * RD2 + 0.8 * RD3 #> Reading ~ 0.4 * Vocabulary + 0.3 * WorkingMemory #> Vocabulary ~~ 0.5 * WorkingMemory ``` -------------------------------- ### Remove fixed parameters from a lavaan model Source: https://wjschne.github.io/simstandard/reference/fixed2free.html Example from the documentation. It creates a lavaan model with fixed parameters, applies fixed2free to remove them, and prints the resulting model. The output shows the model without the numeric multipliers. ```r library(simstandard) # lavaan model with fixed parameters m = " Latent_1 =~ 0.9 * Ob_11 + 0.8 * Ob_12 + 0.7 * Ob_13 Latent_2 =~ 0.9 * Ob_21 + 0.6 * Ob_22 + 0.4 * Ob_23 " # Same model, but with fixed parameters removed. m_free <- fixed2free(m) cat(m_free) #> Latent_1 =~ Ob_11 + Ob_12 + Ob_13 #> Latent_2 =~ Ob_21 + Ob_22 + Ob_23 ``` -------------------------------- ### Set seed, simulate data with sim_standardized, and fit with lavaan sem Source: https://wjschne.github.io/simstandard/articles/simstandard_tutorial.html Use set.seed(12) for reproducible simulation. Generate 100,000 observations (latent=FALSE, errors=FALSE) from model m, then fit the free model m_free to the simulated data using lavaan's sem(). Finally, print a summary with standardized estimates and fit measures. The output shows convergence after 27 iterations with CFI=1.000, TLI=1.000, and RMSEA=0.001. ```r set.seed(12) d <- sim_standardized( m, n = 100000, latent = FALSE, errors = FALSE) ``` ```r library(lavaan) lav_results <- sem( model = m_free, data = d) ``` ```r summary( lav_results, standardized = TRUE, fit.measures = TRUE) ``` ```r #> lavaan 0.6.17 ended normally after 27 iterations #> #> Estimator ML #> Optimization method NLMINB #> Number of model parameters 14 #> #> Number of observations 100000 #> #> Model Test User Model: #> #> Test statistic 7.493 #> Degrees of freedom 7 #> P-value (Chi-square) 0.379 #> #> Model Test Baseline Model: #> #> Test statistic 371352.125 #> Degrees of freedom 15 #> P-value 0.000 #> #> User Model versus Baseline Model: #> #> Comparative Fit Index (CFI) 1.000 #> Tucker-Lewis Index (TLI) 1.000 #> #> Loglikelihood and Information Criteria: #> #> Loglikelihood user model (H0) -666610.982 #> Loglikelihood unrestricted model (H1) -666607.236 #> #> Akaike (AIC) 1333249.965 #> Bayesian (BIC) 1333383.146 #> Sample-size adjusted Bayesian (SABIC) 1333338.653 #> #> Root Mean Square Error of Approximation: #> #> RMSEA 0.001 #> 90 Percent confidence interval - lower 0.000 #> 90 Percent confidence interval - upper 0.004 #> P-value H_0: RMSEA <= 0.050 1.000 #> P-value H_0: RMSEA >= 0.080 0.000 #> #> Standardized Root Mean Square Residual: #> #> SRMR 0.001 #> #> Parameter Estimates: #> #> Standard errors Standard #> Information Expected #> Information saturated (h1) model Structured #> #> Latent Variables: #> Estimate Std.Err z-value P(>|z|) Std.lv Std.all #> A =~ #> A1 1.000 0.703 0.703 #> A2 1.142 0.005 231.116 0.000 0.803 0.800 #> A3 1.284 0.005 247.676 0.000 0.903 0.901 #> B1 0.427 0.004 114.547 0.000 0.300 0.300 #> B =~ #> B1 1.000 0.701 0.700 #> B2 1.139 0.005 238.139 0.000 0.798 0.798 #> B3 1.288 0.005 247.366 0.000 0.902 0.901 #> #> Regressions: #> Estimate Std.Err z-value P(>|z|) Std.lv Std.all #> B ~ #> A 0.597 0.004 141.196 0.000 0.599 0.599 #> #> Variances: #> Estimate Std.Err z-value P(>|z|) Std.lv Std.all #> .A1 0.507 0.003 194.579 0.000 0.507 0.506 #> .A2 0.362 0.002 165.904 0.000 0.362 0.359 #> .A3 0.188 0.002 97.276 0.000 0.188 0.188 #> .B1 0.170 0.001 130.982 0.000 0.170 0.169 #> .B2 0.362 0.002 178.775 0.000 0.362 0.362 #> .B3 0.189 0.002 106.863 0.000 0.189 0.189 #> A 0.495 0.004 121.519 0.000 1.000 1.000 #> .B 0.315 0.003 124.248 0.000 0.641 0.641 ``` -------------------------------- ### Compute composite scores with sim_standardized Source: https://wjschne.github.io/simstandard/articles/simstandard_tutorial.html Sets composites=TRUE to calculate equally-weighted composite scores based on the indicators of each latent variable. The output includes a column ending in _Composite. The example uses the same model as the factor score example. ```r m <- " A =~ 0.9 * A1 + 0.8 * A2 + 0.7 * A3 " sim_standardized( m, n = 100000, composites = TRUE ) %>% head() #> # A tibble: 6 × 8 #> A1 A2 A3 A e_A1 e_A2 e_A3 A_Composite #> #> 1 0.138 -0.699 -0.234 -0.446 0.539 -0.342 0.0781 -0.304 #> 2 -0.600 -0.794 0.121 -0.472 -0.176 -0.416 0.452 -0.487 #> 3 -0.339 0.611 0.240 0.150 -0.474 0.491 0.135 0.196 #> 4 1.56 1.38 1.39 1.27 0.417 0.367 0.502 1.66 #> 5 -1.51 -0.985 0.420 -1.46 -0.199 0.182 1.44 -0.795 #> 6 0.527 1.07 2.20 0.416 0.152 0.736 1.91 1.45 ``` -------------------------------- ### model_complete(m) Source: https://wjschne.github.io/simstandard/reference/model_complete.html Takes a lavaan model with standardized paths and loadings and returns a complete lavaan model syntax with standardized variances. ```APIDOC ## model_complete(m) ### Description Takes a lavaan model with standardized paths and loadings and returns a complete lavaan model syntax with standardized variances. ### Usage ```r model_complete(m) ``` ### Arguments - **m** - Structural model represented by lavaan syntax ### Value character string representing lavaan model ### Examples ```r library(simstandard) # lavaan model m = " Latent_1 =~ 0.9 * Ob_11 + 0.8 * Ob_12 + 0.7 * Ob_13 Latent_2 =~ 0.9 * Ob_21 + 0.6 * Ob_22 + 0.4 * Ob_23 Latent_2 ~ 0.6 * Latent_1 " # Same lavaan syntax, but with standardized variances m_complete <- model_complete(m) cat(m_complete) #> #> Latent_1 =~ 0.9 * Ob_11 + 0.8 * Ob_12 + 0.7 * Ob_13 #> Latent_2 =~ 0.9 * Ob_21 + 0.6 * Ob_22 + 0.4 * Ob_23 #> Latent_2 ~ 0.6 * Latent_1 #> #> # Variances #> Ob_11 ~~ 0.19 * Ob_11 #> Ob_12 ~~ 0.36 * Ob_12 #> Ob_13 ~~ 0.51 * Ob_13 #> Ob_21 ~~ 0.19 * Ob_21 #> Ob_22 ~~ 0.64 * Ob_22 #> Ob_23 ~~ 0.84 * Ob_23 #> Latent_1 ~~ 1 * Latent_1 #> Latent_2 ~~ 0.64 * Latent_2 ``` ``` -------------------------------- ### get_factor_score_validity function example Source: https://wjschne.github.io/simstandard/reference/get_factor_score_validity.html Usage example for get_factor_score_validity() with a lavaan model string. Defines a model with latent variables A and B, where B is regressed on A, then computes validity coefficients. The output shows validity coefficients for A_FS and B_FS, both approximately 0.897. ```r m <- " A =~ 0.5 * A1 + 0.8 * A2 + 0.8 * A3 B =~ 0.5 * B1 + 0.8 * B2 + 0.8 * B3 B ~ 0.5 * A " get_factor_score_validity(m) #> A_FS B_FS #> 0.8974064 0.8974064 ```