The “naive_bayes” Function in R

  • Package: naivebayes

  • Purpose: Fit a Naive Bayes classifier to data.

  • General class: Model

  • Required argument(s):

    • x: Predictor variables (data frame or matrix).

    • y: Response variable (vector).

  • Notable optional arguments:

    • laplace: Laplace smoothing parameter (default is 0).

    • usekernel: Boolean indicating if kernel density estimation should be used for continuous variables (default is FALSE).

  • Example:

  • # Load the required library
    library(naivebayes)

    # Example dataset
    data(iris)

    # Fit a Naive Bayes model
    model <- naive_bayes(Species ~ ., data = iris, laplace = 1)

    # Summary of the model
    print(model)

    # Predict on new data (although in this example, the data is not new)
    predictions <- predict(model, iris)

    # Print predictions
    head(predictions)

  • In this example, the naive_bayes function from the naivebayes package is used to fit a Naive Bayes classifier to the iris dataset. The response variable is Species, and all other columns are used as predictor variables. Laplace smoothing is applied with a parameter value of 1. The model is then used to make predictions on the same dataset.

Previous
Previous

The “predict” Function in R

Next
Next

The “layer_max_pooling_2d” Function in R