R/calculate_oscillations.R
calculate_oscillations.Rd
Oscillations are proxies for local feature importance at the instance level. Find more details in Ceteris Paribus Oscillations Chapter.
calculate_oscillations(x, sort = TRUE, ...)
a ceteris paribus explainer produced with the ceteris_paribus()
function
a logical value. If TRUE
then rows are sorted along the oscillations
other arguments
an object of the class ceteris_paribus_oscillations
Explanatory Model Analysis. Explore, Explain, and Examine Predictive Models. https://ema.drwhy.ai/
library("DALEX")
library("ingredients")
titanic_small <- select_sample(titanic_imputed, n = 500, seed = 1313)
# build a model
model_titanic_glm <- glm(survived ~ gender + age + fare,
data = titanic_small, family = "binomial")
explain_titanic_glm <- explain(model_titanic_glm,
data = titanic_small[,-8],
y = titanic_small[,8])
#> Preparation of a new explainer is initiated
#> -> model label : lm ( default )
#> -> data : 500 rows 7 cols
#> -> target variable : 500 values
#> -> predict function : yhat.glm will be used ( default )
#> -> predicted values : No value for predict function target column. ( default )
#> -> model_info : package stats , ver. 4.2.2 , task classification ( default )
#> -> predicted values : numerical, min = 0.0795294 , mean = 0.302 , max = 0.9859411
#> -> residual function : difference between y and yhat ( default )
#> -> residuals : numerical, min = -0.8204691 , mean = 8.796651e-12 , max = 0.8567173
#> A new explainer has been created!
cp_rf <- ceteris_paribus(explain_titanic_glm, titanic_small[1,])
calculate_oscillations(cp_rf)
#> _vname_ _ids_ oscillations
#> 1 gender 515 0.20738245
#> 2 age 515 0.05371374
#> 5 fare 515 0.04197095
#> 3 class 515 0.00000000
#> 4 embarked 515 0.00000000
#> 6 sibsp 515 0.00000000
#> 7 parch 515 0.00000000
# \donttest{
library("ranger")
apartments_rf_model <- ranger(m2.price ~ construction.year + surface + floor +
no.rooms + district, data = apartments)
explainer_rf <- explain(apartments_rf_model,
data = apartments_test[,-1],
y = apartments_test$m2.price,
label = "ranger forest",
verbose = FALSE)
apartment <- apartments_test[1,]
cp_rf <- ceteris_paribus(explainer_rf, apartment)
calculate_oscillations(cp_rf)
#> _vname_ _ids_ oscillations
#> 5 district 1001 1161.14035
#> 2 surface 1001 383.71890
#> 3 floor 1001 327.63667
#> 4 no.rooms 1001 75.01989
#> 1 construction.year 1001 51.28965
# }