Learn R Programming

SSLfmm (version 0.1.0)

compute_d2: Squared Discriminant Score for Two-Group LDA (Equal Covariance)

Description

Computes \(d^2\) where \(d = \beta_0 + y^\top \beta_1\) for two classes under the common-covariance (LDA) model: $$\beta_1 = \Sigma^{-1}(\mu_1 - \mu_2), \quad \beta_0 = \log(\pi_1/\pi_2) - \tfrac{1}{2}(\mu_1 + \mu_2)^\top \Sigma^{-1}(\mu_1 - \mu_2).$$

Usage

compute_d2(y, mu1, mu2, Sigma_inv, pi1, pi2)

Value

If y is a vector, a single numeric \(d^2\). If y is a matrix, a numeric vector of \(d^2\) values for each row.

Arguments

y

Numeric vector (length \(p\)) or numeric matrix with \(p\) columns; rows are observations.

mu1, mu2

Numeric vectors of length \(p\): class means.

Sigma_inv

\(p \times p\) numeric precision matrix (inverse covariance).

pi1, pi2

Positive scalars: class prior probabilities (need not sum to 1).

Examples

Run this code
set.seed(1)
mu1 <- c(0, 0); mu2 <- c(1, 1)
S <- matrix(c(1, .2, .2, 1), 2, 2)
Sigma_inv <- solve(S)
x <- c(0.5, -0.2)
compute_d2(x, mu1, mu2, Sigma_inv, pi1 = 0.6, pi2 = 0.4)

X <- matrix(rnorm(10 * 2), ncol = 2)
compute_d2(X, mu1, mu2, Sigma_inv, 0.5, 0.5)

Run the code above in your browser using DataLab