Learn R Programming

treemisc (version 0.0.1)

decision_boundary: Add decision boundary to a scatterplot.

Description

Adds the decision boundary from a classification model (binary or multiclass) to an existing scatterplot.

Usage

decision_boundary(model, train, y, x1, x2, pfun, grid.resolution = 100, ...)

# S3 method for default decision_boundary( model, train, y, x1, x2, pfun = NULL, grid.resolution = 100, ... )

Value

No return value, only called for side effects; in this case, a contour displaying the decision boundary of a classifier is added to an existing scatterplot.

Arguments

model

The associated model object.

train

Data frame of training observations.

y

Character string giving the name of the outcome variable in train.

x1

Character string giving the name of the predictor in train that corresponds to the x-axis.

x2

Character string giving the name of the predictor in train that corresponds to the y-axis.

pfun

Optional prediction wrapper that returns a vector of predicted class labels. It must have exactly two arguments: object and newdata.

grid.resolution

Integer specifying the resolution of the contour plot. Default is 100.

...

Additional optional arguments to be passed on to contour.

Examples

Run this code
if (FALSE) {
library(mlbench)
library(rpart)
library(treemisc)

# Generate training data from the twonorm benchmark problem
set.seed(1050)  # for reproducibility
trn <- as.data.frame(mlbench.twonorm(500, d = 2))

# Fit a default classification tree
tree <- rpart(classes ~ ., data = trn)

# Scatterplot of training data
palette("Okabe-Ito")
plot(x.2 ~ x.1, data = trn, col = as.integer(trn$classes) + 1,
     xlab = expression(x[1]), ylab = expression(x[2]))
palette("default")

# Add a decision boundary
decision_boundary(tree, train = trn, y = "y", x1 = "x.1", x2 = "x.2")
}

Run the code above in your browser using DataLab