Learn R Programming

SSLfmm (version 0.1.0)

bayesclassifier: Bayes' Rule Classifier

Description

Classifier specified by Bayes' rule. Assigns \(\arg\max_i \{ \log(\pi_i) + \log \mathcal{N} (y_j \mid \mu_i, \Sigma_i) \}\).

Usage

bayesclassifier(dat, p, g, pi = NULL, mu = NULL, sigma = NULL, paralist = NULL)

Value

An integer vector of length \(n\) with predicted class labels in \(1:g\).

Arguments

dat

An \(n \times p\) numeric matrix (or a length-\(p\) numeric vector, treated as \(1 \times p\)).

p

Integer; dimension of the observation vector.

g

Integer; number of Gaussian components/classes.

pi

Numeric length-\(g\) vector of mixing proportions (must sum to 1).

mu

Either:

  • A \(p \times g\) numeric matrix (column \(i\) is \(\mu_i\)), or

  • A length-\(g\) list of length-\(p\) numeric vectors.

sigma

Either:

  • A \(p \times p\) covariance matrix (shared), or

  • A length-\(g\) list of \(p \times p\) covariance matrices, or

  • A \(p \times p \times g\) numeric array.

paralist

Optional list with elements pi, mu, sigma (overrides explicit args if provided and non-NULL).

Examples

Run this code
# Minimal example with list-style mu and sigma:
set.seed(1)
p <- 2; g <- 2
pi <- c(0.5, 0.5)
mu <- list(`1`=c(0.96, 0.02), `2`=c(-1.02, -0.03))
sigma <- list(
  matrix(c(0.9417379,0.5447264, 0.5447264,0.9811853), 2, 2, byrow=TRUE),
  matrix(c(0.9984812,0.3314474, 0.3314474,1.1316865), 2, 2, byrow=TRUE)
)
X <- mvtnorm::rmvnorm(5, mean = mu[[1]], sigma = sigma[[1]])
bayesclassifier(X, p=p, g=g, pi=pi, mu=mu, sigma=sigma)

Run the code above in your browser using DataLab