diff --git a/R/r2.R b/R/r2.R index dacdef614..2789b5efe 100644 --- a/R/r2.R +++ b/R/r2.R @@ -896,12 +896,10 @@ r2.DirichletRegModel <- function(model, ...) { m <- sum(w * f / sum(w)) mss <- sum(w * (f - m)^2) } + } else if (is.null(w)) { + mss <- sum(f^2) } else { - if (is.null(w)) { - mss <- sum(f^2) - } else { - mss <- sum(w * f^2) - } + mss <- sum(w * f^2) } if (is.null(w)) { rss <- sum(r^2) diff --git a/R/r2_bayes.R b/R/r2_bayes.R index 24e8269ba..71a44ef43 100644 --- a/R/r2_bayes.R +++ b/R/r2_bayes.R @@ -201,28 +201,26 @@ r2_posterior.brmsfit <- function(model, verbose = TRUE, ...) { }) names(br2) <- res } + } else if (mi$is_mixed) { + br2 <- list( + R2_Bayes = as.vector(rstantools::bayes_R2( + model, + re.form = NULL, + re_formula = NULL, + summary = FALSE + )), + R2_Bayes_marginal = as.vector(rstantools::bayes_R2( + model, + re.form = NA, + re_formula = NA, + summary = FALSE + )) + ) + names(br2$R2_Bayes) <- rep("Conditional R2", length(br2$R2_Bayes)) + names(br2$R2_Bayes_marginal) <- rep("Marginal R2", length(br2$R2_Bayes)) } else { - if (mi$is_mixed) { - br2 <- list( - R2_Bayes = as.vector(rstantools::bayes_R2( - model, - re.form = NULL, - re_formula = NULL, - summary = FALSE - )), - R2_Bayes_marginal = as.vector(rstantools::bayes_R2( - model, - re.form = NA, - re_formula = NA, - summary = FALSE - )) - ) - names(br2$R2_Bayes) <- rep("Conditional R2", length(br2$R2_Bayes)) - names(br2$R2_Bayes_marginal) <- rep("Marginal R2", length(br2$R2_Bayes)) - } else { - br2 <- list(R2_Bayes = as.vector(rstantools::bayes_R2(model, summary = FALSE))) - names(br2$R2_Bayes) <- rep("R2", length(br2$R2_Bayes)) - } + br2 <- list(R2_Bayes = as.vector(rstantools::bayes_R2(model, summary = FALSE))) + names(br2$R2_Bayes) <- rep("R2", length(br2$R2_Bayes)) } br2 @@ -400,7 +398,7 @@ as.data.frame.r2_bayes <- function(x, ...) { # remove sig and g cols params_theta <- params[, !grepl(pattern = "^sig2$|^g_|^g$", colnames(params))] - params_sigma <- sqrt(params[, grepl(pattern = "^sig2$", colnames(params))]) + params_sigma <- sqrt(params[, colnames(params) == "sig2"]) # Model Matrix mm <- insight::get_modelmatrix(model[1]) diff --git a/R/r2_coxsnell.R b/R/r2_coxsnell.R index d56803f5c..042e5461a 100644 --- a/R/r2_coxsnell.R +++ b/R/r2_coxsnell.R @@ -69,20 +69,20 @@ r2_coxsnell.glm <- function(model, verbose = TRUE, ...) { if (is.null(info)) { info <- suppressWarnings(insight::model_info(model, verbose = FALSE)) } + # Cox & Snell's R2 is not defined for binomial models that are not Bernoulli models if (info$is_binomial && !info$is_bernoulli && class(model)[1] == "glm") { if (verbose) { insight::format_alert("Can't calculate accurate R2 for binomial models that are not Bernoulli models.") } return(NULL) - } else { - # if no deviance, return NA - if (is.null(model$deviance)) { - return(NULL) - } - r2_coxsnell <- (1 - exp((model$deviance - model$null.deviance) / insight::n_obs(model, disaggregate = TRUE))) - names(r2_coxsnell) <- "Cox & Snell's R2" - r2_coxsnell } + # if no deviance, return NULL + if (is.null(model$deviance)) { + return(NULL) + } + r2_coxsnell <- (1 - exp((model$deviance - model$null.deviance) / insight::n_obs(model, disaggregate = TRUE))) + names(r2_coxsnell) <- "Cox & Snell's R2" + r2_coxsnell } #' @export @@ -95,22 +95,22 @@ r2_coxsnell.glmmTMB <- function(model, verbose = TRUE, ...) { if (is.null(info)) { info <- suppressWarnings(insight::model_info(model, verbose = FALSE)) } + # Cox & Snell's R2 is not defined for binomial models that are not Bernoulli models if (info$is_binomial && !info$is_bernoulli) { if (verbose) { insight::format_alert("Can't calculate accurate R2 for binomial models that are not Bernoulli models.") } return(NULL) - } else { - dev <- stats::deviance(model) - # if no deviance, return NA - if (is.null(dev)) { - return(NULL) - } - null_dev <- stats::deviance(insight::null_model(model)) - r2_coxsnell <- (1 - exp((dev - null_dev) / insight::n_obs(model, disaggregate = TRUE))) - names(r2_coxsnell) <- "Cox & Snell's R2" - r2_coxsnell } + dev <- stats::deviance(model) + # if no deviance, return NULL + if (is.null(dev)) { + return(NULL) + } + null_dev <- stats::deviance(insight::null_model(model)) + r2_coxsnell <- (1 - exp((dev - null_dev) / insight::n_obs(model, disaggregate = TRUE))) + names(r2_coxsnell) <- "Cox & Snell's R2" + r2_coxsnell } diff --git a/R/test_bf.R b/R/test_bf.R index 60cb3a129..38b0e421d 100644 --- a/R/test_bf.R +++ b/R/test_bf.R @@ -9,21 +9,21 @@ test_bf <- function(...) { #' @export test_bf.default <- function(..., reference = 1, text_length = NULL) { # Attribute class to list and get names from the global environment - objects <- insight::ellipsis_info(..., only_models = TRUE) - names(objects) <- match.call(expand.dots = FALSE)$`...` + my_objects <- insight::ellipsis_info(..., only_models = TRUE) + names(my_objects) <- match.call(expand.dots = FALSE)[["..."]] # validation checks (will throw error if non-valid objects) - .test_performance_checks(objects, multiple = FALSE) + .test_performance_checks(objects = my_objects, multiple = FALSE) - if (length(objects) == 1 && isTRUE(insight::is_model(objects))) { + if (length(my_objects) == 1 && isTRUE(insight::is_model(my_objects))) { insight::format_error( - "`test_bf()` is designed to compare multiple models together. For a single model, you might want to run `bayestestR::bf_parameters()` instead." + "`test_bf()` is designed to compare multiple models together. For a single model, you might want to run `bayestestR::bf_parameters()` instead." # nolint ) } # If a suitable class is found, run the more specific method on it - if (inherits(objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { - test_bf(objects, reference = reference, text_length = text_length) + if (inherits(my_objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { + test_bf(my_objects, reference = reference, text_length = text_length) } else { insight::format_error("The models cannot be compared for some reason :/") } @@ -87,9 +87,9 @@ test_bf.ListModels <- function(objects, reference = 1, text_length = NULL, ...) if (all(bayesian_models)) { "yes" - } else if (!all(bayesian_models)) { - "no" - } else { + } else if (any(bayesian_models)) { "mixed" + } else { + "no" } } diff --git a/R/test_likelihoodratio.R b/R/test_likelihoodratio.R index 9784b13a9..529516487 100644 --- a/R/test_likelihoodratio.R +++ b/R/test_likelihoodratio.R @@ -22,29 +22,29 @@ test_lrt <- test_likelihoodratio #' @export test_likelihoodratio.default <- function(..., estimator = "OLS", verbose = TRUE) { # Attribute class to list - objects <- insight::ellipsis_info(..., only_models = TRUE) + my_objects <- insight::ellipsis_info(..., only_models = TRUE) # validation checks (will throw error if non-valid objects) - objects <- .test_performance_checks(objects, verbose = verbose) + my_objects <- .test_performance_checks(my_objects, verbose = verbose) # different default when mixed model or glm is included if (missing(estimator)) { - mixed_models <- sapply(objects, insight::is_mixed_model) - if (all(mixed_models) && all(sapply(objects, .is_lmer_reml)) && isTRUE(attributes(objects)$same_fixef)) { + mixed_models <- sapply(my_objects, insight::is_mixed_model) + if (all(mixed_models) && all(sapply(my_objects, .is_lmer_reml)) && isTRUE(attributes(my_objects)$same_fixef)) { estimator <- "REML" - } else if (any(mixed_models) || !all(attributes(objects)$is_linear)) { + } else if (any(mixed_models) || !all(attributes(my_objects)$is_linear)) { estimator <- "ML" } } # ensure proper object names - objects <- .check_objectnames(objects, sapply(match.call(expand.dots = FALSE)$`...`, as.character)) + my_objects <- .check_objectnames(my_objects, sapply(match.call(expand.dots = FALSE)[["..."]], as.character)) # If a suitable class is found, run the more specific method on it - if (inherits(objects, "ListNestedRegressions")) { - test_likelihoodratio(objects, estimator = estimator) - } else if (inherits(objects, "ListLavaan")) { - test_likelihoodratio_ListLavaan(..., objects = objects) # Because lavaanLRT requires the ellipsis + if (inherits(my_objects, "ListNestedRegressions")) { + test_likelihoodratio(my_objects, estimator = estimator) + } else if (inherits(my_objects, "ListLavaan")) { + test_likelihoodratio_ListLavaan(..., objects = my_objects) # Because lavaanLRT requires the ellipsis } else { insight::format_error( "The models are not nested, which is a prerequisite for `test_likelihoodratio()`.", @@ -106,7 +106,7 @@ test_likelihoodratio.ListNestedRegressions <- function(objects, estimator = "ML" same_fixef <- attributes(objects)$same_fixef # sort by df - if (!all(sort(dfs) == dfs) && !all(sort(dfs) == rev(dfs))) { + if (is.unsorted(dfs) && is.unsorted(rev(dfs))) { objects <- objects[order(dfs)] dfs <- sort(dfs, na.last = TRUE) } diff --git a/R/test_performance.R b/R/test_performance.R index b643143f7..f818b02dd 100644 --- a/R/test_performance.R +++ b/R/test_performance.R @@ -236,17 +236,17 @@ test_performance <- function(..., reference = 1, verbose = TRUE) { #' @export test_performance.default <- function(..., reference = 1, include_formula = FALSE, verbose = TRUE) { # Attribute class to list and get names from the global environment - objects <- insight::ellipsis_info(..., only_models = TRUE) + my_objects <- insight::ellipsis_info(..., only_models = TRUE) # validation checks (will throw error if non-valid objects) - objects <- .test_performance_checks(objects, verbose = verbose) + my_objects <- .test_performance_checks(my_objects, verbose = verbose) # ensure proper object names - objects <- .check_objectnames(objects, sapply(match.call(expand.dots = FALSE)$`...`, as.character)) + my_objects <- .check_objectnames(my_objects, sapply(match.call(expand.dots = FALSE)[["..."]], as.character)) # If a suitable class is found, run the more specific method on it - if (inherits(objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { - test_performance(objects, reference = reference, include_formula = include_formula) + if (inherits(my_objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { + test_performance(my_objects, reference = reference, include_formula = include_formula) } else { insight::format_error("The models cannot be compared for some reason :/") } @@ -421,10 +421,10 @@ test_performance.ListNonNestedRegressions <- function(objects, .test_performance_init <- function(objects, include_formula = FALSE) { - names <- insight::model_name(objects, include_formula = include_formula) + model_names <- insight::model_name(objects, include_formula = include_formula) out <- data.frame( Name = names(objects), - Model = names, + Model = model_names, stringsAsFactors = FALSE ) row.names(out) <- NULL @@ -453,7 +453,7 @@ test_performance.ListNonNestedRegressions <- function(objects, if (same_response && !inherits(objects, "ListLavaan") && isFALSE(attributes(objects)$same_response)) { insight::format_error( - "The models' dependent variables don't have the same data, which is a prerequisite to compare them. Probably the proportion of missing data differs between models." + "The models' dependent variables don't have the same data, which is a prerequisite to compare them. Probably the proportion of missing data differs between models." # nolint ) } diff --git a/R/test_vuong.R b/R/test_vuong.R index 449f55ddb..f0cce6d16 100644 --- a/R/test_vuong.R +++ b/R/test_vuong.R @@ -8,17 +8,17 @@ test_vuong <- function(..., verbose = TRUE) { #' @export test_vuong.default <- function(..., reference = 1, verbose = TRUE) { # Attribute class to list and get names from the global environment - objects <- insight::ellipsis_info(..., only_models = TRUE) + my_objects <- insight::ellipsis_info(..., only_models = TRUE) # validation checks (will throw error if non-valid objects) - objects <- .test_performance_checks(objects, verbose = verbose) + my_objects <- .test_performance_checks(my_objects, verbose = verbose) # ensure proper object names - objects <- .check_objectnames(objects, sapply(match.call(expand.dots = FALSE)$`...`, as.character)) + my_objects <- .check_objectnames(my_objects, sapply(match.call(expand.dots = FALSE)[["..."]], as.character)) # If a suitable class is found, run the more specific method on it - if (inherits(objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { - test_vuong(objects, reference = reference) + if (inherits(my_objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { + test_vuong(my_objects, reference = reference) } else { insight::format_error("The models cannot be compared for some reason :/") } diff --git a/R/test_wald.R b/R/test_wald.R index 0e043502a..ddfe21829 100644 --- a/R/test_wald.R +++ b/R/test_wald.R @@ -8,17 +8,17 @@ test_wald <- function(..., verbose = TRUE) { #' @export test_wald.default <- function(..., verbose = TRUE) { # Attribute class to list and get names from the global environment - objects <- insight::ellipsis_info(..., only_models = TRUE) + my_objects <- insight::ellipsis_info(..., only_models = TRUE) # validation checks (will throw error if non-valid objects) - objects <- .test_performance_checks(objects, verbose = verbose) + my_objects <- .test_performance_checks(my_objects, verbose = verbose) # ensure proper object names - objects <- .check_objectnames(objects, sapply(match.call(expand.dots = FALSE)$`...`, as.character)) + my_objects <- .check_objectnames(my_objects, sapply(match.call(expand.dots = FALSE)[["..."]], as.character)) # If a suitable class is found, run the more specific method on it - if (inherits(objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { - test_wald(objects) + if (inherits(my_objects, c("ListNestedRegressions", "ListNonNestedRegressions", "ListLavaan"))) { + test_wald(my_objects) } else { insight::format_error("The models cannot be compared for some reason :/") } @@ -37,10 +37,10 @@ test_wald.ListNestedRegressions <- function(objects, verbose = TRUE, ...) { ) } return(test_likelihoodratio(objects)) - } else { - out <- .test_wald(objects, test = "F") } + out <- .test_wald(objects, test = "F") + attr(out, "is_nested") <- TRUE class(out) <- c("test_performance", class(out)) out @@ -60,7 +60,7 @@ test_wald.ListNonNestedRegressions <- function(objects, verbose = TRUE, ...) { dfs <- sapply(objects, insight::get_df, type = "residual") # sort by df - if (!all(sort(dfs) == dfs) && !all(sort(dfs) == rev(dfs))) { + if (is.unsorted(dfs) && is.unsorted(rev(dfs))) { objects <- objects[order(dfs)] dfs <- sort(dfs, na.last = TRUE) } @@ -78,18 +78,18 @@ test_wald.ListNonNestedRegressions <- function(objects, verbose = TRUE, ...) { # Find reference-model related stuff refmodel <- order(dfs)[1] - scale <- dev[refmodel] / dfs[refmodel] + my_scale <- dev[refmodel] / dfs[refmodel] # test = "F" if (test == "F") { - f_value <- (dev_diff / dfs_diff) / scale + f_value <- (dev_diff / dfs_diff) / my_scale f_value[!is.na(f_value) & f_value < 0] <- NA # rather than p = 0 - out$`F` <- f_value + out[["F"]] <- f_value p <- stats::pf(f_value, abs(dfs_diff), dfs[refmodel], lower.tail = FALSE) # test = "LRT" } else { - chi2 <- dev_diff / scale * sign(dfs_diff) + chi2 <- dev_diff / my_scale * sign(dfs_diff) chi2[!is.na(chi2) & chi2 < 0] <- NA # rather than p = 0 out$Chi2 <- chi2 p <- stats::pchisq(chi2, abs(dfs_diff), lower.tail = FALSE)