knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

discrim

CRAN status Codecov test coverage R-CMD-check Lifecycle: experimental

discrim contains simple bindings to enable the parsnip package to fit various discriminant analysis models, such as

Installation

You can install the released version of discrim from CRAN with:

install.packages("discrim")

And the development version from GitHub with:

# install.packages("pak")
pak::pak("tidymodels/discrim")

Available Engines

The discrim package provides engines for the models in the following table.

library(parsnip)

parsnip_models <- get_from_env("models") %>%
  setNames(., .) %>%
  purrr::map_dfr(get_from_env, .id = "model")

library(discrim)

discrim_models <- get_from_env("models") %>%
  setNames(., .) %>%
  purrr::map_dfr(get_from_env, .id = "model")

dplyr::anti_join(
  discrim_models, parsnip_models, 
  by = c("model", "engine", "mode")
) %>%
  knitr::kable()

Example

Here is a simple model using a simulated two-class data set contained in the package:

#| fig-alt: "Scatter chart. X1 along the x-axis, X2 along the y-axis. points are scattered, with a trend between X1 and X2. Most of the middle points are colored and labeled Class2, with the remaining points labeled Class1. Two connected straight lines, doing its best to separate the two classes."
library(discrim)

parabolic_grid <-
  expand.grid(X1 = seq(-5, 5, length = 100),
              X2 = seq(-5, 5, length = 100))

fda_mod <-
  discrim_flexible(num_terms = 3) %>%
  # increase `num_terms` to find smoother boundaries
  set_engine("earth") %>%
  fit(class ~ ., data = parabolic)

parabolic_grid$fda <-
  predict(fda_mod, parabolic_grid, type = "prob")$.pred_Class1

library(ggplot2)
ggplot(parabolic, aes(x = X1, y = X2)) +
  geom_point(aes(col = class), alpha = .5) +
  geom_contour(data = parabolic_grid, aes(z = fda), col = "black", breaks = .5) +
  theme_bw() +
  theme(legend.position = "top") +
  coord_equal()

Contributing

This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



tidymodels/discrim documentation built on April 15, 2024, 9:19 p.m.