Skip to content

Commit

Permalink
lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 4, 2024
1 parent 49b885f commit 435f975
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 91 deletions.
8 changes: 3 additions & 5 deletions R/r2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 900 in R/r2.R

View check run for this annotation

Codecov / codecov/patch

R/r2.R#L899-L900

Added lines #L899 - L900 were not covered by tests
} else {
if (is.null(w)) {
mss <- sum(f^2)
} else {
mss <- sum(w * f^2)
}
mss <- sum(w * f^2)

Check warning on line 902 in R/r2.R

View check run for this annotation

Codecov / codecov/patch

R/r2.R#L902

Added line #L902 was not covered by tests
}
if (is.null(w)) {
rss <- sum(r^2)
Expand Down
42 changes: 20 additions & 22 deletions R/r2_bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
36 changes: 18 additions & 18 deletions R/r2_coxsnell.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 81 in R/r2_coxsnell.R

View check run for this annotation

Codecov / codecov/patch

R/r2_coxsnell.R#L81

Added line #L81 was not covered by tests
}
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
Expand All @@ -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)

Check warning on line 105 in R/r2_coxsnell.R

View check run for this annotation

Codecov / codecov/patch

R/r2_coxsnell.R#L105

Added line #L105 was not covered by tests
# if no deviance, return NULL
if (is.null(dev)) {
return(NULL)

Check warning on line 108 in R/r2_coxsnell.R

View check run for this annotation

Codecov / codecov/patch

R/r2_coxsnell.R#L107-L108

Added lines #L107 - L108 were not covered by tests
}
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

Check warning on line 113 in R/r2_coxsnell.R

View check run for this annotation

Codecov / codecov/patch

R/r2_coxsnell.R#L110-L113

Added lines #L110 - L113 were not covered by tests
}


Expand Down
20 changes: 10 additions & 10 deletions R/test_bf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)[["..."]]

Check warning on line 13 in R/test_bf.R

View check run for this annotation

Codecov / codecov/patch

R/test_bf.R#L12-L13

Added lines #L12 - L13 were not covered by tests

# validation checks (will throw error if non-valid objects)
.test_performance_checks(objects, multiple = FALSE)
.test_performance_checks(objects = my_objects, multiple = FALSE)

Check warning on line 16 in R/test_bf.R

View check run for this annotation

Codecov / codecov/patch

R/test_bf.R#L16

Added line #L16 was not covered by tests

if (length(objects) == 1 && isTRUE(insight::is_model(objects))) {
if (length(my_objects) == 1 && isTRUE(insight::is_model(my_objects))) {

Check warning on line 18 in R/test_bf.R

View check run for this annotation

Codecov / codecov/patch

R/test_bf.R#L18

Added line #L18 was not covered by tests
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

Check warning on line 20 in R/test_bf.R

View check run for this annotation

Codecov / codecov/patch

R/test_bf.R#L20

Added line #L20 was not covered by tests
)
}

# 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)

Check warning on line 26 in R/test_bf.R

View check run for this annotation

Codecov / codecov/patch

R/test_bf.R#L25-L26

Added lines #L25 - L26 were not covered by tests
} else {
insight::format_error("The models cannot be compared for some reason :/")
}
Expand Down Expand Up @@ -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"
}
}
22 changes: 11 additions & 11 deletions R/test_likelihoodratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 47 in R/test_likelihoodratio.R

View check run for this annotation

Codecov / codecov/patch

R/test_likelihoodratio.R#L46-L47

Added lines #L46 - L47 were not covered by tests
} else {
insight::format_error(
"The models are not nested, which is a prerequisite for `test_likelihoodratio()`.",
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 8 additions & 8 deletions R/test_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 :/")
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Check warning on line 456 in R/test_performance.R

View check run for this annotation

Codecov / codecov/patch

R/test_performance.R#L456

Added line #L456 was not covered by tests
)
}

Expand Down
10 changes: 5 additions & 5 deletions R/test_vuong.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 :/")
}
Expand Down
24 changes: 12 additions & 12 deletions R/test_wald.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 :/")
}
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit 435f975

Please sign in to comment.