semproducible
Generates R code for your lavaan models automatically, which helps with reproducible open science
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (9.9%) to scientific vocabulary
Keywords
Repository
Generates R code for your lavaan models automatically, which helps with reproducible open science
Basic Info
Statistics
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
- Releases: 0
Topics
Metadata Files
README.md
Semproducible reproduce a lavaan model
Semproducible is an R package that makes your latent variable models in lavaan reproducible. Semproducible generates all the necessary data and R code from your existing lavaan model.
This package makes it possible to reproduce a model without disclosing the raw data (which might be sensitive).
You can use semproducible in two ways:
- Generate reproducible code from a lavaan model
- Generate reproducible code from a data frame
Benefits
- Only one line of code to reproduce your model
- The generated code fit a journal appendix
- Researchers can reproduce your model (and try alternative model specifications)
Install
r
devtools::install_github("peterdalle/semproducible")
Usage
r
semproducible(x, formula)
Where x is either a lavaan model or a data frame.
And formula is the lavaan formula syntax (e.g., ind60 =~ x1 + x2 + x3).
Examples
1. Generate code from a lavaan model
Let's say you have the following lavaan model, and you want to make it reproducible:
```r library(lavaan)
Example: http://lavaan.ugent.be/tutorial/sem.html
formula <- "# latent variables ind60 =~ x1 + x2 + x3 dem60 =~ y1 + y2 + y3 + y4 dem65 =~ y5 + y6 + y7 + y8 # regressions dem60 ~ ind60 dem65 ~ ind60 + dem60 # residual covariances y1 ~~ y5 y2 ~~ y4 + y6 y3 ~~ y7 y4 ~~ y8 y6 ~~ y8"
fit <- sem(formula, data=PoliticalDemocracy) ```
The only thing you need to do is to pass fit and formula into semproducible:
```r library(semproducible)
semproducible(fit, formula) ```
2. Generate code from a data frame
If you have a data frame with many variables, you can reproduce the entire data frame and let other researchers choose variables to model.
```r library(semproducible) library(tidyverse)
iris %>% select(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) %>% semproducible(formula = "Sepal.Length ~ Sepal.Width + Petal.Length") ```
3. Save the generated code to a file
```r code <- semproducible(fit, formula)
save_code(code, "filename.R") ```
What the generated code looks like
If you run example 2 above, the generated code will look like this:
```r library(tibble) library(lavaan)
Number of observations.
observations <- 150
Covariance matrix.
cov_mat <- tribble(~Sepal.Length, ~Sepal.Width, ~Petal.Length, ~Petal.Width,
0.685693512304251, -0.0424340044742729, 1.27431543624161, 0.516270693512304,
-0.0424340044742729, 0.189979418344519, -0.329656375838926, -0.12163937360179,
1.27431543624161, -0.329656375838926, 3.11627785234899, 1.29560939597315,
0.516270693512304, -0.12163937360179, 1.29560939597315, 0.581006263982103)
Convert data frame to matrix (that lavaan can handle).
covmat <- as.matrix(covmat)
Rows should have names too.
rownames(covmat) <- colnames(covmat)
SEM model in lavaan syntax.
formula <- 'Sepal.Length ~ Sepal.Width + Petal.Length'
Fit SEM model.
fit <- lavaan::sem(formula, sample.cov = cov_mat, sample.nobs = observations)
Show results.
summary(fit) ```
And if you then run the generated code above, you will notice that it has been successfully reproduced.
Questions
- Do I need to specify all columns?
- How do I control the width of the code?
- Can I use semproducible in a
tidyversepipeline? - How do I know that my generated code is correct?
- Why doesn't my generated code produce the exact same result?
Documentation
Load semproducible and then run ?semproducible in the R console to view the full documentation.
Support
Report problems or request a new feature by submitting a new issue.
Contribute
You can help with:
- Test semproducible in your project, and report a bug
- Read the documentation and verify that it corresponds to actual behavior
License
Owner
- Name: Peter M. Dahlgren
- Login: peterdalle
- Kind: user
- Location: Sweden
- Website: https://peterdahlgren.com/
- Twitter: peterdalle
- Repositories: 6
- Profile: https://github.com/peterdalle
Work with all things data. I like web, R, stats, open science, C#, .NET, Python, Bash.
Citation (CITATION.cff)
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
- family-names: "Dahlgren"
given-names: "Peter M."
orcid: "https://orcid.org/0000-0002-3509-3329"
title: "Semproducible"
date-released: 2022-03-22
version: 0.9.2
url: "https://github.com/peterdalle/semproducible"
preferred-citation:
type: software
authors:
- family-names: "Dahlgren"
given-names: "Peter M."
orcid: "https://orcid.org/0000-0002-3509-3329"
title: "Semproducible"
year: 2022
url: "https://github.com/peterdalle/semproducible"
GitHub Events
Total
- Watch event: 1
Last Year
- Watch event: 1
Dependencies
- crayon * suggests
- lavaan * suggests
- stats * suggests
- stringi * suggests
- testthat >= 2.1.0 suggests
- tibble * suggests