{"id":16272602,"url":"https://github.com/rempsyc/lavaanextra","last_synced_at":"2025-03-19T23:31:13.015Z","repository":{"id":57728543,"uuid":"526792129","full_name":"rempsyc/lavaanExtra","owner":"rempsyc","description":"Convenience Functions for Package `lavaan`","archived":false,"fork":false,"pushed_at":"2024-07-01T19:41:04.000Z","size":21732,"stargazers_count":18,"open_issues_count":4,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-17T12:21:53.592Z","etag":null,"topics":["convenience-functions","lavaan","psychology","r","rstats","statistics","structural-equation-modeling"],"latest_commit_sha":null,"homepage":"https://lavaanExtra.remi-theriault.com/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rempsyc.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"rempsyc"}},"created_at":"2022-08-20T01:04:35.000Z","updated_at":"2024-08-25T10:58:59.000Z","dependencies_parsed_at":"2024-06-09T20:26:21.396Z","dependency_job_id":"7aea82b0-24fd-459a-9d9f-8b0a6a151d32","html_url":"https://github.com/rempsyc/lavaanExtra","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rempsyc%2FlavaanExtra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rempsyc%2FlavaanExtra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rempsyc%2FlavaanExtra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rempsyc%2FlavaanExtra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rempsyc","download_url":"https://codeload.github.com/rempsyc/lavaanExtra/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244524872,"owners_count":20466518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["convenience-functions","lavaan","psychology","r","rstats","statistics","structural-equation-modeling"],"created_at":"2024-10-10T18:18:26.073Z","updated_at":"2025-03-19T23:31:10.715Z","avatar_url":"https://github.com/rempsyc.png","language":"R","funding_links":["https://github.com/sponsors/rempsyc"],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# lavaanExtra: Convenience functions for `lavaan`\n\n```{r version, echo = FALSE, message = FALSE, warning = FALSE}\nflextable::set_flextable_defaults(background.color = \"white\")\n\nlibrary(lavaan)\nlibrary(lavaanExtra)\n```\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/rempsyc/lavaanExtra/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/rempsyc/lavaanExtra/actions/workflows/R-CMD-check.yaml)\n[![CRAN status](https://www.r-pkg.org/badges/version/lavaanExtra)](https://cran.r-project.org/package=lavaanExtra)\n[![r-universe](https://rempsyc.r-universe.dev/badges/lavaanExtra)](https://rempsyc.r-universe.dev/lavaanExtra)\n[![Last-commit](https://img.shields.io/github/last-commit/rempsyc/lavaanExtra)](https://github.com/rempsyc/lavaanExtra/commits/main)\n[![Codecov test coverage](https://codecov.io/gh/rempsyc/lavaanExtra/branch/main/graph/badge.svg)](https://app.codecov.io/gh/rempsyc/lavaanExtra?branch=main)\n[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)\n[![downloads](https://cranlogs.r-pkg.org/badges/lavaanExtra)](https://shinyus.ipub.com/cranview/)\n[![total](https://cranlogs.r-pkg.org/badges/grand-total/lavaanExtra)](https://shinyus.ipub.com/cranview/)\n[![DOI](https://joss.theoj.org/papers/10.21105/joss.05701/status.svg)](https://doi.org/10.21105/joss.05701)\n[![sponsors](https://img.shields.io/github/sponsors/rempsyc)](https://github.com/sponsors/rempsyc)\n[![followers](https://img.shields.io/github/followers/rempsyc?style=social)](https://github.com/rempsyc?tab=followers)\n[![stars](https://img.shields.io/github/stars/rempsyc/lavaanExtra?style=social)](https://github.com/rempsyc/lavaanExtra/stargazers)\n\u003c!-- badges: end --\u003e\n\nAffords an alternative, vector-based syntax to `lavaan`, as well as other convenience functions such as naming paths and defining indirect links automatically. Also offers convenience formatting optimized for a publication and script sharing workflow.\n\n## Installation\n\nYou can install the `lavaanExtra` package directly from CRAN:\n``` r\ninstall.packages(\"lavaanExtra\")\n```\n\nOr the development version from the r-universe (note that there is a 24-hour delay with GitHub):\n``` r\ninstall.packages(\"lavaanExtra\", repos = c(\n  rempsyc = \"https://rempsyc.r-universe.dev\",\n  CRAN = \"https://cloud.r-project.org\"))\n```\n\nOr from GitHub, for the very latest version:\n``` r\n# If not already installed, install package `remotes` with `install.packages(\"remotes\")`\nremotes::install_github(\"rempsyc/lavaanExtra\")\n```\n\nTo see all the available functions, use:\n``` r\nhelp(package = \"lavaanExtra\")\n```\n\n**Dependencies:** Because `lavaanExtra` is a package of convenience functions relying on several external packages, it uses (inspired by the [`easystats`](https://easystats.github.io/easystats/) packages) a minimalist philosophy of only installing packages that you need when you need them through `rlang::check_installed()`. Should you wish to specifically install all suggested dependencies at once (you can view the full list by clicking on the CRAN badge on this page), you can run the following (be warned that this may take a long time, as some of the suggested packages are only used in the vignettes or examples):\n``` r\ninstall.packages(\"lavaanExtra\", dependencies = TRUE)\n```\n\n## Why use `lavaanExtra`?\n\n1. **Reusable code**. Don't repeat yourself anymore when you only want to change a few things when comparing and fitting models.\n2. **Shorter code**. Because of point 1, you can have shorter code, since you write it once and simply reuse it. For items with similar patterns, you can also use `paste0()` with appropriate item numbers instead of typing each one every time.\n3. **Less error-prone code**. Because of point 1, you can have less risk of human errors since you don't have possibly multiple different version of the same thing (which makes it easier to correct too).\n4. **Better control over your code**. Because of point 1, you are in control of the whole flow. You change it once, and it will change it everywhere else in the script, without having to change it manually for each model.\n5. **More readable code**. Because of point 1, other people (but also yourself) only have to process the information the first time to make sure it's been specified correctly, and not every time you check the new models.\n6. **Prettier code**. Because it will format the model for you in a pretty way, every time. You don't have to worry about manually making your model good-looking and readable anymore.\n7. **More accessible code**. You don't have to remember the exact syntax (although it is recommended) for it to work. It uses intuitive variable names that most people can understand. This benefit is most apparent for beginners, but it also saves precious typing time for veterans.\n\n## Overview\n\n```{r headers, results = \"asis\", echo = FALSE}\nsection.1 \u003c- \"CFA example\"\nsection.2 \u003c- \"SEM example\"\nsection.3 \u003c- \"Final note\"\n\ncute_cat \u003c- function(x, header.level = 1) {\n  cat(rep(\"#\", header.level), \" \", x, sep = \"\")\n}\n\ncute_TOC \u003c- function(section) {\n  cat(\"[\", section, \"]\",\n    \"(#\", tolower(gsub(\" \", \"-\", gsub(\",\", \"\", section))), \")\",\n    \"\u003ca name = '\", section, \"'/\u003e\",\n    \"\\n \\n\",\n    sep = \"\"\n  )\n}\n\ninvisible(lapply(\n  list(\n    section.1, section.2, section.3\n  ),\n  cute_TOC\n))\n```\n\n## CFA example\n\n```{r cfaplot}\n# Load libraries\nlibrary(lavaan)\nlibrary(lavaanExtra)\n\n# Define latent variables\nlatent \u003c- list(\n  visual = c(\"x1\", \"x2\", \"x3\"),\n  textual = c(\"x4\", \"x5\", \"x6\"),\n  speed = c(\"x7\", \"x8\", \"x9\")\n)\n\n# If you have many items, you can also use the `paste0` function:\nx \u003c- paste0(\"x\", 1:9)\nlatent \u003c- list(\n  visual = x[1:3],\n  textual = x[4:6],\n  speed = x[7:9]\n)\n\n# Write the model, and check it\ncfa.model \u003c- write_lavaan(latent = latent)\ncat(cfa.model)\n\n# Fit the model fit and plot with `lavaanExtra::cfa_fit_plot`\n# to get the factor loadings visually (optionally as PDF)\nfit.cfa \u003c- cfa_fit_plot(cfa.model, HolzingerSwineford1939)\n```\n\n\u003cimg src=\"man/figures/cfaplot.png\" width=\"30%\" /\u003e\n\n```{r cfa2, results='hide'}\n# Get nice fit indices with the `rempsyc::nice_table` integration\nnice_fit(fit.cfa, nice_table = TRUE)\n```\n\n```{r cfa2.save, include=FALSE}\ntable_temp \u003c- nice_fit(fit.cfa, nice_table = TRUE) |\u003e\n  flextable::autofit()\nflextable::save_as_image(table_temp,\n  path = \"man/figures/README-cfa2-1.png\",\n  expand = 0, res = 300\n)\n```\n\n\u003cimg src=\"man/figures/README-cfa2-1.png\" width=\"100%\" /\u003e\n\n## SEM example\n\nNote that latent variables have been defined above, so we can reuse them as is, without having to redefine them. \n\n```{r saturated}\n# Define our other variables\nM \u003c- \"visual\"\nIV \u003c- c(\"ageyr\", \"grade\")\nDV \u003c- c(\"speed\", \"textual\")\n\n# Define our lavaan lists\nmediation \u003c- list(speed = M, textual = M, visual = IV)\nregression \u003c- list(speed = IV, textual = IV)\ncovariance \u003c- list(speed = \"textual\", ageyr = \"grade\")\n\n# Define indirect effects object\nindirect \u003c- list(IV = IV, M = M, DV = DV)\n\n# Write the model, and check it\nmodel \u003c- write_lavaan(\n  mediation = mediation,\n  regression = regression,\n  covariance = covariance,\n  indirect = indirect,\n  latent = latent,\n  label = TRUE\n)\ncat(model)\n\nfit.sem \u003c- sem(model, data = HolzingerSwineford1939)\n```\n\n```{r saturated2, results='hide'}\n# Get regression parameters and make pretty with `rempsyc::nice_table`\nlavaan_reg(fit.sem, nice_table = TRUE, highlight = TRUE)\n```\n\n```{r saturated2.save, include=FALSE}\ntable_temp \u003c- lavaan_reg(fit.sem, nice_table = TRUE, highlight = TRUE) |\u003e\n  flextable::autofit()\nflextable::save_as_image(table_temp,\n  path = \"man/figures/README-saturated-1.png\",\n  expand = 0, res = 300\n)\n```\n\n\u003cimg src=\"man/figures/README-saturated-1.png\" width=\"100%\" /\u003e\n\n```{r correlation, results='hide'}\n# Get covariances/correlations and make them pretty with \n# the `rempsyc::nice_table` integration\nlavaan_cor(fit.sem, nice_table = TRUE)\n```\n\n```{r covariance.save, include=FALSE}\ntable_temp \u003c- lavaan_cor(fit.sem, nice_table = TRUE) |\u003e\n  flextable::autofit()\nflextable::save_as_image(table_temp,\n  path = \"man/figures/README-covariance-1.png\",\n  expand = 0, res = 300\n)\n```\n\n\u003cimg src=\"man/figures/README-covariance-1.png\" width=\"100%\" /\u003e\n\n```{r path2, results='hide'}\n# Get nice fit indices with the `rempsyc::nice_table` integration\nfit_table \u003c- nice_fit(list(fit.cfa, fit.sem), nice_table = TRUE)\nfit_table\n```\n\n```{r path2.save, include=FALSE}\ntable_temp \u003c- nice_fit(list(fit.cfa, fit.sem), nice_table = TRUE) |\u003e\n  flextable::autofit()\nflextable::save_as_image(table_temp,\n  path = \"man/figures/README-path2-1.png\",\n  expand = 0, res = 300\n)\n```\n\n\u003cimg src=\"man/figures/README-path2-1.png\" width=\"100%\" /\u003e\n\n```{r indirect2, results='hide'}\n# Save fit table to Word!\nflextable::save_as_docx(fit_table, path = \"fit_table.docx\")\n# Note that it will also render to PDF in an `rmarkdown` document\n# with `output: pdf_document`, but using `latex_engine: xelatex`\n# is necessary when including Unicode symbols in tables like with\n# the `nice_fit()` function.\n\n# Let's get the user-defined (e.g., indirect) effects only and make it pretty\n# with the `rempsyc::nice_table` integration\nlavaan_defined(fit.sem, nice_table = TRUE)\n```\n\n```{r indirect2.save, include=FALSE}\ntable_temp \u003c- lavaan_defined(fit.sem, nice_table = TRUE) |\u003e\n  flextable::autofit()\nflextable::save_as_image(table_temp,\n  path = \"man/figures/README-indirect2-1.png\",\n  expand = 0, res = 300\n)\n```\n\n\u003cimg src=\"man/figures/README-indirect2-1.png\" width=\"100%\" /\u003e\n\n```{r, echo=FALSE}\nunlink(\"fit_table.docx\")\n```\n\n```{r plot, eval = FALSE}\n# Plot our model\nnice_lavaanPlot(fit.sem)\n```\n\n\u003cimg src=\"man/figures/semplot.png\" width=\"70%\" /\u003e\n\n```{r plot2}\n# Alternative way to plot\nmylayout \u003c- data.frame(\n  IV = c(\"\", \"x1\", \"grade\", \"\", \"ageyr\", \"\", \"\"),\n  M = c(\"\", \"x2\", \"\", \"visual\", \"\", \"\", \"\"),\n  DV = c(\"\", \"x3\", \"textual\", \"\", \"speed\", \"\", \"\"),\n  DV.items = c(paste0(\"x\", 4:6), \"\", paste0(\"x\", 7:9))\n) |\u003e\n  as.matrix()\nmylayout\n```\n\n```{r plot3, eval = FALSE}\nnice_tidySEM(fit.sem, layout = mylayout, label_location = 0.7)\n\nggplot2::ggsave(\"my_semPlot.pdf\", width = 6, height = 6, limitsize = FALSE)\n```\n\n\u003cimg src=\"man/figures/semplot2.png\" width=\"70%\" /\u003e\n\n## Final note\nThis is an experimental package in a *very* early stage. Any feedback or feature request is appreciated, and the package will likely change and evolve over time based on community feedback. Feel free to open an issue or discussion to share your questions or concerns. And of course, please have a look at the other tutorials to discover even more cool features: https://lavaanExtra.remi-theriault.com/articles/\n\n## Support me and this package\n\nThank you for your support. You can support me and this package here: https://github.com/sponsors/rempsyc\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frempsyc%2Flavaanextra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frempsyc%2Flavaanextra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frempsyc%2Flavaanextra/lists"}