Learn R Programming

candisc (version 1.0.0)

plot_discrim: Create a discriminant analysis decision plot using ggplot.

Description

[Experimental]

Discriminant analysis can be more easily understood from plots of the data variables showing how observations are classified. plot_discrim() uses the ideas behind effect plots: Visualize predicted values for two focal variables over a grid, with other variables in a model held fixed.

Usage

plot_discrim(
  model,
  vars,
  data,
  resolution = 100,
  contour = TRUE,
  contour.color = "black",
  point.size = 3,
  showgrid = c("tile", "point", "none"),
  ...,
  modes.means
)

Arguments

model

a discriminant analysis model object from MASS::lda() or MASS::qda()

vars

either a character vector of length 2 of the names of variables, or a formula of form V1 ~ V2 specifying y and x axis in the plot respectively.

data

data to use for visualization. Should contain all the data needed to use the model

resolution

number of points in x, y variables to use for visualizating the predicted class boundaries and regions.

contour

logical; should the plot display the boundaries of the classes by contours?

contour.color

color of the lines for the contour boundaries

point.size

size of the plot symbols use to show the data observations

showgrid

a character string; how to display predicted class regions: "tile" for ggplot2::geom_tile(), "point" for ggplot2::geom_point(), or "none" for no grid display.

...

further parameters passed to predict()

modes.means

levels to use for evaluating predictions using the variables *not specified in vars. If not specified, the function uses the means for quantitative variables, ...

Author

Original code by Oliver on SO https://stackoverflow.com/questions/63782598/quadratic-discriminant-analysis-qda-plot-in-r. Generalized by Michael Friendly

Details

In setting up this plot for ggplot2::ggplot(), this function maps color and shape of class-specific elements of the plot to the value of the class variable in the discriminant analysis. But it simply uses the ggplot defaults...

See Also

klaR::partimat()

Examples

Run this code
library(MASS)
library(ggplot2)
library(dplyr)
library(candisc)

iris.lda <- lda(Species ~ ., iris)
plot_discrim(iris.lda, Petal.Length ~ Petal.Width, data=iris, showgrid = "tile")

Run the code above in your browser using DataLab