semproducible: Generate R code to reproduce a latent variable model (lavaan)

View source: R/semproducible.R

semproducibleR Documentation

Generate R code to reproduce a latent variable model (lavaan)

Description

Generate R code from your data frame, an existing covariance matrix, or a lavaan object.

Usage

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()
)

Arguments

x

a data.frame with numeric variables, a matrix with covariances, or a fitted lavaan object that you want to reproduce.

formula

character string with a custom lavaan formula syntax (e.g. y ~ x) that should be included in the code. If formula is NULL, and x is a lavaan object, then a formula is generated automatically from the lavaan object.

covmat_variable

character string with arbitrary variable name for the generated covariance matrix. Defaults to "cov_mat".

fit_variable

character string with arbitrary target variable name for the lavaan object. Defaults to "fit".

digits

number of decimal digits of the covariance matrix. The default (NULL) will show all available digits as specified by your R options. The higher the number of decimal digits, the more accurate the reproducible model will be.

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 FALSE.

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 lavaan model will be executed during generation. If TRUE, a message will tell you whether the code executed with errors or not.

use

character string for computing the covariances in the presence of missing values. This value is simply passed on to the use parameter of the cov function. The value must be everything, all.obs, complete.obs, na.or.complete, or pairwise.complete.obs.

template

a character string with a custom code template that is used when generating the R code. The default value is code_template which contains code for lavaan.

Details

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.

Value

a character string with the generated R code.

Examples

## 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)

peterdalle/semproducible documentation built on April 1, 2022, 8:47 p.m.