Skip to content

Commit

Permalink
use lavaan_extract() internally for lavaan_cov())
Browse files Browse the repository at this point in the history
  • Loading branch information
rempsyc committed Oct 9, 2023
1 parent df03a63 commit 8052819
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* `write_lavaan()`: accepts a new argument, `threshold`, represented by the "|" operator.
* `lavaan_ind()` renames to `lavaan_defined()` because it was not specific to indirect effects but actually extracts any user-defined parameters such as total effects and moderated slopes using the `:=` operator.
* `lavaan_reg()` gets rid of the estimate argument, to comply with best practices of reporting both unstandardized and standardized parameters with their respective confidence intervals.
* new function: `lavaan_extract()`, which takes a specific operator and extracts relevant information (now used internally for `lavaan_reg()`, `lavaan_defined()`)
* new function: `lavaan_extract()`, which takes a specific operator and extracts relevant information (now used internally for `lavaan_reg()`, `lavaan_defined()`, `lavaan_cov()`)

# lavaanExtra 0.1.8
* CRAN resubmission
Expand Down
36 changes: 8 additions & 28 deletions R/lavaan_cov.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,17 @@
#' library(lavaan)
#' fit <- sem(HS.model, data = HolzingerSwineford1939)
#' lavaan_cov(fit)
#' lavaan_cov(fit, estimate = "r")
#' lavaan_cor(fit) # same as previous
lavaan_cov <- function(fit, estimate = "sigma", nice_table = FALSE, ...) {
og.names <- c("lhs", "rhs", "pvalue", "est", "ci.lower", "ci.upper")
new.names <- c("Variable 1", "Variable 2", "p", "sigma", "CI_lower", "CI_upper")
if (estimate == "sigma") {
x <- lavaan::parameterEstimates(fit)
} else if (estimate == "r") {
x <- lavaan::standardizedsolution(fit, level = 0.95)
og.names[4] <- "est.std"
new.names[4] <- "r"
} else {
stop("The 'estimate' argument may only be one of c('sigma', 'r').")
}
x <- x[which(x["op"] == "~~"), ]
diag <- which(x$lhs == x$rhs)
x <- x[-diag, ] # keep only off-diagonal elements
x <- x[og.names]
names(x) <- new.names
if (nice_table) {
insight::check_if_installed("rempsyc",
version = get_dep_version("rempsyc"),
reason = "for this feature."
)
x <- rempsyc::nice_table(x, ...)
}
x
lavaan_cov <- function(fit, nice_table = FALSE, ...) {
lavaan_extract(fit,
operator = "~~",
lhs_name = "Variable 1",
rhs_name = "Variable 2",
diag = "exclude",
nice_table = nice_table)
}

#' @export
#' @describeIn lavaan_cov Shortcut for `lavaan_cov(fit, estimate = "r")`
lavaan_cor <- function(fit, nice_table = FALSE, ...) {
lavaan_cov(fit, estimate = "r", nice_table = nice_table, ...)
lavaan_cov(fit, nice_table = nice_table, ...)
}
11 changes: 10 additions & 1 deletion R/lavaan_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ lavaan_extract <- function(fit,
lhs_name = "Left-Hand Side",
rhs_name = "Right-Hand Side",
underscores_to_symbol = "\u2192",
diag = NULL,
nice_table = FALSE,
...) {
if (missing(operator)) {
Expand All @@ -68,10 +69,18 @@ lavaan_extract <- function(fit,

x <- lavaan::parameterEstimates(fit)
x <- x[which(x["op"] == operator), ]
x <- x[og.names]

es <- lavaan::standardizedsolution(fit, level = 0.95)
es <- es[which(es["op"] == operator), ]

if (!is.null(diag) && diag == "exclude") {
diag <- which(x$lhs == x$rhs)
x <- x[-diag, ] # keep only off-diagonal elements
es <- es[-diag, ] # keep only off-diagonal elements
new.names[c(6, 9:11)] <- c("sigma", "r", "CI_lower_r", "CI_upper_r")
}

x <- x[og.names]
es <- es[c("est.std", og.names[7:8])]
names(es)[2:3] <- paste0(names(es)[2:3], ".std")

Expand Down
14 changes: 0 additions & 14 deletions tests/testthat/test-lavaan_cov.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,3 @@ test_that("nice_fit as nice_table", {
c("nice_table", "flextable")
)
})

test_that("nice_fit estimates", {
expect_s3_class(
lavaan_cov(fit, estimate = "sigma"),
c("lavaan.data.frame", "data.frame")
)
expect_s3_class(
lavaan_cov(fit, estimate = "r"),
c("lavaan.data.frame", "data.frame")
)
expect_error(
lavaan_cov(fit, estimate = "B"),
)
})

0 comments on commit 8052819

Please sign in to comment.