Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint #287

Merged
merged 3 commits into from
Jul 25, 2024
Merged

Lint #287

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/recheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
workflow_dispatch:
inputs:
which:
type: choice
description: Which dependents to check
options:
- strong
- most

name: Reverse dependency check

jobs:
revdep_check:
name: Reverse check ${{ inputs.which }} dependents
uses: r-devel/recheck/.github/workflows/recheck.yml@v1
with:
which: ${{ inputs.which }}
41 changes: 18 additions & 23 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
linters: linters_with_tags(
tags = c(
"best_practices",
"common_mistakes",
"consistency",
"correctness",
"default",
"best_practices",
"common_mistakes",
"correctness",
"executing"
),
"efficiency",
"executing",
"package_development",
"pkg_testthat",
"readability",
"regex",
"robustness",
"style",
"tidy_design"
),
return_linter(),
object_overwrite_linter(),
unnecessary_concatenation_linter(),
cyclocomp_linter(complexity_limit = 19), # TODO small refactor
implicit_integer_linter = NULL, # TODO refactor
library_call_linter = NULL, # TODO refactor
object_length_linter(),
line_length_linter(120),
todo_comment_linter = NULL, # TODO refactor
implicit_assignment_linter = NULL, # TODO refactor
length_levels_linter = NULL, # TODO refactor
any_duplicated_linter = NULL, # TODO refactor
expect_length_linter = NULL, # TODO refactor
expect_comparison_linter = NULL, # TODO refactor
expect_null_linter = NULL, # TODO refactor
expect_not_linter = NULL, # TODO refactor
expect_named_linter = NULL, # TODO refactor
expect_type_linter = NULL, # TODO refactor
expect_s3_class_linter = NULL, # TODO refactor
fixed_regex_linter = NULL, # TODO refactor
redundant_ifelse_linter = NULL, #TODO refactor
expect_identical_linter = NULL, # TODO big refactor
undesirable_function_linter = NULL, # TODO big refactor
line_length_linter(305), # TODO change to 120 - big refactor
object_length_linter(length = 31), # May or may not refactor
nzchar_linter = NULL, # Whats the point of this lintr?
condition_message_linter = NULL, # Whats the point of this lintr?
condition_call_linter = NULL, # Whats the point of this lintr?
paste_linter = NULL, # Probably never gonna remove this one
object_name_linter = NULL # Probably never gonna remove this one
)

25 changes: 13 additions & 12 deletions R/S3GenericExtenstions.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
#'
c.caretList <- function(...) {
new_model_list <- unlist(lapply(list(...), function(x) {
if (!inherits(x, "caretList")) {
if (!inherits(x, "train")) stop("class of modelList1 must be 'caretList' or 'train'")

# assuming this is a single train object
if (inherits(x, "caretList")) {
x
} else if (inherits(x, "train")) {
x <- list(x)
names(x) <- x[[1]]$method
names(x) <- x[[1L]]$method
x
} else {
stop("class of modelList1 must be 'caretList' or 'train'")
}
x
}), recursive = FALSE)

# Make sure names are unique
Expand Down Expand Up @@ -81,15 +82,15 @@ c.caretList <- function(...) {
#'
c.train <- function(...) {
new_model_list <- unlist(lapply(list(...), function(x) {
if (!inherits(x, "caretList")) {
if (!inherits(x, "train")) stop("class of modelList1 must be 'caretList' or 'train'")

# assuming this is a single train object
if (inherits(x, "caretList")) {
x
} else if (inherits(x, "train")) {
x <- list(x)
names(x) <- x[[1]]$method
names(x) <- x[[1L]]$method
x
} else {
stop("class of modelList1 must be 'caretList' or 'train'")
}
x
}), recursive = FALSE)

# Make sure names are unique
Expand Down
46 changes: 23 additions & 23 deletions R/caretEnsemble.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ summary.caretEnsemble <- function(object, ...) {
val <- getMetric.train(object$ens_model)
cat(paste0("The following models were ensembled: ", types, " \n"))
cat("They were weighted: \n")
cat(paste0(paste0(round(wghts, 4), collapse = " "), "\n"))
cat(paste0("The resulting ", metric, " is: ", round(val, 4), "\n"))
cat(paste0(paste0(round(wghts, 4L), collapse = " "), "\n"))
cat(paste0("The resulting ", metric, " is: ", round(val, 4L), "\n"))

# Add code to compare ensemble to individual models
cat(paste0("The fit for each individual model on the ", metric, " is: \n"))
Expand Down Expand Up @@ -96,7 +96,7 @@ extractModRes <- function(ensemble) {
),
stringsAsFactors = FALSE
)
names(modRes)[2:3] <- c(metric, paste0(metric, "SD"))
names(modRes)[2L:3L] <- c(metric, paste0(metric, "SD"))
modRes
}

Expand Down Expand Up @@ -163,12 +163,12 @@ varImp.caretEnsemble <- function(object, ...) {

# Convert to data.frame
dat <- varImpFrame(coef_importance)
dat[is.na(dat)] <- 0
dat[is.na(dat)] <- 0L
names(dat) <- make.names(names(coef_importance))

# Scale the importances
norm_to_100 <- function(d) d / sum(d) * 100
dat <- apply(dat, 2, norm_to_100)
norm_to_100 <- function(d) d / sum(d) * 100.0
dat <- apply(dat, 2L, norm_to_100)

# Calculate overall importance
model_weights <- coef(object$ens_model$finalModel)
Expand All @@ -179,7 +179,7 @@ varImp.caretEnsemble <- function(object, ...) {
names(model_weights) <- names(object$models)
model_weights <- model_weights[names(model_weights) %in% names(coef_importance)]
model_weights <- abs(model_weights)
overall <- norm_to_100(apply(dat, 1, weighted.mean, w = model_weights))
overall <- norm_to_100(apply(dat, 1L, weighted.mean, w = model_weights))
dat <- data.frame(overall = overall, dat)

# Order by overall importance
Expand All @@ -191,7 +191,7 @@ varImp.caretEnsemble <- function(object, ...) {
#' @keywords internal
# This function only gets called once, in varImp.caretEnsemble
clean_varImp <- function(x) {
names(x$importance)[1] <- "Overall"
names(x$importance)[1L] <- "Overall"
x$importance <- x$importance[, "Overall", drop = FALSE]
x$importance
}
Expand All @@ -206,18 +206,18 @@ varImpFrame <- function(x) {
dat$id <- row.names(dat)
dat$model <- sub("\\.[^\n]*", "", dat$id)
dat$var <- sub("^[^.]*", "", dat$id)
dat$var <- substr(dat$var, 2, nchar(dat$var))
dat$var <- substr(dat$var, 2L, nchar(dat$var))

# Parse intercept variables
dat$var[grep("Inter", dat$var)] <- "Intercept"
dat$var[grep("Inter", dat$var, fixed = TRUE)] <- "Intercept"
dat$id <- NULL
row.names(dat) <- NULL
dat <- reshape(dat,
direction = "wide", v.names = "Overall",
idvar = "var", timevar = "model"
)
row.names(dat) <- dat[, 1]
dat[, -1]
row.names(dat) <- dat[, 1L]
dat[, -1L]
}

#' @title Plot Diagnostics for an caretEnsemble Object
Expand Down Expand Up @@ -252,9 +252,9 @@ plot.caretEnsemble <- function(x, ...) {
theme_bw() +
labs(x = "Individual Model Method", y = metricLab)

if (nrow(x$error) > 0) {
if (nrow(x$error) > 0L) {
plt <- plt +
geom_hline(linetype = 2, linewidth = 0.2, yintercept = min(x$error[[metricLab]]), color = I("red"))
geom_hline(linetype = 2L, linewidth = 0.2, yintercept = min(x$error[[metricLab]]), color = I("red"))
}
plt
}
Expand All @@ -274,7 +274,7 @@ extractPredObsResid <- function(object, show_class_id = 2L) {
obs <- predobs$obs
id <- predobs$rowIndex
if (type == "Regression") {
pred <- pred[[1]]
pred <- pred[[1L]]
} else {
show_class <- levels(object)[show_class_id]
pred <- pred[[show_class]]
Expand Down Expand Up @@ -361,11 +361,11 @@ autoplot.caretEnsemble <- function(object, xvars = NULL, show_class_id = 2L, ...
ymin = .data[["ymin"]],
ymax = .data[["ymax"]]
)) +
ggplot2::geom_point(size = I(3), alpha = I(0.8)) +
ggplot2::geom_point(size = I(3L), alpha = I(0.8)) +
ggplot2::theme_bw() +
ggplot2::geom_smooth(
method = "lm", se = FALSE,
linewidth = I(1.1), color = I("red"), linetype = 2
linewidth = I(1.1), color = I("red"), linetype = 2L
) +
ggplot2::labs(
x = "Fitted Values", y = "Range of Resid.",
Expand All @@ -377,24 +377,24 @@ autoplot.caretEnsemble <- function(object, xvars = NULL, show_class_id = 2L, ...
if (is.null(xvars)) {
xvars <- names(x_data)
xvars <- setdiff(xvars, c(".outcome", ".weights", "(Intercept)"))
xvars <- sample(xvars, 2)
xvars <- sample(xvars, 2L)
}
data.table::set(x_data, j = "id", value = seq_len(nrow(x_data)))
plotdf <- merge(ensemble_data, x_data, by = "id")
g5 <- ggplot2::ggplot(plotdf, ggplot2::aes(.data[[xvars[1]]], .data[["resid"]])) +
ggplot2::geom_point() +
ggplot2::geom_smooth(se = FALSE) +
ggplot2::scale_x_continuous(xvars[1]) +
ggplot2::scale_x_continuous(xvars[1L]) +
ggplot2::scale_y_continuous("Residuals") +
ggplot2::labs(title = paste0("Residuals Against ", xvars[1])) +
ggplot2::labs(title = paste0("Residuals Against ", xvars[1L])) +
ggplot2::theme_bw()
g6 <- ggplot2::ggplot(plotdf, ggplot2::aes(.data[[xvars[2]]], .data[["resid"]])) +
g6 <- ggplot2::ggplot(plotdf, ggplot2::aes(.data[[xvars[2L]]], .data[["resid"]])) +
ggplot2::geom_point() +
ggplot2::geom_smooth(se = FALSE) +
ggplot2::scale_x_continuous(xvars[2]) +
ggplot2::scale_y_continuous("Residuals") +
ggplot2::labs(title = paste0("Residuals Against ", xvars[2])) +
ggplot2::labs(title = paste0("Residuals Against ", xvars[2L])) +
ggplot2::theme_bw()
# nolint end: object_usage_linter
suppressMessages(gridExtra::grid.arrange(g1, g2, g3, g4, g5, g6, ncol = 2))
suppressMessages(gridExtra::grid.arrange(g1, g2, g3, g4, g5, g6, ncol = 2L))
}
Loading
Loading