Skip to content

Commit

Permalink
candidate fix for #149
Browse files Browse the repository at this point in the history
  • Loading branch information
pbiecek committed Jan 14, 2023
1 parent f061a70 commit 67452a6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions R/aggregate_profiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions R/describe_aggregated_profiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ 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_'])
model_name <- paste(toupper(substr(model_name, 1, 1)), substr(model_name, 2, nchar(model_name)), sep="")


# 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,
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions R/describe_ceteris_paribus.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand All @@ -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,
Expand Down Expand Up @@ -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]), ]
Expand Down
2 changes: 1 addition & 1 deletion R/describe_feature_importance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 67452a6

Please sign in to comment.