File: naiveBayes.Rd

package info (click to toggle)
r-cran-e1071 1.7-3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,184 kB
  • sloc: cpp: 2,770; ansic: 1,022; sh: 4; makefile: 2
file content (107 lines) | stat: -rwxr-xr-x 4,003 bytes parent folder | download | duplicates (5)
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
\name{naiveBayes}
\alias{naiveBayes}
\alias{naiveBayes.default}
\alias{naiveBayes.formula}
\alias{print.naiveBayes}
\alias{predict.naiveBayes}
\title{Naive Bayes Classifier}
\description{
  Computes the conditional a-posterior probabilities of a categorical
  class variable given independent predictor variables using
  the Bayes rule.
}
\usage{
\S3method{naiveBayes}{formula}(formula, data, laplace = 0, ..., subset, na.action = na.pass)
\S3method{naiveBayes}{default}(x, y, laplace = 0, ...)


\S3method{predict}{naiveBayes}(object, newdata,
  type = c("class", "raw"), threshold = 0.001, eps = 0, ...)

}
\arguments{
  \item{x}{A numeric matrix, or a data frame of categorical and/or
    numeric variables.}
  \item{y}{Class vector.}
  \item{formula}{A formula of the form \code{class ~ x1 + x2 +
      \dots}. Interactions are not allowed.}
  \item{data}{Either a data frame of predictors (categorical and/or
    numeric) or a contingency table.}
  \item{laplace}{positive double controlling Laplace smoothing. The
    default (0) disables Laplace smoothing.}
  \item{\dots}{Currently not used.}
  \item{subset}{For data given in a data frame, an index vector
    specifying the cases to be used in the
    training sample.  (NOTE: If given, this argument must be
    named.)}
  \item{na.action}{A function to specify the action to be taken if \code{NA}s are
          found. The default action is not to count them for the
	  computation of the probability factors. An
          alternative is na.omit, which leads to rejection of cases
          with missing values on any required variable.  (NOTE: If
          given, this argument must be named.)}
  \item{object}{An object of class \code{"naiveBayes"}.}
  \item{newdata}{A dataframe with new predictors (with possibly fewer
    columns than the training data). Note that the column names of
    \code{newdata} are matched against the training data ones.}
  \item{type}{If \code{"raw"}, the conditional a-posterior
    probabilities for each class are returned, and the class with
    maximal probability else.}
  \item{threshold}{Value replacing cells with probabilities within \code{eps} range.}
  \item{eps}{double for specifying an epsilon-range to apply laplace
    smoothing (to replace zero or close-zero probabilities by \code{theshold}.)}
}
\value{
  An object of class \code{"naiveBayes"} including components:

  \item{apriori}{Class distribution for the dependent variable.}
  \item{tables}{A list of tables, one for each predictor variable. For each
    categorical variable a table giving, for each attribute level, the conditional
    probabilities given the target class. For each numeric variable, a
    table giving, for each target class, mean and standard deviation of
    the (sub-)variable.}

}
\details{
  The standard naive Bayes classifier (at least this implementation)
  assumes independence of the predictor
  variables, and Gaussian distribution (given the target class) of
  metric predictors.
  For attributes with missing values, the
  corresponding table entries are omitted for prediction.
  }
\author{David Meyer \email{David.Meyer@R-project.org}. Laplace smoothing
enhancement by Jinghao Xue.}
\examples{
## Categorical data only:
data(HouseVotes84, package = "mlbench")
model <- naiveBayes(Class ~ ., data = HouseVotes84)
predict(model, HouseVotes84[1:10,])
predict(model, HouseVotes84[1:10,], type = "raw")

pred <- predict(model, HouseVotes84)
table(pred, HouseVotes84$Class)

## using laplace smoothing:
model <- naiveBayes(Class ~ ., data = HouseVotes84, laplace = 3)
pred <- predict(model, HouseVotes84[,-1])
table(pred, HouseVotes84$Class)


## Example of using a contingency table:
data(Titanic)
m <- naiveBayes(Survived ~ ., data = Titanic)
m
predict(m, as.data.frame(Titanic))

## Example with metric predictors:
data(iris)
m <- naiveBayes(Species ~ ., data = iris)
## alternatively:
m <- naiveBayes(iris[,-5], iris[,5])
m
table(predict(m, iris), iris[,5])
}
\keyword{classif}
\keyword{category}