Title: | Generate Standardized Data |
---|---|
Description: | Creates simulated data from structural equation models with standardized loading. Data generation methods are described in Schneider (2013) <doi:10.1177/0734282913478046>. |
Authors: | W. Joel Schneider [aut, cre] |
Maintainer: | W. Joel Schneider <[email protected]> |
License: | CC0 |
Version: | 0.7.0 |
Built: | 2024-11-09 04:08:16 UTC |
Source: | https://github.com/wjschne/simstandard |
For each latent variable in a structural model, add a composite score to observed data.
add_composite_scores( d, m, mu = 0, sigma = 1, names_suffix = "_Composite", keep_observed_scores = TRUE, ... )
add_composite_scores( d, m, mu = 0, sigma = 1, names_suffix = "_Composite", keep_observed_scores = TRUE, ... )
d |
A data.frame with observed data in standardized form (i.e, z-scores) |
m |
A character string with lavaan model |
mu |
Score means. Composite scores will also have this mean. Defaults to 0. |
sigma |
Score standard deviations. Composite scores will also have this standard deviation. Defaults to 1. |
names_suffix |
A character string added to each composite score name |
keep_observed_scores |
The observed scores are returned along with the composite scores. |
... |
parameters passed to simstandardized_matrices |
data.frame with observed data and estimated factor scores
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)
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)
For each latent variable in a structural model, add an estimated factor score to observed data.
add_factor_scores( d, m, mu = 0, sigma = 1, CI = FALSE, p = 0.95, names_suffix = "_FS", keep_observed_scores = TRUE, ... )
add_factor_scores( d, m, mu = 0, sigma = 1, CI = FALSE, p = 0.95, names_suffix = "_FS", keep_observed_scores = TRUE, ... )
d |
A data.frame with observed data in standardized form (i.e, z-scores) |
m |
A character string with lavaan model |
mu |
Population mean of the observed scores. Factor scores will also have this mean. Defaults to 0. |
sigma |
Population standard deviation of the observed scores. Factor scores will also have this standard deviation. Defaults to 1. |
CI |
Add confidence intervals? Defaults to 'FALSE'. If 'TRUE', for each factor score, a lower and upper bound of the confidence interval is created. For example, the lower bound of factor score 'X' is 'X_LB', and the upper bound is 'X_UB'. |
p |
confidence interval proportion. Defaults to 0.95 |
names_suffix |
A character string added to each factor score name |
keep_observed_scores |
The observed scores are returned along with the factor scores. |
... |
parameters passed to simstandardized_matrices |
data.frame with observed data and estimated factor scores
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)
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)
Remove fixed parameters from a lavaan model
fixed2free(m)
fixed2free(m)
m |
Structural model represented by lavaan syntax |
character string representing lavaan model
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)
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)
Return factor score coefficients
get_factor_score_coefficients(m, latent = TRUE, errors = FALSE, ...)
get_factor_score_coefficients(m, latent = TRUE, errors = FALSE, ...)
m |
Structural model represented by lavaan syntax or output of sim_standardized_matrices function. |
latent |
Include latent variables. |
errors |
Include observed error and latent disturbances variables. |
... |
parameters passed to the 'sim_standardized_matrices' function |
A matrix of factor score coefficients
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)
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)
Return factor score validity coefficients
get_factor_score_validity(m, latent = TRUE, errors = FALSE, ...)
get_factor_score_validity(m, latent = TRUE, errors = FALSE, ...)
m |
Structural model represented by lavaan syntax or output of sim_standardized_matrices function. |
latent |
Include latent variables. |
errors |
Include observed error and latent disturbances variables. |
... |
parameters passed to the 'sim_standardized_matrices' function |
A matrix of validity coefficients
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)
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)
Return factor score validity coefficient standard errors
get_factor_score_validity_se(m, latent = TRUE, errors = FALSE, ...)
get_factor_score_validity_se(m, latent = TRUE, errors = FALSE, ...)
m |
Structural model represented by lavaan syntax or output of sim_standardized_matrices function. |
latent |
Include latent variables. |
errors |
Include observed error and latent disturbances variables. |
... |
parameters passed to the 'sim_standardized_matrices' function |
A matrix of factor score standard errors
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)
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)
Function that takes a lavaan model with standardized parameters and returns a model-implied correlation matrix
get_model_implied_correlations( m, observed = TRUE, latent = FALSE, errors = FALSE, factor_scores = FALSE, composites = FALSE, ... )
get_model_implied_correlations( m, observed = TRUE, latent = FALSE, errors = FALSE, factor_scores = FALSE, composites = FALSE, ... )
m |
Structural model represented by lavaan syntax or output of sim_standardized_matrices function. |
observed |
Include observed variables |
latent |
Include latent variables |
errors |
Include observed error and latent disturbances variables |
factor_scores |
Include factor score variables |
composites |
Include composite variables |
... |
parameters passed to the 'sim_standardized_matrices' function |
A correlation matrix
library(simstandard) # lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" get_model_implied_correlations(m)
library(simstandard) # lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" get_model_implied_correlations(m)
Return model names
get_model_names(m, ...)
get_model_names(m, ...)
m |
Structural model represented by lavaan syntax or output of sim_standardized_matrices function. |
... |
parameters passed to the 'sim_standardized_matrices' function |
A list of variable names
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)
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)
Extract standardized RAM matrices from a lavaan object
lav2ram(fit)
lav2ram(fit)
fit |
An object of class lavaan |
list of RAM matrices A (asymmetric paths), S (symmetric paths), and F (filter matrix)
Create lavaan model syntax from matrix coefficients
matrix2lavaan( measurement_model = NULL, structural_model = NULL, covariances = NULL )
matrix2lavaan( measurement_model = NULL, structural_model = NULL, covariances = NULL )
measurement_model |
A matrix or data.frame with measurement model loadings. Column names are latent variables. Row names or the first column of a data.frame are indicator variables. |
structural_model |
A matrix or data.frame with structural model coefficients (i.e., regressions). Column names are "causal" variables. Row names or the first column of a data.frame are "effect" variables. |
covariances |
A matrix or data.frame with model covariances. Column names must match the row names. If a data.frame, row variable names can be specified in the first column. |
a character string with lavaan syntax
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)
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)
Function that takes a lavaan model with standardized paths and loadings and returns a complete lavaan model syntax with standardized variances
model_complete(m)
model_complete(m)
m |
Structural model represented by lavaan syntax |
character string representing lavaan model
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)
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)
This function takes a lavaan model with standardized parameters and simulates latent scores, errors, disturbances, and observed scores.
sim_standardized( m, n = 1000, observed = TRUE, latent = TRUE, errors = TRUE, factor_scores = FALSE, composites = FALSE, matrices = FALSE, ... )
sim_standardized( m, n = 1000, observed = TRUE, latent = TRUE, errors = TRUE, factor_scores = FALSE, composites = FALSE, matrices = FALSE, ... )
m |
Structural model represented by lavaan syntax |
n |
Number of simulated cases |
observed |
Include observed variables |
latent |
Include latent variables |
errors |
Include observed error and latent disturbances variables |
factor_scores |
Include factor score variables |
composites |
Include composite variables |
matrices |
Include matrices as attribute of tibble |
... |
Arguments passed to 'simstandardized_matrices' |
This function supports the '~' operator for regressions, the '~~' for covariances (but not variances), and the '=~' latent variable loadings. It does not support intercepts (e.g,. 'y ~ 1'), thresholds, scaling factors, formative factors, or equality constraints.
tibble with standardized data
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)
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)
Function that takes a lavaan model with standardized parameters and returns a list with model characteristics
sim_standardized_matrices(m, max_iterations = 100, composite_threshold = NULL)
sim_standardized_matrices(m, max_iterations = 100, composite_threshold = NULL)
m |
Structural model represented by lavaan syntax |
max_iterations |
Maximum number of iterations before the algorithm fails |
composite_threshold |
Loadings with absolute values less than this threshold will not be counted as composite indicators |
This function supports the '~' operator for regressions, the '~~' for covariances (but not variances), and the '=~' latent variable loadings. It does not support intercepts (e.g,. 'y ~ 1'), thresholds, scaling factors, formative factors, or equality constraints.
list of path and covariance coefficients
library(simstandard) # lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" sim_standardized_matrices(m)
library(simstandard) # lavaan model m = "Latent_1 =~ 0.8 * Ob_1 + 0.7 * Ob_2 + 0.4 * Ob_3" sim_standardized_matrices(m)