File: confusionMatrix.Rd

package info (click to toggle)
r-cran-caret 7.0-1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,036 kB
  • sloc: ansic: 210; sh: 10; makefile: 2
file content (184 lines) | stat: -rw-r--r-- 6,268 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/confusionMatrix.R
\name{confusionMatrix}
\alias{confusionMatrix}
\alias{confusionMatrix.table}
\alias{confusionMatrix.default}
\alias{confusionMatrix.matrix}
\title{Create a confusion matrix}
\usage{
confusionMatrix(data, ...)

\method{confusionMatrix}{default}(
  data,
  reference,
  positive = NULL,
  dnn = c("Prediction", "Reference"),
  prevalence = NULL,
  mode = "sens_spec",
  ...
)

\method{confusionMatrix}{matrix}(
  data,
  positive = NULL,
  prevalence = NULL,
  mode = "sens_spec",
  ...
)

\method{confusionMatrix}{table}(
  data,
  positive = NULL,
  prevalence = NULL,
  mode = "sens_spec",
  ...
)
}
\arguments{
\item{data}{a factor of predicted classes (for the default method) or an
object of class \code{\link[base]{table}}.}

\item{\dots}{options to be passed to \code{table}. NOTE: do not include
\code{dnn} here}

\item{reference}{a factor of classes to be used as the true results}

\item{positive}{an optional character string for the factor level that
corresponds to a "positive" result (if that makes sense for your data). If
there are only two factor levels, the first level will be used as the
"positive" result. When \code{mode = "prec_recall"}, \code{positive} is the
same value used for \code{relevant} for functions \code{\link{precision}},
\code{\link{recall}}, and \code{\link{F_meas.table}}.}

\item{dnn}{a character vector of dimnames for the table}

\item{prevalence}{a numeric value or matrix for the rate of the "positive"
class of the data. When \code{data} has two levels, \code{prevalence} should
be a single numeric value. Otherwise, it should be a vector of numeric
values with elements for each class. The vector should have names
corresponding to the classes.}

\item{mode}{a single character string either "sens_spec", "prec_recall", or
"everything"}
}
\value{
a list with elements \item{table}{the results of \code{table} on
\code{data} and \code{reference}} \item{positive}{the positive result level}
\item{overall}{a numeric vector with overall accuracy and Kappa statistic
values} \item{byClass}{the sensitivity, specificity, positive predictive
value, negative predictive value, precision, recall, F1, prevalence,
detection rate, detection prevalence and balanced accuracy for each class.
For two class systems, this is calculated once using the \code{positive}
argument}
}
\description{
Calculates a cross-tabulation of observed and predicted classes with
associated statistics.
}
\details{
The functions requires that the factors have exactly the same levels.

For two class problems, the sensitivity, specificity, positive predictive
value and negative predictive value is calculated using the \code{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

\tabular{rcc}{ \tab Reference \tab \cr Predicted \tab Event \tab No Event
\cr Event \tab A \tab B \cr No Event \tab C \tab D \cr }

The formulas used here are: \deqn{Sensitivity = A/(A+C)} \deqn{Specificity =
D/(B+D)} \deqn{Prevalence = (A+C)/(A+B+C+D)} \deqn{PPV = (sensitivity *
prevalence)/((sensitivity*prevalence) + ((1-specificity)*(1-prevalence)))}
\deqn{NPV = (specificity * (1-prevalence))/(((1-sensitivity)*prevalence) +
((specificity)*(1-prevalence)))} \deqn{Detection Rate = A/(A+B+C+D)}
\deqn{Detection Prevalence = (A+B)/(A+B+C+D)} \deqn{Balanced Accuracy =
(sensitivity+specificity)/2}

\deqn{Precision = A/(A+B)} \deqn{Recall = A/(A+C)} \deqn{F1 =
(1+beta^2)*precision*recall/((beta^2 * precision)+recall)}

where \code{beta = 1} for this function.

See the references for discussions 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
\code{\link[stats]{mcnemar.test}} (which can produce \code{NA} values with
sparse tables).

The overall accuracy rate is computed along with a 95 percent confidence
interval for this rate (using \code{\link[stats]{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.
}
\note{
If the reference and data factors have the same levels, but in the
incorrect order, the function will reorder them to the order of the data and
issue a warning.
}
\examples{

###################
## 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

confusionMatrix(iris$Species, sample(iris$Species))

newPrior <- c(.05, .8, .15)
names(newPrior) <- levels(iris$Species)

confusionMatrix(iris$Species, sample(iris$Species))


}
\references{
Kuhn, M. (2008), ``Building predictive models in R using the
caret package, '' \emph{Journal of Statistical Software},
(\doi{10.18637/jss.v028.i05}).

Altman, D.G., Bland, J.M. (1994) ``Diagnostic tests 1: sensitivity and
specificity,'' \emph{British Medical Journal}, vol 308, 1552.

Altman, D.G., Bland, J.M. (1994) ``Diagnostic tests 2: predictive values,''
\emph{British Medical Journal}, vol 309, 102.

Velez, D.R., et. al. (2008) ``A balanced accuracy function for epistasis
modeling in imbalanced datasets using multifactor dimensionality
reduction.,'' \emph{Genetic Epidemiology}, vol 4, 306.
}
\seealso{
\code{\link{as.table.confusionMatrix}},
\code{\link{as.matrix.confusionMatrix}}, \code{\link{sensitivity}},
\code{\link{specificity}}, \code{\link{posPredValue}},
\code{\link{negPredValue}}, \code{\link{print.confusionMatrix}},
\code{\link[stats]{binom.test}}
}
\author{
Max Kuhn
}
\keyword{utilities}