devtools::load_all("classifyr", export_all = FALSE)ℹ Loading classifyr
We use export_all = FALSE to simulate how R behaves when calling library.
devtools::load_all("classifyr", export_all = FALSE)ℹ Loading classifyr
We have a simple function to “train” a two-stage model:
model <- train_classifyr(iris, target = "Species")Training classifyr model
Bundling models into a single object
This object is a list with a custom class attribute tacked onto the front:
attr(model, which = "class")[1] "classifyr_model" "list"
Because we’ve defined a custom print method, though—print.classifyr_model()—we can control how the object is printed or displayed in settings where a string representation is needed:
modelClassifyr model
- Target: Species
- Features: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
We have defined a predict method as well, so we can use the model just like any other in R:
preds <- predict(model, iris)Making predictions for classifyr model
head(preds, n = 3)[1] 0.2071281 0.6798370 0.7549796
Finally, maybe we’d like to add other functionality. Here we’ve (1) created and (2) defined a new explain method:
explns <- explain(model, preds, iris)
head(explns, n = 5)Explaining model predictions
pred letter number message
1 0.2071281 K 22 Brought to you by the letter K and the number 22
2 0.6798370 V 71 Brought to you by the letter V and the number 71
3 0.7549796 Q 52 Brought to you by the letter Q and the number 52
4 0.5999480 X 31 Brought to you by the letter X and the number 31
5 0.4022563 U 13 Brought to you by the letter U and the number 13