library("DALEX")
library("modelStudio")
#:# ex1 classification on 'titanic' data
# fit a model
model_titanic <- glm(survived ~., data = titanic_imputed, family = "binomial")
# create an explainer for the model
explainer_titanic <- explain(model_titanic,
data = titanic_imputed,
y = titanic_imputed$survived,
label = "Titanic GLM")
# pick observations
new_observations <- titanic_imputed[1:2,]
rownames(new_observations) <- c("Lucas","James")
# make a studio for the model
modelStudio(explainer_titanic,
new_observations,
N = 200, B = 5) # faster example
# \donttest{
#:# ex2 regression on 'apartments' data
if (requireNamespace("ranger", quietly=TRUE)) {
library("ranger")
model_apartments <- ranger(m2.price ~. ,data = apartments)
explainer_apartments <- explain(model_apartments,
data = apartments,
y = apartments$m2.price)
new_apartments <- apartments[1:2,]
rownames(new_apartments) <- c("ap1","ap2")
# change dashboard dimensions and animation length
modelStudio(explainer_apartments,
new_apartments,
facet_dim = c(2, 3),
time = 800)
# add information about true labels
modelStudio(explainer_apartments,
new_apartments,
new_observation_y = new_apartments$m2.price)
# don't compute EDA plots
modelStudio(explainer_apartments,
eda = FALSE)
}
#:# ex3 xgboost model on 'HR' dataset
if (requireNamespace("xgboost", quietly=TRUE)) {
library("xgboost")
HR_matrix <- model.matrix(status == "fired" ~ . -1, HR)
# fit a model
xgb_matrix <- xgb.DMatrix(HR_matrix, label = HR$status == "fired")
params <- list(max_depth = 3, objective = "binary:logistic", eval_metric = "auc")
model_HR <- xgb.train(params, xgb_matrix, nrounds = 300)
# create an explainer for the model
explainer_HR <- explain(model_HR,
data = HR_matrix,
y = HR$status == "fired",
type = "classification",
label = "xgboost")
# pick observations
new_observation <- HR_matrix[1:2, , drop=FALSE]
rownames(new_observation) <- c("id1", "id2")
# make a studio for the model
modelStudio(explainer_HR,
new_observation)
}
# }
Run the code above in your browser using DataLab