Unified (formula-based) interface version of the quadratic discriminant
analysis algorithm provided by MASS::qda()
.
Usage
mlQda(train, ...)
ml_qda(train, ...)
# S3 method for formula
mlQda(formula, data, ..., subset, na.action)
# S3 method for default
mlQda(train, response, ...)
# S3 method for mlQda
predict(
object,
newdata,
type = c("class", "membership", "both"),
prior = object$prior,
method = c("plug-in", "predictive", "debiased", "looCV", "cv"),
...
)
Arguments
- train
a matrix or data frame with predictors.
- ...
further arguments passed to
MASS::qda()
or itspredict()
method (see the corresponding help page).- formula
a formula with left term being the factor variable to predict and the right term with the list of independent, predictive variables, separated with a plus sign. If the data frame provided contains only the dependent and independent variables, one can use the
class ~ .
short version (that one is strongly encouraged). Variables with minus sign are eliminated. Calculations on variables are possible according to usual formula convention (possibly protected by usingI()
).- data
a data.frame to use as a training set.
- subset
index vector with the cases to define the training set in use (this argument must be named, if provided).
- na.action
function to specify the action to be taken if
NA
s are found. Forml_qda()
na.fail
is used by default. The calculation is stopped if there is anyNA
in the data. Another option isna.omit
, where cases with missing values on any required variable are dropped (this argument must be named, if provided). For thepredict()
method, the default, and most suitable option, isna.exclude
. In that case, rows withNA
s innewdata=
are excluded from prediction, but reinjected in the final results so that the number of items is still the same (and in the same order asnewdata=
).- response
a vector of factor for the classification.
- object
an mlQda object
- newdata
a new dataset with same conformation as the training set (same variables, except may by the class for classification or dependent variable for regression). Usually a test set, or a new dataset to be predicted.
- type
the type of prediction to return.
"class"
by default, the predicted classes. Other options are"membership"
the membership (a number between 0 and 1) to the different classes, or"both"
to return classes and memberships.- prior
the prior probabilities of class membership. By default, the prior are obtained from the object and, if they where not changed, correspond to the proportions observed in the training set.
- method
"plug-in"
,"predictive"
,"debiased"
,"looCV"
, or"cv"
."plug-in"
(default) the usual unbiased parameter estimates are used. With"predictive"
, the parameters are integrated out using a vague prior. With"debiased"
, an unbiased estimator of the log posterior probabilities is used. With"looCV"
, the leave-one-out cross-validation fits to the original data set are computed and returned. With"cv"
, cross-validation is used instead. If you specifymethod = "cv"
thencvpredict()
is used and you cannot providenewdata=
in that case.
Value
ml_qda()
/mlQda()
creates an mlQda, mlearning object
containing the classifier and a lot of additional metadata used by the
functions and methods you can apply to it like predict()
or
cvpredict()
. In case you want to program new functions or extract
specific components, inspect the "unclassed" object using unclass()
.
See also
mlearning()
, cvpredict()
, confusion()
, also MASS::qda()
that
actually does the classification.
Examples
# Prepare data: split into training set (2/3) and test set (1/3)
data("iris", package = "datasets")
train <- c(1:34, 51:83, 101:133)
iris_train <- iris[train, ]
iris_test <- iris[-train, ]
# One case with missing data in train set, and another case in test set
iris_train[1, 1] <- NA
iris_test[25, 2] <- NA
iris_qda <- ml_qda(data = iris_train, Species ~ .)
summary(iris_qda)
#> A mlearning object of class mlQda (quadratic discriminant analysis):
#> Initial call: mlQda.formula(formula = Species ~ ., data = iris_train)
#> Call:
#> qda(sapply(train, as.numeric), grouping = response, .args. = ..1)
#>
#> Prior probabilities of groups:
#> setosa versicolor virginica
#> 0.3333333 0.3333333 0.3333333
#>
#> Group means:
#> Sepal.Length Sepal.Width Petal.Length Petal.Width
#> setosa 5.048485 3.478788 1.478788 0.2454545
#> versicolor 6.027273 2.763636 4.284848 1.3303030
#> virginica 6.642424 2.951515 5.642424 2.0090909
confusion(iris_qda)
#> 99 items classified with 98 true positives (error rate = 1%)
#> Predicted
#> Actual 01 02 03 (sum) (FNR%)
#> 01 setosa 33 0 0 33 0
#> 02 versicolor 0 32 1 33 3
#> 03 virginica 0 0 33 33 0
#> (sum) 33 32 34 99 1
confusion(predict(iris_qda, newdata = iris_test), iris_test$Species)
#> 50 items classified with 48 true positives (error rate = 4%)
#> Predicted
#> Actual 01 02 03 04 (sum) (FNR%)
#> 01 setosa 16 0 0 0 16 0
#> 02 NA 0 0 0 0 0
#> 03 versicolor 0 1 15 1 17 12
#> 04 virginica 0 0 0 17 17 0
#> (sum) 16 1 15 18 50 4
# Another dataset (binary predictor... not optimal for qda, just for test)
data("HouseVotes84", package = "mlbench")
house_qda <- ml_qda(data = HouseVotes84, Class ~ ., na.action = na.omit)
#> Warning: force conversion from factor to numeric; may be not optimal or suitable
summary(house_qda)
#> A mlearning object of class mlQda (quadratic discriminant analysis):
#> Initial call: mlQda.formula(formula = Class ~ ., data = HouseVotes84, na.action = na.omit)
#> Call:
#> qda(sapply(train, as.numeric), grouping = response, .args. = ..1)
#>
#> Prior probabilities of groups:
#> democrat republican
#> 0.5344828 0.4655172
#>
#> Group means:
#> V1 V2 V3 V4 V5 V6 V7
#> democrat 1.588710 1.451613 1.854839 1.048387 1.201613 1.443548 1.766129
#> republican 1.212963 1.472222 1.157407 1.990741 1.953704 1.870370 1.268519
#> V8 V9 V10 V11 V12 V13 V14
#> democrat 1.830645 1.790323 1.532258 1.508065 1.129032 1.290323 1.346774
#> republican 1.148148 1.138889 1.574074 1.157407 1.851852 1.842593 1.981481
#> V15 V16
#> democrat 1.596774 1.943548
#> republican 1.111111 1.666667