View source: R/semproducible.R
semproducible | R Documentation |
Generate R code from your data frame, an existing covariance matrix, or
a lavaan
object.
semproducible( x, formula = NULL, covmat_variable = "cov_mat", fit_variable = "fit", digits = NULL, drop_non_numeric = FALSE, comments = TRUE, vars_per_line = 9, eval = FALSE, use = "complete.obs", template = code_template() )
x |
a |
formula |
character string with a custom lavaan formula syntax
(e.g. |
covmat_variable |
character string with arbitrary variable name
for the generated covariance matrix. Defaults to |
fit_variable |
character string with arbitrary target variable name
for the lavaan object. Defaults to |
digits |
number of decimal digits of the covariance matrix. The
default ( |
drop_non_numeric |
whether non-numeric columns should be dropped
from the data frame. This is useful if you have characters or factors as
columns, which should0 be excluded from the covariance matrix. Defaults to
|
comments |
whether the generated code should include comments that describe the code. Defaults to TRUE. |
vars_per_line |
number of variables/values per line. Use a low value to decrease the width of the generated code. |
eval |
whether the generated code and |
use |
character string for computing the covariances in the presence
of missing values. This value is simply passed on to the |
template |
a character string with a custom code template that
is used when generating the R code. The default value is
|
The R code reproduces your structural equation model (SEM) to support open science with minimal effort, and makes it easy to supply your code to a journal article, or to share your model with others without sharing your full dataset that may contain sensitive information.
Semproducible is useful when you need to create a reproducible covariance
matrix that can be used by a structural equation model (SEM) in
lavaan
.
You supply a data frame with numeric variables (or a covariance matrix that
you have already prepared). semproducible
then generates R code that
can reproduce the SEM model as a covariance matrix.
You can also directly supply a lavaan SEM model, and semproducible will use the fitted (observed) covariance matrix and produce R code that reproduce the model, using the estimator of your lavaan model. Note, however, that this will not give you all the possible models that you could have ran with all the data, but only the variables that are passed to lavaan.
Save the code to a file using the
save_code
function.
a character string with the generated R code.
## Not run: library(semproducible) library(lavaan) # Create random data set.seed(5543) data <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100), w = rnorm(100), q = rnorm(100)) code <- semproducible(data, formula="y ~ x") code <- semproducible(data, formula="y ~ x", digits=5) code <- semproducible(data, formula="y ~ x", digits=5, vars_per_line=4) # View code code save_code(code, "create_data.R") # Example: http://lavaan.ugent.be/tutorial/cfa.html HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model, data=HolzingerSwineford1939) code <- semproducible(fit) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.