### Install simstandard from GitHub Source: https://github.com/wjschne/simstandard/blob/main/README.md Installs the development version of simstandard from GitHub using the remotes package. ```r remotes::install_github("wjschne/simstandard") ``` -------------------------------- ### Install simstandard from CRAN Source: https://github.com/wjschne/simstandard/blob/main/README.md Installs the most recent stable release of simstandard from CRAN. ```r install.packages("simstandard") ``` -------------------------------- ### Install remotes package Source: https://github.com/wjschne/simstandard/blob/main/README.md Installs the remotes package, which is required to install the development version of simstandard from GitHub. ```r install.packages("remotes") ``` -------------------------------- ### Generate lavaan syntax from matrices Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/matrix2lavaan.html Complete example showing how to specify measurement, structural, and covariance matrices and generate lavaan syntax. The output is displayed as comments after the cat() call. ```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 ``` -------------------------------- ### Vignette setup: load packages and set seed Source: https://github.com/wjschne/simstandard/blob/main/vignettes/simstandard_tutorial.Rmd Setup chunk for the vignette: sets knitr options, loads required packages (lavaan, simstandard, knitr, ggplot2, tibble, tidyr, dplyr), and sets a random seed for reproducibility. ```r knitr::opts_chunk$set( collapse = TRUE, comment = "#>", out.height = "100%", out.width = "100%", fig.width = 7, fig.height = 7 ) library(lavaan) library(simstandard) library(knitr) library(ggplot2) library(tibble) library(tidyr) library(dplyr) set.seed(123456) ``` -------------------------------- ### Evaluate model fit and extract RAM paths Source: https://github.com/wjschne/simstandard/blob/main/docs/articles/simstandard_tutorial.html This example generates data based on a model, evaluates the fit using lavaan, and extracts the RAM paths for visualization. ```R # Set the random number generator for reproducible results set.seed(12) # Generate data based on model m d <- sim_standardized( m, n = 100000, latent = FALSE, errors = FALSE) # Evaluate the fit of model m_free on data d library(lavaan) lav_results <- sem( model = m_free, data = d) # Display summary of model summary( lav_results, standardized = TRUE, fit.measures = TRUE) #> 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) 133338.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 # Extract RAM paths ram <- lav2ram(lav_results) # Display asymmetric paths (i.e., single-headed arrows for # loadings and structure coefficients) ram$A %>% ggcor() ``` ```R # Display symmetric paths (i.e., curved double-headed arrows # exogenous variances, error variances, disturbance variances, # and any covariances among these) ram$S %>% ggcor() ``` -------------------------------- ### Example: sim_standardized_matrices with lavaan model Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/sim_standardized_matrices.html Demonstrates using sim_standardized_matrices with a simple lavaan model. The output includes RAM matrices (A, S, filter_matrix, iA) and correlation matrices (R, R_all). ```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 2.813249e-17 -3.750998e-17 0.4586159 0.7399517 #> e_Ob_2_FS 2.075660e-16 8.216327e-01 1.117663e-16 0.2870247 -0.3826996 #> e_Ob_3_FS 5.690770e-17 5.690770e-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 2.813249e-17 #> Ob_3 0.0000000 0.9165151 0.4632768 -3.750998e-17 #> 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 2.075660e-16 5.690770e-17 0.8150834 #> Ob_2 8.216327e-01 5.690770e-17 0.7977412 #> Ob_3 1.117663e-16 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 ``` -------------------------------- ### Create lavaan syntax from measurement, structural, and covariance matrices Source: https://github.com/wjschne/simstandard/blob/main/man/matrix2lavaan.Rd This example builds the three matrix components for matrix2lavaan: a measurement model 'm' (demonstrated with a data.frame using rownames, a data.frame using a first column for indicators, and a matrix), a structural model 's' with coefficient 0.5 from X to Y, and a symmetric covariance matrix 'Sigma' for B and C. It then calls matrix2lavaan and prints the generated lavaan syntax with cat(). ```R library(simstandard) ``` ```R 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") ``` ```R 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)) ``` ```R 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"))) ``` ```R s <- matrix(0.5, nrow = 1, ncol = 1, dimnames = list("Y", "X")) ``` ```R Sigma <- matrix(c(1, 0.3, 0.3, 1), nrow = 2, ncol = 2, dimnames = list(c("B","C"), c("B","C")) ) ``` ```R model <- matrix2lavaan(measurement_model = m, structural_model = s, covariances = Sigma) cat(model) ``` -------------------------------- ### Example: get_factor_score_validity_se with lavaan model Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/get_factor_score_validity_se.html Example usage: defines a lavaan model with two latent factors (A and B) and a regression path, then calls get_factor_score_validity_se on it. The output shows the standard errors for A_FS and B_FS, both 0.4412049. ```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: add_factor_scores with lavaan model Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/add_factor_scores.html Example usage of add_factor_scores with a lavaan model and a data.frame of observed scores. The output shows the original columns plus 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 ``` -------------------------------- ### Example: Remove fixed parameters from lavaan model Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/fixed2free.html Loads simstandard, defines a lavaan model with fixed parameters, calls fixed2free to remove them, and prints the result. The output shows the model without the fixed loadings. ```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 ``` -------------------------------- ### Example: Compute composite scores for two cases Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/add_composite_scores.html This example loads the simstandard package, defines a lavaan model with a single latent variable X, creates a data.frame with two cases, and calls add_composite_scores to compute 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 ``` -------------------------------- ### Complete lavaan model with standardized variances Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/model_complete.html Example usage of model_complete with a lavaan model. The output shows the original model syntax plus added variance terms for all observed and latent variables. ```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 ``` -------------------------------- ### Simulate data with sim_standardized Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/sim_standardized.html Example usage of sim_standardized with a lavaan model. It simulates 10 cases and prints the resulting tibble with observed variables, latent scores, and error terms. The output shows the structure of the returned tibble. ```r library(simstandard) # Lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" # simulate 10 cases sim_standardized(m, n = 10) #> # A tibble: 10 × 7 #> Ob_1 Ob_2 Ob_3 Latent_1 e_Ob_1 e_Ob_2 e_Ob_3 #> #> 1 -0.844 0.178 -2.24 -0.00557 -0.840 0.182 -2.23 #> 2 0.175 0.647 -1.77 -0.247 0.373 0.820 -1.67 #> 3 0.357 0.238 -0.256 0.629 -0.147 -0.202 -0.507 #> 4 -0.251 -2.47 -0.276 -1.86 1.24 -1.16 0.470 #> 5 -1.04 -0.677 0.132 -0.914 -0.313 -0.0376 0.498 #> 6 0.871 0.776 -0.901 0.738 0.281 0.259 -1.20 #> 7 1.12 -0.0808 -0.864 -0.0160 1.13 -0.0696 -0.858 #> 8 -0.355 -0.957 0.928 0.176 -0.496 -1.08 0.857 #> 9 0.0390 1.07 0.0491 -0.134 0.146 1.16 0.103 #> 10 -0.292 0.548 0.140 1.07 -1.15 -0.199 -0.287 ``` -------------------------------- ### Compute factor scores with add_factor_scores Source: https://github.com/wjschne/simstandard/blob/main/man/add_factor_scores.Rd Example from the add_factor_scores documentation. It loads the simstandard package, defines a lavaan model with a single latent variable X measured by X1, X2, and X3, creates a data.frame with two cases of observed scores, and calls add_factor_scores to compute factor scores for those cases. ```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) ``` -------------------------------- ### Example: get_model_implied_correlations with lavaan model Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/get_model_implied_correlations.html Example usage of get_model_implied_correlations with a lavaan model. The model defines a latent variable with three observed indicators. The output is a correlation matrix of the observed variables. ```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 ``` -------------------------------- ### Manual model matrix construction and calculation Source: https://github.com/wjschne/simstandard/blob/main/vignettes/simstandard_tutorial.Rmd Demonstrates manual construction of RAM matrices from lavaan syntax, calculation of total effects, and visualization of exogenous variable contributions. ```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) m_fit |> filter(op == "~") |> select(lhs, rhs, ustart) 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) ((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)) (iA %*% extended_S %*% t(iA))[v$v_endogenous,v$v_exogenous] ``` -------------------------------- ### Add composite scores to new data with add_composite_scores Source: https://github.com/wjschne/simstandard/blob/main/docs/articles/simstandard_tutorial.html Use add_composite_scores to add equally-weighted composite scores to a new dataset. The example uses the same data d and model m from the previous example, and shows the resulting data frame with the A_Composite column. ```R add_composite_scores(d, m) #> A1 A2 A3 A_Composite #> 1 2 2.5 1.3 2.220935 #> 2 -1 -1.5 -2.1 -1.761431 ``` -------------------------------- ### Convert lavaan model syntax with fixed2free Source: https://github.com/wjschne/simstandard/blob/main/docs/articles/simstandard_tutorial.html Demonstrates defining a lavaan model with fixed parameters and using fixed2free to generate an equivalent model with all parameters set free. ```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 " # Make model m free m_free <- fixed2free(m) # Display model m_free cat(m_free) #> B ~ A #> A =~ A1 + A2 + A3 + B1 #> B =~ B1 + B2 + B3 ``` -------------------------------- ### get_factor_score_coefficients function usage and example Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/get_factor_score_coefficients.html This example defines a lavaan model with two latent variables and a regression, then uses get_factor_score_coefficients to compute factor score coefficients. The output shows a matrix with columns for each latent variable's factor score. ```r get_factor_score_coefficients(m, latent = TRUE, errors = FALSE, ...) ``` ```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 ``` -------------------------------- ### Generate lavaan syntax with matrix2lavaan Source: https://github.com/wjschne/simstandard/blob/main/docs/articles/simstandard_tutorial.html Calls matrix2lavaan with the measurement, structural, and covariance matrices, then prints the resulting lavaan syntax. The output shows the generated model syntax with factor loadings, regressions, and covariances. ```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 ``` -------------------------------- ### Example: get_factor_score_validity with lavaan model Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/get_factor_score_validity.html Example of using get_factor_score_validity with a lavaan model syntax. The model defines two latent factors A and B with loadings, and a regression of B on A. The output shows validity coefficients for A_FS and B_FS, both approximately 0.8974064. ```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 ``` -------------------------------- ### model_complete(m) Source: https://github.com/wjschne/simstandard/blob/main/docs/reference/model_complete.html Takes a lavaan model with standardized paths and loadings and returns a complete lavaan model syntax with standardized variances. Accepts a structural model represented by lavaan syntax and returns a character string representing the complete lavaan model. ```APIDOC ## Function: model_complete(m) ### Description Takes a lavaan model with standardized paths and loadings and returns a complete lavaan model syntax with standardized variances. ### Signature model_complete(m) ### Parameters #### Arguments - **m** (lavaan syntax string) - Required - Structural model represented by lavaan syntax ### Returns - **character string** - Complete lavaan model syntax with standardized variances ### Example ```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 ``` ```