Skip to content

Commit

Permalink
test lavaan fit interp
Browse files Browse the repository at this point in the history
  • Loading branch information
mattansb committed Jan 6, 2022
1 parent 7de0cc0 commit dc8666d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 133 deletions.
147 changes: 15 additions & 132 deletions R/interpret_cfa_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@
#' dem60 =~ y1 + y2 + y3
#' dem60 ~ ind60 "
#' model <- lavaan::sem(structure, data = PoliticalDemocracy)
#' # interpret(model) # Not working until new performance is up
#' interpret(model)
#' }
#'
#' @references
#' - Awang, Z. (2012). A handbook on SEM. Structural equation modeling.
#'
Expand Down Expand Up @@ -246,137 +247,19 @@ interpret.lavaan <- function(x, ...) {
#' @rdname interpret_gfi
#' @export
interpret.performance_lavaan <- function(x, ...) {
table <- data.frame(Name = c(), Value = c(), Interpretation = c(), Threshold = c())

# GFI
if ("GFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "GFI",
Value = x$GFI,
Interpretation = interpret_gfi(x$GFI),
Threshold = 0.95
)
)
}

# AGFI
if ("AGFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "AGFI",
Value = x$AGFI,
Interpretation = interpret_agfi(x$AGFI),
Threshold = 0.90
)
)
}

# NFI
if ("NFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "NFI",
Value = x$NFI,
Interpretation = interpret_nfi(x$NFI, rules = "byrne1994"),
Threshold = 0.90
)
)
}

# NNFI
if ("NNFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "NNFI",
Value = x$NNFI,
Interpretation = interpret_nnfi(x$NNFI, rules = "byrne1994"),
Threshold = 0.90
)
)
}

# CFI
if ("CFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "CFI",
Value = x$CFI,
Interpretation = interpret_cfi(x$CFI),
Threshold = 0.90
)
)
}

# RMSEA
if ("RMSEA" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "RMSEA",
Value = x$RMSEA,
Interpretation = interpret_rmsea(x$RMSEA),
Threshold = 0.05
)
)
}

# SRMR
if ("SRMR" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "SRMR",
Value = x$SRMR,
Interpretation = interpret_srmr(x$SRMR),
Threshold = 0.08
)
)
}

# RFI
if ("RFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "RFI",
Value = x$RFI,
Interpretation = interpret_rfi(x$RFI),
Threshold = 0.90
)
)
}

# IFI
if ("IFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "IFI",
Value = x$IFI,
Interpretation = interpret_ifi(x$IFI),
Threshold = 0.90
)
)
}

# IFI
if ("PNFI" %in% names(x)) {
table <- rbind(
table,
data.frame(
Name = "PNFI",
Value = x$PNFI,
Interpretation = interpret_pnfi(x$PNFI),
Threshold = 0.50
)
mfits <- c("GFI", "AGFI", "NFI", "NNFI",
"CFI", "RMSEA", "SRMR", "RFI",
"IFI", "PNFI")
mfits <- intersect(names(x), mfits)

table <- lapply(mfits, function(ind_name) {
.interpret_ind <- match.fun(paste0("interpret_", tolower(ind_name)))
data.frame(
Name = ind_name,
Value = x[[ind_name]],
Interpretation = .interpret_ind(x[[ind_name]])
)
}
})

table
do.call(rbind, table)
}
3 changes: 2 additions & 1 deletion man/interpret_gfi.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-interpret.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ if (require("testthat") && require("effectsize")) {
expect_error(interpret_pnfi(0.6, "DUPA"))
expect_error(interpret_rmsea(0.6, "DUPA"))
expect_error(interpret_srmr(0.6, "DUPA"))

skip_on_cran()
skip_if_not_installed("lavaan")
skip_if_not_installed("performance", minimum_version = "0.8.0.1")

structure <- " ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3
dem60 ~ ind60 "
model <- lavaan::sem(structure, data = lavaan::PoliticalDemocracy)
int <- interpret(model)
expect_equal(int$Name, c("GFI", "AGFI", "NFI", "NNFI", "CFI", "RMSEA", "SRMR", "RFI", "IFI", "PNFI"))
expect_equal(int$Value,c(0.9666, 0.9124, 0.9749, 1.0001, 1, 0, 0.0273, 0.9529, 1.0001, 0.5199), tolerance = 0.001)
})

test_that("interpret_icc", {
Expand Down

0 comments on commit dc8666d

Please sign in to comment.