diff --git a/R/icc.R b/R/icc.R index 46c1a331a..47fbb9a45 100644 --- a/R/icc.R +++ b/R/icc.R @@ -532,14 +532,12 @@ print.icc_decomposed <- function(x, digits = 2, ...) { name_full = "ICC", verbose = TRUE) { vars <- tryCatch( - { - insight::get_variance(model, - name_fun = name_fun, - name_full = name_full, - tolerance = tolerance, - verbose = verbose - ) - }, + insight::get_variance(model, + name_fun = name_fun, + name_full = name_full, + tolerance = tolerance, + verbose = verbose + ), error = function(e) { if (inherits(e, c("simpleError", "error")) && verbose) { insight::print_color(e$message, "red") @@ -597,7 +595,7 @@ print.icc_decomposed <- function(x, digits = 2, ...) { # prepare arguments for "lme4::bootMer" .do_lme4_bootmer <- function(model, .boot_fun, iterations, dots) { insight::check_if_installed(c("lme4", "boot")) - args <- list( + my_args <- list( model, .boot_fun, nsim = iterations, @@ -608,25 +606,25 @@ print.icc_decomposed <- function(x, digits = 2, ...) { ) # add/overwrite dot-args if (!is.null(dots[["use.u"]])) { - args$use.u <- dots[["use.u"]] + my_args$use.u <- dots[["use.u"]] } if (!is.null(dots[["re.form"]])) { - args$re.form <- dots[["re.form"]] + my_args$re.form <- dots[["re.form"]] } if (!is.null(dots[["type"]])) { - args$type <- dots[["type"]] - if (args$type == "semiparametric") { - args$use.u <- TRUE + my_args$type <- dots[["type"]] + if (my_args$type == "semiparametric") { + my_args$use.u <- TRUE } } if (!is.null(dots[["parallel"]])) { - args$parallel <- dots[["parallel"]] + my_args$parallel <- dots[["parallel"]] } if (!is.null(dots[["ncpus"]])) { - args$ncpus <- dots[["ncpus"]] + my_args$ncpus <- dots[["ncpus"]] } # bootsrap - do.call(lme4::bootMer, args) + do.call(lme4::bootMer, args = my_args) } @@ -664,12 +662,10 @@ print.icc_decomposed <- function(x, digits = 2, ...) { } model_rank <- tryCatch( - { - if (!is.null(model$rank)) { - model$rank - df_int - } else { - insight::n_parameters(model) - df_int - } + if (!is.null(model$rank)) { + model$rank - df_int + } else { + insight::n_parameters(model) - df_int }, error = function(e) insight::n_parameters(model) - df_int ) diff --git a/R/r2_loo.R b/R/r2_loo.R index 040d9b572..dbcaf38b3 100644 --- a/R/r2_loo.R +++ b/R/r2_loo.R @@ -50,10 +50,10 @@ r2_loo <- function(model, robust = TRUE, ci = 0.95, verbose = TRUE, ...) { loo_r2 <- structure( class = "r2_loo", lapply(loo_r2, ifelse(robust, stats::median, mean)), - "SE" = lapply(loo_r2, ifelse(robust, stats::mad, stats::sd)), + SE = lapply(loo_r2, ifelse(robust, stats::mad, stats::sd)), # "Estimates" = lapply(r2_bayesian, bayestestR::point_estimate, centrality = "all", dispersion = TRUE), - "CI" = lapply(loo_r2, bayestestR::hdi, ci = ci), - "robust" = robust + CI = lapply(loo_r2, bayestestR::hdi, ci = ci), + robust = robust ) return(loo_r2) } @@ -84,13 +84,13 @@ r2_loo_posterior.brmsfit <- function(model, verbose = TRUE, ...) { res <- insight::find_response(model) if (mi[[1]]$is_mixed) { br2_mv <- list( - "R2_loo" = rstantools::loo_R2( + R2_loo = rstantools::loo_R2( model, re.form = NULL, re_formula = NULL, summary = FALSE ), - "R2_loo_marginal" = rstantools::loo_R2( + R2_loo_marginal = rstantools::loo_R2( model, re.form = NA, re_formula = NA, @@ -99,40 +99,38 @@ r2_loo_posterior.brmsfit <- function(model, verbose = TRUE, ...) { ) br2 <- lapply(seq_along(res), function(x) { list( - "R2_loo" = unname(as.vector(br2_mv$R2_loo[, x])), - "R2_loo_marginal" = unname(as.vector(br2_mv$R2_loo_marginal[, x])) + R2_loo = unname(as.vector(br2_mv$R2_loo[, x])), + R2_loo_marginal = unname(as.vector(br2_mv$R2_loo_marginal[, x])) ) }) names(br2) <- res } else { - br2_mv <- list("R2_loo" = rstantools::loo_R2(model, summary = FALSE)) + br2_mv <- list(R2_loo = rstantools::loo_R2(model, summary = FALSE)) br2 <- lapply(seq_along(res), function(x) { - list("R2_loo" = unname(as.vector(br2_mv$R2_loo[, x]))) + list(R2_loo = unname(as.vector(br2_mv$R2_loo[, x]))) }) names(br2) <- res } + } else if (mi$is_mixed) { + br2 <- list( + R2_loo = as.vector(rstantools::loo_R2( + model, + re.form = NULL, + re_formula = NULL, + summary = FALSE + )), + R2_loo_marginal = as.vector(rstantools::loo_R2( + model, + re.form = NA, + re_formula = NA, + summary = FALSE + )) + ) + names(br2$R2_loo) <- rep("Conditional R2_adjusted", length(br2$R2_loo)) + names(br2$R2_loo_marginal) <- rep("Marginal R2_adjusted", length(br2$R2_loo)) } else { - if (mi$is_mixed) { - br2 <- list( - "R2_loo" = as.vector(rstantools::loo_R2( - model, - re.form = NULL, - re_formula = NULL, - summary = FALSE - )), - "R2_loo_marginal" = as.vector(rstantools::loo_R2( - model, - re.form = NA, - re_formula = NA, - summary = FALSE - )) - ) - names(br2$R2_loo) <- rep("Conditional R2_adjusted", length(br2$R2_loo)) - names(br2$R2_loo_marginal) <- rep("Marginal R2_adjusted", length(br2$R2_loo)) - } else { - br2 <- list("R2_loo" = as.vector(rstantools::loo_R2(model, summary = FALSE))) - names(br2$R2_loo) <- rep("R2_adjusted", length(br2$R2_loo)) - } + br2 <- list(R2_loo = as.vector(rstantools::loo_R2(model, summary = FALSE))) + names(br2$R2_loo) <- rep("R2_adjusted", length(br2$R2_loo)) } br2