0

I am trying to see the underlying algorithm (code) in the naive_bayes function (naivebayes package), the predict.naive_bayes function (naivebayes package) and the naiveBayes function (e1071 package). I am getting things like what is below but I cannot see the algorithm itself. naiveBayes function (x, ...) UseMethod("naiveBayes")

 naive_bayes
function (x, ...) 
{
    UseMethod("naive_bayes")
}
<environment: namespace:naivebayes>

predict.naive_bayes
Error: object 'predict.naive_bayes' not found
> predict
function (object, ...) 
UseMethod("predict")
 <bytecode: 0x000000000ad686d0>
 <environment: namespace:stats>
 naivebayes::naive_bayes
function (x, ...) 
{
    UseMethod("naive_bayes")
}
<environment: namespace:naivebayes>
> naivebayes:::naive_bayes
function (x, ...) 
{
    UseMethod("naive_bayes")
}
<environment: namespace:naivebayes>
>




I also tried this
> download.packages(naivebayes)
Error in dir.exists(destdir) : 
  argument "destdir" is missing, with no default
> download.packages(naivebayes, destdir = ".",type = "source")
Error in unique(pkgs) : object 'naivebayes' not found
> download.packages(e1071, destdir = ".",type = "source")
Error in unique(pkgs) : object 'e1071' not found
>
matt
  • 131
  • 1
  • 8

1 Answers1

0

For me, the far easiest way to look up code is CRAN's read-only mirror on GitHub.

predict.naive_bayes is here.

predict.naive_bayes <- function(object, newdata = NULL, type = c("class", "prob"),
                                threshold = 0.001, ...) {

  if (is.null(newdata)) newdata <- object$data$x
  else newdata <- as.data.frame(newdata)
  na <- sapply(newdata, anyNA)
  type <- match.arg(type)
  lev <- object$levels
  n_lev <- length(lev)
  n_obs <- dim(newdata)[1L]
  usekernel <- object$usekernel
  prior <- as.double(object$prior)
  tables <- object$tables
  features <- names(newdata)[names(newdata) %in% names(tables)]
  log_sum <- 0
...

e1071's naiveBayes is here... You get the gist (pun intended).

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197