decision_boundary: Plot decision boundary for a classifier with 2 predictors

Description Usage Arguments Details Value Examples

View source: R/decision-boundary.R

Description

Plot decision boundary for a classifier with 2 predictors

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
decision_boundary(
  fitted_model,
  y,
  x1,
  x2,
  predict_type = NULL,
  predict_fun = NULL,
  resolution = 100,
  showgrid = TRUE,
  ...
)

Arguments

fitted_model

a fitted classifier object, for example the result of glm(...)

y

the original observed classes

x1

original first predictor

x2

original second predictor

predict_type

default is NULL, many predict methods accept a type argument, if known, this can help to produce classifications

predict_fun

if your classifier is different than one of the standard methods (see below) you can pass a custom function which accepts a single argument g (a new data dataframe with columns) and produces a vector of classes

resolution

number of points along the two dimensions to produce classes

showgrid

do we plot a grid of predictions

...

additional arguments passed to plot()

Details

This function should work with only the mandatory arguments if the classifier is produced via glm(), MASS::lda(), MASS::qda() or from either multinom() or nnet() in the {nnet} package. Other classifiers likely require additional instructions. If the predict method produces integer or factor labels as output, it should just "work".

Value

If assigned to an object, returns the matrix of predicted classes

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
x1 = runif(100,-1,1)
x2 = runif(100,-1,1)
y = rbinom(100, 1, 1/(1+exp(-2*x1-3*x2)))

logit = glm(y~x1+x2, family="binomial")
disc = MASS::lda(y~x1+x2)

par(mfrow=c(1,2))
decision_boundary(logit, y, x1, x2)
decision_boundary(disc, y, x1, x2)
par(mfrow=c(1,1))

dajmcdon/ubc-stat406-labs documentation built on Aug. 18, 2020, 1:23 p.m.