Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jan 8, 2024
1 parent 8f11ab2 commit 2163bf9
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions R/mcdonalds_omega.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,55 @@ mcdonalds_omega <- function(x, ...) {


#' @export
mcdonalds_omega.data.frame <- function(x, 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 (verbose) {
insight::format_warning("Too few columns in `x` to compute McDonald's Omega.")
}
return(NULL)
}
mcdonalds_omega.data.frame <- function(x, ci = 0.95, verbose = TRUE, ...) {
varnames <- colnames(x)
q <- length(varnames)

Check warning on line 37 in R/mcdonalds_omega.r

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/mcdonalds_omega.r,line=37,col=3,[object_overwrite_linter] 'q' is an exported object from package 'base'. Avoid re-using such symbols.
N <- nrow(x)
loadingName <- paste0("a", 1:q)
errorName <- paste0("b", 1:q)

Check warning on line 40 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L36-L40

Added lines #L36 - L40 were not covered by tests

model <- paste0("f1 =~ NA*", varnames[1], " + ")
loadingLine <- paste(paste0(loadingName, "*", varnames), collapse = " + ")
factorLine <- "f1 ~~ 1*f1\n"
errorLine <- paste(paste0(varnames, " ~~ ", errorName, "*", varnames), collapse = "\n")
sumLoading <- paste("loading :=", paste(loadingName, collapse = " + "), "\n")
sumError <- paste("error :=", paste(errorName, collapse = " + "), "\n")
relia <- "relia := (loading^2) / ((loading^2) + error) \n"
model <- paste0(
model,
loadingLine,
"\n",
factorLine,
errorLine,
"\n",
sumLoading,
sumError,
relia

Check warning on line 58 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L42-L58

Added lines #L42 - L58 were not covered by tests
)

fit <- lavaan::cfa(model,
data = attitude[, -1], missing = "ml", estimator = "mlr", se = "default"

Check warning on line 62 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L61-L62

Added lines #L61 - L62 were not covered by tests
)

lavaan::parameterEstimates(fit)

Check warning on line 65 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L65

Added line #L65 was not covered by tests

# Compute Cronbach's Alpha
dim(.data)[2] / (dim(.data)[2] - 1) * (1 - sum(apply(.data, 2, stats::var)) / stats::var(rowSums(.data)))
est <- 0.8274243
se <- 0.05258224

Check warning on line 68 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L67-L68

Added lines #L67 - L68 were not covered by tests

crit <- stats::qnorm((1 + ci) / 2)

Check warning on line 70 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L70

Added line #L70 was not covered by tests

logest <- log(est / (1 - est))
logse <- se / (est * (1 - est))
loglower <- logest - crit * logse
logupper <- logest + crit * logse
if (logupper < loglower) {
temp <- loglower
loglower <- logupper
loguppper <- temp

Check warning on line 79 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L72-L79

Added lines #L72 - L79 were not covered by tests
}
lower <- 1 / (1 + exp(-loglower))
upper <- 1 / (1 + exp(-logupper))
c(lower, upper)

Check warning on line 83 in R/mcdonalds_omega.r

View check run for this annotation

Codecov / codecov/patch

R/mcdonalds_omega.r#L81-L83

Added lines #L81 - L83 were not covered by tests
}


Expand Down

0 comments on commit 2163bf9

Please sign in to comment.