lda.Rd
Linear discriminant analysis.
lda(x, ...) # S3 method for formula lda(formula, data, ..., subset, na.action) # S3 method for default lda(x, grouping, prior = proportions, tol = 1.0e-4, method, CV = FALSE, nu, ...) # S3 method for data.frame lda(x, ...) # S3 method for matrix lda(x, grouping, ..., subset, na.action)
formula | A formula of the form |
---|---|
data | Data frame from which variables specified in |
x | (required if no formula is given as the principal argument.) a matrix or data frame or Matrix containing the explanatory variables. |
grouping | (required if no formula principal argument is given.) a factor specifying the class for each observation. |
prior | the prior probabilities of class membership. If unspecified, the class proportions for the training set are used. If present, the probabilities should be specified in the order of the factor levels. |
tol | A tolerance to decide if a matrix is singular; it will reject variables
and linear combinations of unit-variance variables whose variance is
less than |
subset | An index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.) |
na.action | A function to specify the action to be taken if |
method |
|
CV | If true, returns results (classes and posterior probabilities) for leave-one-out cross-validation. Note that if the prior is estimated, the proportions in the whole dataset are used. |
nu | degrees of freedom for |
... | arguments passed to or from other methods. |
If CV = TRUE
the return value is a list with components
class
, the MAP classification (a factor), and posterior
,
posterior probabilities for the classes.
Otherwise it is an object of class "lda"
containing the
following components:
the prior probabilities used.
the group means.
a matrix which transforms observations to discriminant functions, normalized so that within groups covariance matrix is spherical.
the singular values, which give the ratio of the between- and within-group standard deviations on the linear discriminant variables. Their squares are the canonical F-statistics.
The number of observations used.
The (matched) function call.
The function
tries hard to detect if the within-class covariance matrix is
singular. If any variable has within-group variance less than
tol^2
it will stop and report the variable as constant. This
could result from poor scaling of the problem, but is more
likely to result from constant variables.
Specifying the prior
will affect the classification unless
over-ridden in predict.lda
. Unlike in most statistical packages, it
will also affect the rotation of the linear discriminants within their
space, as a weighted between-groups covariance matrix is used. Thus
the first few linear discriminants emphasize the differences between
groups with the weights given by the prior, which may differ from
their prevalence in the dataset.
If one or more groups is missing in the supplied data, they are dropped with a warning, but the classifications produced are with respect to the original set of levels.
This function may be called giving either a formula and
optional data frame, or a matrix and grouping factor as the first
two arguments. All other arguments are optional, but subset=
and
na.action=
, if required, must be fully named.
If a formula is given as the principal argument the object may be
modified using update()
in the usual way.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth edition. Springer.
Ripley, B. D. (1996) Pattern Recognition and Neural Networks. Cambridge University Press.
Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]), Sp = rep(c("s","c","v"), rep(50,3))) train <- sample(1:150, 75) table(Iris$Sp[train])#> #> c s v #> 22 31 22## your answer may differ ## c s v ## 22 23 30 z <- lda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train) predict(z, Iris[-train, ])$class#> [1] s s s s s s s s s s s s s s s s s s s c c c c c c c c c c c c v c c c c v c #> [39] c c c c c c c c c v v v v v v v v v v c v v v v v v v c v v v v v v v v v #> Levels: c s v## [1] s s s s s s s s s s s s s s s s s s s s s s s s s s s c c c ## [31] c c c c c c c v c c c c v c c c c c c c c c c c c v v v v v ## [61] v v v v v v v v v v v v v v v (z1 <- update(z, . ~ . - Petal.W.))#> Call: #> lda(Sp ~ Sepal.L. + Sepal.W. + Petal.L., data = Iris, prior = c(1, #> 1, 1)/3, subset = train) #> #> Prior probabilities of groups: #> c s v #> 0.3333333 0.3333333 0.3333333 #> #> Group means: #> Sepal.L. Sepal.W. Petal.L. #> c 5.968182 2.763636 4.254545 #> s 4.974194 3.448387 1.458065 #> v 6.668182 2.950000 5.600000 #> #> Coefficients of linear discriminants: #> LD1 LD2 #> Sepal.L. 1.7449211 -1.254634 #> Sepal.W. 0.0379671 3.754167 #> Petal.L. -3.7525160 1.032709 #> #> Proportion of trace: #> LD1 LD2 #> 0.9905 0.0095