decision_boundary: Add decision boundary to a scatterplot.

View source: R/decision_boundary.R

decision_boundaryR Documentation

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, ...)

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

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.

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.

Note

Based on a function written by Michael Hahsler; see https://michael.hahsler.net/SMU/EMIS7332/R/viz_classifier.html.

Examples

## Not run: 
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")

## End(Not run)

bgreenwell/treemisc documentation built on Oct. 26, 2022, 12:56 a.m.