diff --git a/R/aggregate_profiles.R b/R/aggregate_profiles.R index 8c54b7c..d928222 100644 --- a/R/aggregate_profiles.R +++ b/R/aggregate_profiles.R @@ -104,7 +104,7 @@ aggregate_profiles <- function(x, ..., elist <- list(...) if (length(elist) > 1) { # only ceteris_paribus_explainer objects - elist <- elist[sapply(elist, function(x) "ceteris_paribus_explainer" %in% class(x))] + elist <- elist[sapply(elist, is, "ceteris_paribus_explainer")] } else { elist <- NULL } @@ -166,7 +166,7 @@ aggregate_profiles <- function(x, ..., all_profiles$`_x_`[tmp == viname] <- all_profiles[tmp == viname, viname] } - if (class(all_profiles) != "data.frame") { + if (!is(all_profiles, "data.frame")) { all_profiles <- as.data.frame(all_profiles) } diff --git a/R/describe_aggregated_profiles.R b/R/describe_aggregated_profiles.R index 6e94596..5bc14e8 100644 --- a/R/describe_aggregated_profiles.R +++ b/R/describe_aggregated_profiles.R @@ -44,7 +44,7 @@ describe.partial_dependence_explainer <- function(x, # ERROR HANDLING if (length(unique(x[ ,'_vname_'])) == 1) variables <- as.character(x[1,'_vname_']) if (is.null(variables)) stop("Choose a single variable to be described.") - if (!class(variables) == "character") stop("Enter the single variables name as character.") + if (!is(variables, "character")) stop("Enter the single variables name as character.") # Assigning model's name model_name <- as.character(x[1,'_label_']) @@ -52,7 +52,7 @@ describe.partial_dependence_explainer <- function(x, # Generating description - if (class(x[ ,'_x_']) == "numeric") { + if (is(x[ ,'_x_'], "numeric")) { description <- describe_aggregated_profiles_continuous(x = x, nonsignificance_treshold = nonsignificance_treshold, display_values = display_values, @@ -274,7 +274,7 @@ specify_df_aggregated <- function(x, variables, nonsignificance_treshold) { treshold <- NULL - if (class(df[ ,variables]) == "factor" | class(df[ ,variables]) == "character") { + if (is(df[ ,variables], "factor") | is(df[ ,variables], "character")) { df['importance'] <- sapply(df[ ,'_yhat_'], function(x) abs(x-baseline_prediction)) df['importance'] <- round(df['importance'],3) diff --git a/R/describe_ceteris_paribus.R b/R/describe_ceteris_paribus.R index 40379f0..2cd0ee2 100644 --- a/R/describe_ceteris_paribus.R +++ b/R/describe_ceteris_paribus.R @@ -60,14 +60,14 @@ describe.ceteris_paribus_explainer <- function(x, # ERROR HANDLING if (length(unique(x[ ,'_vname_'])) == 1) variables <- as.character(x[1,'_vname_']) if (is.null(variables)) stop("Choose a single variable to be described.") - if (!class(variables) == "character") stop("Enter the single variables name as character.") + if (!is(variables, "character")) stop("Enter the single variables name as character.") # Assigning model's name model_name <- as.character(x[1,'_label_']) model_name <- paste(toupper(substr(model_name, 1, 1)), substr(model_name, 2, nchar(model_name)), sep="") # Assigning a value to the choosen variable - variables_value <- ifelse(class(attr(x, "observations")[,variables]) == 'numeric', + variables_value <- ifelse(is(attr(x, "observations")[,variables], "numeric"), round(attr(x, "observations")[,variables],3), as.character(attr(x, "observations")[,variables])) @@ -77,7 +77,7 @@ describe.ceteris_paribus_explainer <- function(x, "") # Generating description - if (class(x[ ,variables]) == "numeric") { + if (is(x[ ,variables], "numeric")) { description <- describe_ceteris_paribus_continuous(x = x, nonsignificance_treshold = nonsignificance_treshold, display_values = display_values, @@ -117,11 +117,11 @@ specify_df <- function(x, variables, nonsignificance_treshold) { df <- df[ ,c(variables,"_yhat_")] treshold <- NULL - if (class(df[ ,variables]) == "factor" | class(df[ ,variables]) == "character") { - #choosing the prediction value for the observation being explained + if (is(df[ ,variables], "factor") | is(df[ ,variables], "character")) { + # choosing the prediction value for the observation being explained baseline_prediction <- attr(x, "observations")[1,'_yhat_'] df['importance'] <- sapply(df[ ,'_yhat_'], function(x) abs(x-baseline_prediction)) - df['importance'] <- round(df['importance'],3) + df['importance'] <- round(df['importance'], 3) df <- df[order(df[ ,'importance'], decreasing = TRUE), ] df['variable_name'] <- paste0('"', df[ ,variables],'"') df <- df[-which(df[ ,variables] == attr(x, "observations")[1,variables]), ] diff --git a/R/describe_feature_importance.R b/R/describe_feature_importance.R index 5570676..7d3c819 100644 --- a/R/describe_feature_importance.R +++ b/R/describe_feature_importance.R @@ -28,7 +28,7 @@ describe.feature_importance_explainer <- function(x, ...) { #Error handling - if (!(class(nonsignificance_treshold) == 'numeric')) { + if (!is(nonsignificance_treshold, "numeric")) { stop("Arguments are not valid") } # fix for https://github.com/ModelOriented/ingredients/issues/95