Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jan 8, 2024
1 parent 2427ef9 commit dda2894
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
15 changes: 6 additions & 9 deletions R/mcdonalds_omega.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#'
#' @references McDonald, R.P. (1999). Test theory: A unified treatment. Hillsdale: Erlbaum.
#'
#' @examples
#' @examplesIf requireNamespace("lavaan", quietly = TRUE)
#' data(iris)
#' x <- iris[1:4]
#' mcdonalds_omega(x)
Expand All @@ -42,21 +42,18 @@ mcdonalds_omega <- function(x, ...) {

#' @export
mcdonalds_omega.data.frame <- function(x, ci = 0.95, verbose = TRUE, ...) {
# remove missings
.data <- stats::na.omit(x)

# we need at least two columns for Cronach's Alpha
if (is.null(ncol(.data)) || ncol(.data) < 2) {
if (is.null(ncol(x)) || ncol(x) < 2) {
if (verbose) {
insight::format_warning("Too few columns in `x` to compute McDonald's Omega.")
}
return(NULL)
}

# prepare names and formulas for lavaan
varnames <- colnames(.data)
name_loadings <- paste0("a", seq_len(ncol(.data)))
name_error <- paste0("b", seq_len(ncol(.data)))
varnames <- colnames(x)
name_loadings <- paste0("a", seq_len(ncol(x)))
name_error <- paste0("b", seq_len(ncol(x)))

# we need this specific formulation for lavaan to get the omega reliability estimate
# see code in MBESS
Expand All @@ -83,7 +80,7 @@ mcdonalds_omega.data.frame <- function(x, ci = 0.95, verbose = TRUE, ...) {
insight::check_if_installed("lavaan")

# fit CFA to get reliability estimate
fit <- .safe(suppressWarnings(lavaan::cfa(model, data = .data, missing = "ml", estimator = "mlr", se = "default")))
fit <- .safe(suppressWarnings(lavaan::cfa(model, data = x, missing = "ml", estimator = "mlr", se = "default")))
if (is.null(fit)) {
if (verbose) {
insight::format_warning("Could not compute McDonald's Omega.")
Expand Down
2 changes: 2 additions & 0 deletions man/mcdonalds_omega.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions tests/testthat/test-mcdonalds_omega.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("mcdonalds_omega, data frame", {
data(mtcars)
x <- mtcars[, c("cyl", "gear", "carb", "hp")]
expect_warning(mcdonalds_omega(x), regex = "is not in range [0, 1]")
expect_warning(mcdonalds_omega(x), regex = "is not in range")
expect_warning(mcdonalds_omega(x, ci = NULL), regex = "is greater than 1")
expect_equal(mcdonalds_omega(x, verbose = FALSE), 1.156718, tolerance = 1e-3)

Expand Down Expand Up @@ -35,10 +35,13 @@ test_that("mcdonalds_omega, data frame", {
)
})


test_that("mcdonalds_omega", {
expect_warning(expect_null(mcdonalds_omega(mtcars[1])), regex = "Too few columns")
})

# save time on CRAN
skip_on_cran()

test_that("mcdonalds_omega, principal_components", {
skip_if_not_installed("parameters", minimum_version = "0.21.3")
Expand All @@ -62,7 +65,7 @@ test_that("mcdonalds_omega, principal_components", {
test_that("mcdonalds_omega, matrix", {
m <- as.matrix(iris[1:4])
expect_equal(
mcdonalds_omega(x),
mcdonalds_omega(m),
data.frame(
Omega = 0.984746012592052,
CI_low = 0.969115091775479,
Expand Down

0 comments on commit dda2894

Please sign in to comment.