Skip to content

Commit

Permalink
namespace trellis improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvynBladen committed Sep 15, 2023
1 parent 0d2e10c commit 4375b29
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 99 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ Imports:
dplyr,
ggplot2,
gridExtra,
methods,
minerva,
randomForest,
rlang,
stats,
tidyr
Suggests:
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ importFrom(dplyr,group_by)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(ggplot2,aes)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_point)
Expand All @@ -32,9 +31,11 @@ importFrom(ggplot2,xlim)
importFrom(ggplot2,ylab)
importFrom(ggplot2,ylim)
importFrom(gridExtra,grid.arrange)
importFrom(methods,is)
importFrom(minerva,mine)
importFrom(randomForest,importance)
importFrom(randomForest,randomForest)
importFrom(rlang,.data)
importFrom(stats,cor)
importFrom(stats,lm)
importFrom(stats,model.frame)
Expand Down
34 changes: 18 additions & 16 deletions R/ggvip.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#' @importFrom randomForest importance
#' @importFrom dplyr %>% arrange desc filter between case_when
#' @importFrom ggplot2 ggplot geom_point xlim xlab ylab ggtitle theme
#' aes_string element_text
#' aes element_text
#' @importFrom gridExtra grid.arrange
#' @importFrom rlang .data
#' @description A ggplot of variable importance as measured by a Random Forest.
#' @param x An object of class randomForest.
#' @param scale For permutation based measures such as MSE or Accuracy, should
Expand Down Expand Up @@ -83,18 +84,18 @@ ggvip <- function(x, scale = FALSE, sqrt = TRUE, type = "both", num_var) {
)

g <- imp_frame %>%
ggplot(aes_string(
x = colnames(imp_frame)[1],
y = colnames(imp_frame)[2]
ggplot(aes(
x = .data[[colnames(imp_frame)[1]]],
y = .data[[colnames(imp_frame)[2]]]
)) +
geom_point() +
scale_x_continuous(
limits = c(0, newm),
breaks = seq(0, newm, by = newm / div)
) +
ylab(NULL) +
xlab(ifelse(sqrt == FALSE, colnames(imp_frame)[1],
paste0("sqrt(", colnames(imp_frame)[1], ")")
xlab(ifelse(sqrt, paste0("sqrt(", colnames(imp_frame)[1], ")"),
colnames(imp_frame)[1]
)) +
ggtitle("VIP") +
theme(plot.title = element_text(hjust = 0.5))
Expand Down Expand Up @@ -139,18 +140,19 @@ ggvip <- function(x, scale = FALSE, sqrt = TRUE, type = "both", num_var) {
)), ]

g1 <- imp_frame %>%
ggplot(aes_string(
x = colnames(imp_frame)[1],
y = colnames(imp_frame)[3]
ggplot(aes(
x = .data[[colnames(imp_frame)[1]]],
y = .data[[colnames(imp_frame)[3]]]
)) +
geom_point() +
scale_x_continuous(
limits = c(0, newm),
breaks = seq(0, newm, by = newm / div)
) +
ylab(NULL) +
xlab(ifelse(sqrt == FALSE, colnames(imp_frame)[1],
paste0("sqrt(", colnames(imp_frame)[1], ")")
xlab(ifelse(sqrt,
paste0("sqrt(", colnames(imp_frame)[1], ")"),
colnames(imp_frame)[1]
))

imp_frame <- imp_frame[do.call(base::order, as.list(imp_frame[2])), ]
Expand Down Expand Up @@ -183,18 +185,18 @@ ggvip <- function(x, scale = FALSE, sqrt = TRUE, type = "both", num_var) {
)), ]

g2 <- imp_frame %>%
ggplot(aes_string(
x = colnames(imp_frame)[2],
y = colnames(imp_frame)[3]
ggplot(aes(
x = .data[[colnames(imp_frame)[2]]],
y = .data[[colnames(imp_frame)[3]]]
)) +
geom_point() +
scale_x_continuous(
limits = c(0, newm),
breaks = seq(0, newm, by = newm / div)
) +
ylab(NULL) +
xlab(ifelse(sqrt == FALSE, colnames(imp_frame)[2],
paste0("sqrt(", colnames(imp_frame)[2], ")")
xlab(ifelse(sqrt, paste0("sqrt(", colnames(imp_frame)[2], ")"),
colnames(imp_frame)[2]
))

l <- list()
Expand Down
19 changes: 11 additions & 8 deletions R/mtry_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#' @importFrom dplyr %>% arrange across ends_with desc filter select
#' summarise group_by case_when
#' @importFrom ggplot2 ggplot geom_point geom_line ylab ggtitle theme
#' aes_string scale_x_continuous scale_y_continuous
#' aes scale_x_continuous scale_y_continuous
#' @importFrom tidyr pivot_wider
#' @importFrom stats model.frame na.omit quantile
#' @importFrom methods is
#' @importFrom rlang .data
#' @description A list of data.frames and useful plots for user evaluations of
#' the randomForest hyperparameter mtry.
#' @param formula an object of class "\link{formula}" (or one that can be
Expand Down Expand Up @@ -185,9 +187,9 @@ mtry_compare <- function(formula, data = NULL, scale = FALSE, sqrt = TRUE,
)

g1 <- sd %>%
ggplot(aes_string(
x = colnames(sd)[4], y = colnames(sd)[1],
color = colnames(sd)[3], group = colnames(sd)[3]
ggplot(aes(
x = .data[[colnames(sd)[4]]], y = .data[[colnames(sd)[1]]],
color = .data[[colnames(sd)[3]]], group = .data[[colnames(sd)[3]]]
)) +
geom_point() +
geom_line() +
Expand Down Expand Up @@ -218,9 +220,9 @@ mtry_compare <- function(formula, data = NULL, scale = FALSE, sqrt = TRUE,
)

g2 <- sd %>%
ggplot(aes_string(
x = colnames(sd)[4], y = colnames(sd)[2],
color = colnames(sd)[3], group = colnames(sd)[3]
ggplot(aes(
x = .data[[colnames(sd)[4]]], y = .data[[colnames(sd)[2]]],
color = .data[[colnames(sd)[3]]], group = .data[[colnames(sd)[3]]]
)) +
geom_point() +
geom_line() +
Expand Down Expand Up @@ -251,7 +253,8 @@ mtry_compare <- function(formula, data = NULL, scale = FALSE, sqrt = TRUE,
)

g_err <- err_df %>%
ggplot(aes_string(x = colnames(err_df)[1], y = colnames(err_df)[2])) +
ggplot(aes(x = .data[[colnames(err_df)[1]]],
y = .data[[colnames(err_df)[2]]])) +
geom_point() +
geom_line() +
scale_x_continuous(limits = c(1, num_preds), breaks = mvec) +
Expand Down
5 changes: 3 additions & 2 deletions R/robust_vifs.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' Non-linear Variance Inflation Factors
#' @name robust_vifs
#' @importFrom stats lm model.frame
#' @importFrom ggplot2 ggplot geom_point xlim ylim geom_line ggtitle geom_vline
#' @importFrom ggplot2 ggplot geom_point xlim ylim aes
#' geom_line ggtitle geom_vline
#' @importFrom dplyr %>% arrange desc
#' @importFrom car vif
#' @description A list of data.frames and useful plots for user evaluations of
Expand Down Expand Up @@ -78,7 +79,7 @@ robust_vifs <- function(formula, data, model = randomForest,
vdf[k - 1, 5] <- r2
}

if (log10 == TRUE) {
if (log10) {
vdf$lm_vif <- log10(vdf$lm_vif)
vdf$model_vif <- log10(vdf$model_vif)
colnames(vdf)[c(2, 4)] <- c("Log10_lm_vif", "Log10_model_vif")
Expand Down
Loading

0 comments on commit 4375b29

Please sign in to comment.