Predict

As xspliner final model is GLM, predict method is just wrapper of stats::predict.glm function. Let's see it on the below example:

library(xspliner)
library(randomForest)
library(magrittr)

rf_iris <- randomForest(Petal.Width ~  Sepal.Length + Petal.Length + Species, data = iris)
model_xs <- xspline(Petal.Width ~ 
  Sepal.Length + 
  xs(Petal.Length, effect = list(grid.resolution = 100), transition = list(bs = "cr")) + 
  xf(Species, transition = list(stat = "loglikelihood", value = -300)),
  model = rf_iris)
newdata <- data.frame(
  Sepal.Length = 10, 
  Petal.Length = 2, 
  Species = factor("virginica", levels = levels(iris$Species)))
predict(model_xs, newdata = newdata)

Print

Print method works similarly to the summary. In case of passing just the model, standard print.glm is used.

print(model_xs)

Predictor based print

Summary method allows you to check details about transformation of specific variable.

Standard usage print(xspliner_object, variable_name)

Quantitative variable

When predictor is the quantitative variable its transition is based on GAM model. For this case print uses standard print.gam method.

print(model_xs, "Petal.Length")

Qualitative variable

In case of qualitative predictor, standard print.factorMerger method is used.

print(model_xs, "Species")

Plot

You can see all details in graphics

Transition

Transition method allows you to extract objects used during building transition of variables. There are three possible object types that can be extracted.

Extracting effect

Each transition is built on top of the black box response data. For example the default response for quantitative variables is PDP - for qualitative ones ICE.

In order to extract the effect use transition method with type parameter equals to data

transition(model_xs, predictor = "Petal.Length", type = "data") %>% 
  head

transition(model_xs, predictor = "Species", type = "data") %>% 
  head

Extracting transition model

After we built transition basing on continuity of variable specific model is created. In case of quantitative predictor we build GAM model in order to get spline approximation of effect. In case of qualitative predictor we build factorMerger object and get optimal factor division on that.

To extract the model, use transition method with type = "base":

transition(model_xs, predictor = "Petal.Length", type = "base")

transition(model_xs, predictor = "Species", type = "base")

Extracting transition function

The final result of building transition is transformation function, that is used in the final GLM model estimation.

To extract the function just use transition method with type = "function".

petal_length_xs <- transition(model_xs, predictor = "Petal.Length", type = "function")
x <- seq(1, 7, length.out = 50)
plot(x, petal_length_xs(x))
species_xf <- transition(model_xs, predictor = "Species", type = "function")
species_xf(c("setosa", "versicolor", "virginica"))

Summary

Summary method allows you to check the basic model details. See below what possibilities the method to xspliner model offers.

GLM summary

Standard summary method is just wrapper for summary::glm. In order to use this just type:

summary(model_xs)

Predictor based summary

Summary method allows you to check details about transformation of specific variable.

Standard usage summary(xspliner_object, variable_name)

Quantitative variable

When predictor is quantitative variable its transition is based on GAM model. For this case summary displays summary of that model.

summary(model_xs, "Petal.Length")

Qualitative variable

In case of qualitative predictor, the method displays data.frame storing information how factors were merged during the transition.

summary(model_xs, "Species")

Surrogate vs Black Box comparison

Providing model parameter instead of predictor, the summary displays a few statistics that compares original model with surrogate one. All statistics definitions are included in summary.xspline documentation.

Here we show one example for classification model.

For this example we use ISLR::Default data and build svm model as black box. The model aims to predict default variable, indicating whether the customer defaulted on their debt.

library(xspliner)
library(e1071)
set.seed(1)
data <- ISLR::Default
default.svm <- svm(default ~ ., data = data, probability = TRUE)
default.xs <- xspline(default ~ student + xs(balance) + xs(income), model = default.svm)

In order to check the summary, we need to specify prediction functions for each model. In this case predictions are probabilities of success:

prob_svm <- function(object, newdata) attr(predict(object, newdata = newdata, probability = TRUE), "probabilities")[, 2]
prob_xs <- function(object, newdata) predict(object, newdata = newdata, type = "response")

Almost each summary statistic compares models basing on some data.

In this case we're going to compare models on training data providing:

summary(default.xs, model = default.svm, newdata = data, prediction_funs = list(prob_xs, prob_svm))

Another set of statistics is generated for prediction functions that return response levels.

response_svm <- function(object, newdata) predict(object, newdata = newdata)
response_xs <- function(object, newdata) {
  y_levels <- levels(newdata[[environment(object)$response]])
  factor(y_levels[(predict.glm(object, newdata = newdata, type = "link") > 0) + 1], levels = y_levels)
}

And similarly to previous example:

summary(default.xs, model = default.svm, newdata = data, prediction_funs = list(response_xs, response_svm))


ModelOriented/xspliner documentation built on Oct. 5, 2019, 3:42 p.m.