semproducible: Generate R code to reproduce a SEM model

Description Usage Arguments Value Examples

View source: R/semproducible.r

Description

Generate R code from your data frame, an existing covariance matrix, or a lavaan object. 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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
semproducible(
  x,
  formula = NULL,
  target_variable = "cov_mat",
  digits = NULL,
  drop_non_numeric = FALSE,
  vars_per_line = 9,
  eval = FALSE,
  use = "complete.obs",
  template = NULL
)

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.

target_variable

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

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 should be excluded from the covariance matrix. Defaults to FALSE.

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. A NULL value (default) will use the default template from code_template.

Value

a character string with the generated R code.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## 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))

# Generate R code for the data.
code <- semproducible(data, formula="y ~ x")

# Use 5 decimal digits.
code <- semproducible(data, formula="y ~ x", digits=5)

# Restrict number of values per line to 4.
code <- semproducible(data, formula="y ~ x", digits=5, vars_per_line=4)

# View code.
cat(code)

# Save code to a file.
save_code(code, "create_data.r")

# Reproduce lavaan object (from 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 Sept. 4, 2020, 8:02 a.m.