Skip to content

Commit

Permalink
version 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rempsyc authored and cran-robot committed Jul 2, 2024
1 parent f0f843c commit b60e0fb
Show file tree
Hide file tree
Showing 43 changed files with 2,348 additions and 2,265 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: lavaanExtra
Title: Convenience Functions for Package 'lavaan'
Version: 0.2.0
Date: 2023-10-15
Version: 0.2.1
Date: 2024-07-01
Authors@R:
person("Rémi", "Thériault", , "remi.theriault@mail.mcgill.ca", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-4315-6788"))
Expand All @@ -20,12 +20,12 @@ Suggests: rempsyc (> 0.1.6), flextable, lavaanPlot, DiagrammeRsvg,
markdown, covr, spelling
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.2.3.9000
RoxygenNote: 7.3.1
VignetteBuilder: knitr
Language: en-US
NeedsCompilation: no
Packaged: 2023-10-15 15:22:51 UTC; there
Packaged: 2024-07-01 19:32:47 UTC; coding
Author: Rémi Thériault [aut, cre] (<https://orcid.org/0000-0003-4315-6788>)
Maintainer: Rémi Thériault <remi.theriault@mail.mcgill.ca>
Repository: CRAN
Date/Publication: 2023-10-15 15:50:02 UTC
Date/Publication: 2024-07-01 20:00:02 UTC
84 changes: 42 additions & 42 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
dd2a3d648b909a1774b9099504cec5e5 *DESCRIPTION
2ddd16f2e3758691c135042b3fb6aa11 *DESCRIPTION
2d219d3e4bee721aa04072e81fdc48ef *LICENSE
eca79ff219bcfc0502dd174115c56ac8 *NAMESPACE
75251146de447bf5e41dd9a4fe484fd0 *NEWS.md
22a8e087d42ab1b0efe340349ecf1dd7 *R/cfa_fit_plot.R
cdfdde27e73c540d0b7d2e742b7d48d3 *NEWS.md
e63ba5ba655a75c4bc50e442a7fa395a *R/cfa_fit_plot.R
1b54137d4ae82b40a266122de6d1201b *R/lavaanExtra-package.R
45974162836e7b309b689612c151230f *R/lavaan_cov.R
6d8b230cc986f198bfa89ed770bc4424 *R/lavaan_defined.R
652ff9305378233764be69e5a2ca1909 *R/lavaan_extract.R
e4c7334dfb0fb6199f78f481e704c1c4 *R/lavaan_reg.R
aeedd101535bf0df44310af0424fba85 *R/lavaan_var.R
40ca303ae29779b580c4a905c148ecba *R/nice_fit.R
c71701817df1e62b089de62bf43b821a *R/nice_lavaanPlot.R
93a570cf3e89a835a48997ff337b4bd5 *R/nice_modindices.R
0e0305bc4e599a325724bd4bdf6fb006 *R/nice_tidySEM.R
9385674dde95a33c3ab00693625509fd *R/lavaan_cov.R
a9aacf7bd873c491a3d24ea21c05f0aa *R/lavaan_defined.R
792f0f926ff05e49c055dab6f78078b9 *R/lavaan_extract.R
cb8255aa3ac4feeb181241104173e305 *R/lavaan_reg.R
c80ecf4c2f5fad278160bcf6ab0fecf6 *R/lavaan_var.R
6f1b1d171a1a5d73d12cb53053c968fa *R/nice_fit.R
5b6ea4a382796fe88116e3098f023642 *R/nice_lavaanPlot.R
7566871dd97bb27957b4f731acef2300 *R/nice_modindices.R
a77d8ceda41de273bf8e5bc2796b1c81 *R/nice_tidySEM.R
56bae752b74733bdccbfdd942732cbb0 *R/utils.R
aff2fe70350aa1306f788d3ab029e845 *R/write_lavaan.R
c70430fe5043a8362eae7183f2114bcc *R/write_lavaan.R
7c70d7ae2bcbbf5b44de0cff678f5585 *R/zzz.R
40272b8d1c4a3e1481682ceaa9297c61 *README.md
f01682ffd9d5582c0d3b8a226f3c2b16 *build/partial.rdb
89774310012a250f3415176fbae62ce0 *build/vignette.rds
9bb55996dbb302d4150f35b2970ce84e *README.md
181b5ac5a69bd3e3e542680739e88fd5 *build/partial.rdb
5a2f7c3f2bfdc37886ed3ab1f6675d74 *build/vignette.rds
ae247cd0ba461a8ab2a8c9250281deeb *inst/CITATION
00d236cdaa9a64390938a9b878a20680 *inst/WORDLIST
1f1817916c7eb23b954603ddcb861138 *inst/doc/example.R
772dd79484e17b14e78c20f20985e25c *inst/doc/example.Rmd
8bf56be88cfcda1bbc333310d107b723 *inst/doc/example.html
550a0ddfa3756c184e9f842ea48a884c *inst/doc/write_lavaan.Rmd
cef0dd7a84b04ea6eaf8d55794b7b8ff *inst/doc/example.html
32e9570d7985b91f11c1dfa798ad3e5d *inst/doc/write_lavaan.Rmd
a4255c7c37664e58b23b4c42c0cebc88 *inst/doc/write_lavaan.html
07349a9cb08edee4d0c981dbd668a34b *man/cfa_fit_plot.Rd
711cbe44ec052c25c2efd160c8cb84e4 *man/figures/README-cfa2-1.png
2e3dff5138279fc0f3820753f9341abe *man/figures/README-correlation-1.png
322cc50c4c7b705dc52f24552458a853 *man/figures/README-covariance-1.png
4339003a4d1a2597a641b7e88120c62e *man/figures/README-indirect2-1.png
8402c542b969b681e9808d48b7747ba8 *man/figures/README-path2-1.png
d014dc9ebb1641934486c5ca27e8b2fb *man/figures/README-saturated-1.png
099792a237fa171b7a0f271a8f864d20 *man/figures/README-saturated2-1.png
86f467b83bffb736e8f03dfd3cdc6622 *man/cfa_fit_plot.Rd
6514147e76f6e841d3be63a0975870bb *man/figures/README-cfa2-1.png
8f065536998a7d9c40dcd0a49485a732 *man/figures/README-correlation-1.png
57016a5b306c9366a16885be77a7da37 *man/figures/README-covariance-1.png
1e1a485528ee6e0ba40d35f11cdd014c *man/figures/README-indirect2-1.png
fb1e3f4a774e3fa604d6eedab758602e *man/figures/README-path2-1.png
7b641ad50c77d34a4c3d1c0c97ae037e *man/figures/README-saturated-1.png
23a117fb8baf4d8a6d4089055c65a57a *man/figures/README-saturated2-1.png
903580433fc1a5092211f3fb51a70b9b *man/figures/cfaplot.png
0e32e1cd5d177204eac4e7e81d5b20a0 *man/figures/cfaplot2.png
9e0068138632f96ae7d472dee3c089b5 *man/figures/holzinger_model.png
Expand All @@ -44,30 +44,30 @@ a36cc1bd7a5e21a54edafed77194e036 *man/figures/semplot.png
4c3a56fc50a8bf839ed0282e2e150eb1 *man/figures/semplot2.png
b34fd06703d14d10d7cabeac47268038 *man/lavaanExtra-package.Rd
c25c9a2f6786b8092a23ab7bb78bf9e6 *man/lavaan_cov.Rd
8d1ec590da9caafeafb838431feb3402 *man/lavaan_defined.Rd
73859f8954a6f48efe86e13b4fdb12cc *man/lavaan_extract.Rd
175712e918cc6ae9297ec9f427f92ef6 *man/lavaan_defined.Rd
897f8a24e93c4216d625cb36fd73a2da *man/lavaan_extract.Rd
2147b2a1884d913218cd509598fad778 *man/lavaan_reg.Rd
3da32a6ca69bcd41c04f055f267cfd93 *man/lavaan_var.Rd
68b23ead4fa27822e1058016ae22b8f9 *man/nice_fit.Rd
ea904b54849b742e2aa12cb08862c9e6 *man/nice_lavaanPlot.Rd
53c8e6c992e24324935a1fd4a57da46c *man/lavaan_var.Rd
0a288bda0cfe7baa20bd4d9e6f5ea902 *man/nice_fit.Rd
dc4382da109be7f1db8ffb6f0c456931 *man/nice_lavaanPlot.Rd
9f58f5c0f2a2eaa3f845a447c1e8e053 *man/nice_modindices.Rd
de11d73e2a7515afdf34c72437b0ebe7 *man/nice_tidySEM.Rd
19f586f52882f11adcbd7cd6e76c0f86 *man/write_lavaan.Rd
dbd9bab057163c01c320d0b9d3bb9620 *tests/spelling.R
225550666510dddd51995ec0b9257ce1 *tests/testthat.R
9ea7a6832a27e2f6aeb4d49892de6d2f *tests/testthat/_snaps/lavaan_defined.md
0318d9038bfdeb6c40be900901cba3c6 *tests/testthat/_snaps/nice_modindices.md
1eee6c26e60365c7b6d3820fea9029da *tests/testthat/_snaps/write_lavaan.md
a7425641d7d6ce0bbc1bc7054b9d2b1a *tests/testthat/test-cfa_fit_plot.R
f692c68fef9dacee1f379af6536d2964 *tests/testthat/test-lavaan_cor.R
2f8698fd45033e09d092d779edfdcd4b *tests/testthat/test-lavaan_cov.R
962c47ed86934a2abce0ce0ac5b2b9fb *tests/testthat/_snaps/lavaan_defined.md
d6d78d11a9b3e5c02ad1e89f1be576d0 *tests/testthat/_snaps/nice_modindices.md
494727bcd35b062b8b3e3892bf009174 *tests/testthat/_snaps/write_lavaan.md
5c7f4d90e7a105e6bc7659f32429984e *tests/testthat/test-cfa_fit_plot.R
65d399baedf09857d829c206fb29bdd8 *tests/testthat/test-lavaan_cor.R
33ca40adc4745b9cf073fdd2475ed993 *tests/testthat/test-lavaan_cov.R
58144899645cd20ea980e6c47ba93f84 *tests/testthat/test-lavaan_defined.R
dfe00ca11ff7fca8d3ad239a4e57517f *tests/testthat/test-lavaan_reg.R
ad6b69834c4cd89355f9806a150c96e6 *tests/testthat/test-lavaan_var.R
1bdc1be7a4f8ac4aca854d04ec9a6148 *tests/testthat/test-nice_fit.R
4d475325f19f57d75316dd915d5a292f *tests/testthat/test-nice_lavaanPlot.R
84d8ea91b0f176c134be156a7d41209a *tests/testthat/test-lavaan_reg.R
846b0b08a6c1dcd96bdae68632d9a591 *tests/testthat/test-lavaan_var.R
7989349bfce751eec3c24c2203a1396e *tests/testthat/test-nice_fit.R
715950554a07cc82f1d177d42ab4063e *tests/testthat/test-nice_lavaanPlot.R
3b2bf6bd036a8c3dc59c35dc778e987a *tests/testthat/test-nice_modindices.R
78905aecfa55eb3116d9d31f6331da43 *tests/testthat/test-nice_tidySEM.R
c9c2bba6c478e8ae516a64b13b53bcea *tests/testthat/test-write_lavaan.R
772dd79484e17b14e78c20f20985e25c *vignettes/example.Rmd
550a0ddfa3756c184e9f842ea48a884c *vignettes/write_lavaan.Rmd
32e9570d7985b91f11c1dfa798ad3e5d *vignettes/write_lavaan.Rmd
18 changes: 18 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# lavaanExtra 0.2.2
* Incoming ✨

# lavaanExtra 0.2.1.1
* Incoming ✨

# lavaanExtra 0.2.1
* New CRAN submission

## lavaanExtra 0.2.0.3
* Fix bug in `nice_fit()` that created an error with categorical variables (#37)

## lavaanExtra 0.2.0.2
* Fix bug in `lavaan_defined()` / `lavaan_extract()` that repeated the first cell on all other rows of the first column (#36)

## lavaanExtra 0.2.0.1
* Now skips tests and examples when `DiagrammeRsvg` is not installed

# lavaanExtra 0.2.0
* CRAN resubmission to fix unicode problem

Expand Down
176 changes: 88 additions & 88 deletions R/cfa_fit_plot.R
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
#' @title Fit and plot CFA simultaneously
#'
#' @description Prints and saves CFA fit, as well as plots CFA factor loadings,
#' simultaneously.
#'
#' @param model CFA model to fit.
#' @param data Data set on which to fit the CFA model.
#' @param covs Logical, whether to include covariances on the lavaan plot.
#' @param estimator What estimator to use for the CFA.
#' @param ... Arguments to be passed to function [lavaan::cfa].
#' @param remove.items Optional, if one wants to remove items from the CFA model
#' without having to redefine it completely again.
#' @param print Logical, whether to print model summary to console.
#' @param save.as.pdf Logical, whether to save as PDF for a high-resolution,
#' scalable vector graphic quality plot. Defaults to
#' saving to the "/model" subfolder of the working
#' directory. If it doesn't exist, it creates it. Then
#' automatically open the created PDF in the default
#' browser. Defaults to false.
#' @param file.name Optional (when `save.as.pdf` is set to `TRUE`), if one
#' wants something different than the default file name.
#' It saves to pdf per default, so the .pdf extension
#' should not be specified as it will add it automatically.
#' @return The function returns a `lavaan` fit object. However, it also: prints
#' a summary of the `lavaan` fit object to the console, and; prints a
#' `lavaanPlot` of the `lavaan` fit object.
#' @export
#' @examplesIf requireNamespace("lavaan", quietly = TRUE) && requireNamespace("lavaanPlot", quietly = TRUE)
#' x <- paste0("x", 1:9)
#' (latent <- list(
#' visual = x[1:3],
#' textual = x[4:6],
#' speed = x[7:9]
#' ))
#'
#' HS.model <- write_lavaan(latent = latent)
#' cat(HS.model)
#'
#' library(lavaan)
#' fit <- cfa_fit_plot(HS.model, HolzingerSwineford1939)
#' @import lavaan
#' @section Illustrations:
#'
#' \if{html}{
#' \figure{cfaplot.png}{options: width="400"}
#' }

cfa_fit_plot <- function(
model, data, covs = FALSE, estimator = "MLR", remove.items = "",
print = TRUE, save.as.pdf = FALSE, file.name, ...) {
insight::check_if_installed("lavaanPlot")

if (missing(file.name) && isTRUE(save.as.pdf)) {
stop("To save as PDF, the file name must also be specified.")
}

# Remove requested items
if (!missing(remove.items)) {
remove.items0 <- paste0("\\s", remove.items, "\\s")
remove.items1 <- paste0(remove.items, collapse = " \\+ |")
remove.items1 <- paste0(remove.items1, " \\+")
remove.items2 <- paste0("\\+ ", remove.items0)
remove.items2 <- paste0(remove.items2, collapse = "|")
remove.items3 <- paste0(remove.items, " ", collapse = "|\\+ ")
model <- gsub("\n", " \n", model)
model <- gsub(remove.items1, "", model)
model <- gsub(remove.items2, "", model)
model <- gsub(remove.items3, "", model)
}
# Fit model
fit <- lavaan::cfa(model, data = data, estimator = estimator, ...)
if (isTRUE(print)) {
print(summary(fit,
standardized = TRUE,
fit.measures = TRUE, rsquare = TRUE
))
}
# Plot the model
my.plot <- nice_lavaanPlot(fit, covs = covs)
# # Save file
if (!missing(file.name) && isTRUE(save.as.pdf)) {
lavaanPlot::embed_plot_pdf(my.plot, paste0(file.name, ".pdf"))
utils::browseURL(paste0(file.name, ".pdf"))
} else {
print(my.plot)
}
fit
}
#' @title Fit and plot CFA simultaneously
#'
#' @description Prints and saves CFA fit, as well as plots CFA factor loadings,
#' simultaneously.
#'
#' @param model CFA model to fit.
#' @param data Data set on which to fit the CFA model.
#' @param covs Logical, whether to include covariances on the lavaan plot.
#' @param estimator What estimator to use for the CFA.
#' @param ... Arguments to be passed to function [lavaan::cfa].
#' @param remove.items Optional, if one wants to remove items from the CFA model
#' without having to redefine it completely again.
#' @param print Logical, whether to print model summary to console.
#' @param save.as.pdf Logical, whether to save as PDF for a high-resolution,
#' scalable vector graphic quality plot. Defaults to
#' saving to the "/model" subfolder of the working
#' directory. If it doesn't exist, it creates it. Then
#' automatically open the created PDF in the default
#' browser. Defaults to false.
#' @param file.name Optional (when `save.as.pdf` is set to `TRUE`), if one
#' wants something different than the default file name.
#' It saves to pdf per default, so the .pdf extension
#' should not be specified as it will add it automatically.
#' @return The function returns a `lavaan` fit object. However, it also: prints
#' a summary of the `lavaan` fit object to the console, and; prints a
#' `lavaanPlot` of the `lavaan` fit object.
#' @export
#' @examplesIf requireNamespace("lavaan", quietly = TRUE) && requireNamespace("lavaanPlot", quietly = TRUE) && requireNamespace("DiagrammeRsvg", quietly = TRUE)
#' x <- paste0("x", 1:9)
#' (latent <- list(
#' visual = x[1:3],
#' textual = x[4:6],
#' speed = x[7:9]
#' ))
#'
#' HS.model <- write_lavaan(latent = latent)
#' cat(HS.model)
#'
#' library(lavaan)
#' fit <- cfa_fit_plot(HS.model, HolzingerSwineford1939)
#' @import lavaan
#' @section Illustrations:
#'
#' \if{html}{
#' \figure{cfaplot.png}{options: width="400"}
#' }

cfa_fit_plot <- function(
model, data, covs = FALSE, estimator = "MLR", remove.items = "",
print = TRUE, save.as.pdf = FALSE, file.name, ...) {
insight::check_if_installed("lavaanPlot")

if (missing(file.name) && isTRUE(save.as.pdf)) {
stop("To save as PDF, the file name must also be specified.")
}

# Remove requested items
if (!missing(remove.items)) {
remove.items0 <- paste0("\\s", remove.items, "\\s")
remove.items1 <- paste0(remove.items, collapse = " \\+ |")
remove.items1 <- paste0(remove.items1, " \\+")
remove.items2 <- paste0("\\+ ", remove.items0)
remove.items2 <- paste0(remove.items2, collapse = "|")
remove.items3 <- paste0(remove.items, " ", collapse = "|\\+ ")
model <- gsub("\n", " \n", model)
model <- gsub(remove.items1, "", model)
model <- gsub(remove.items2, "", model)
model <- gsub(remove.items3, "", model)
}
# Fit model
fit <- lavaan::cfa(model, data = data, estimator = estimator, ...)
if (isTRUE(print)) {
print(summary(fit,
standardized = TRUE,
fit.measures = TRUE, rsquare = TRUE
))
}
# Plot the model
my.plot <- nice_lavaanPlot(fit, covs = covs)
# # Save file
if (!missing(file.name) && isTRUE(save.as.pdf)) {
lavaanPlot::embed_plot_pdf(my.plot, paste0(file.name, ".pdf"))
utils::browseURL(paste0(file.name, ".pdf"))
} else {
print(my.plot)
}
fit
}
Loading

0 comments on commit b60e0fb

Please sign in to comment.