
Last chance! 50% off unlimited learning
Sale ends in
confusionMatrix(data, ...)## S3 method for class 'default':
confusionMatrix(data, reference, positive = NULL,
dnn = c("Prediction", "Reference"),
prevalence = NULL, ...)
## S3 method for class 'table':
confusionMatrix(data, positive = NULL, prevalence = NULL, ...)
table
on data
and reference
positive
argumentFor two class problems, the sensitivity, specificity, positive
predictive value and negative predictive value is calculated using the
positive
argument. Also, the prevalence of the "event" is computed from the
data (unless passed in as an argument), the detection rate (the rate of true events also
predicted to be events) and the detection prevalence (the prevalence of predicted events).
Suppose a 2x2 table with notation
The formulas used here are:
See the references for discusions of the first five formulas.
For more than two classes, these results are calculated comparing each factor level to the remaining levels (i.e. a "one versus all" approach).
The overall accuracy and unweighted Kappa statistic are calculated. A p-value from McNemar's test is also computed using mcnemar.test
(which can produce NA
values with sparse tables).
The overall accuracy rate is computed along with a 95 percent confidence interval for this rate (using binom.test
) and a one-sided test to see if the accuracy is better than the "no information rate," which is taken to be the largest class percentage in the data.
Altman, D.G., Bland, J.M. (1994) ``Diagnostic tests 1: sensitivity and specificity,'' British Medical Journal, vol 308, 1552.
Altman, D.G., Bland, J.M. (1994) ``Diagnostic tests 2: predictive values,'' British Medical Journal, vol 309, 102.
as.table.confusionMatrix
, as.matrix.confusionMatrix
,
sensitivity
, specificity
, posPredValue
, negPredValue
,
print.confusionMatrix
, binom.test
###################
## 2 class example
lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
levels = rev(lvs))
pred <- factor(
c(
rep(lvs, times = c(54, 32)),
rep(lvs, times = c(27, 231))),
levels = rev(lvs))
xtab <- table(pred, truth)
confusionMatrix(xtab)
confusionMatrix(pred, truth)
confusionMatrix(xtab, prevalence = 0.25)
###################
## 3 class example
library(MASS)
fit <- lda(Species ~ ., data = iris)
model <- predict(fit)$class
irisTabs <- table(model, iris$Species)
confusionMatrix(irisTabs)
confusionMatrix(model, iris$Species)
newPrior <- c(.05, .8, .15)
names(newPrior) <- levels(iris$Species)
confusionMatrix(irisTabs, prevalence = newPrior)
## Need names for prevalence
confusionMatrix(irisTabs, prevalence = c(.05, .8, .15))
Run the code above in your browser using DataLab