classifyNB: Classify Naive Bayes

View source: R/classifyNB.R

classifyNBR Documentation

Classify Naive Bayes

Description

Classify test data using output from trainNB

Usage

classifyNB(est, test_matrix, test)

Arguments

est

Output object from trainNB

test_matrix

A quanteda document-feature matrix with the same number of rows as test. Rows must match.

test

The data frame containing the text data from which test_matrix was created.

Value

A data frame equal to test with four added columns:

max_posterior

Maximum posterior probability of any class for this document. This matches the posterior probability of the classification assigned in the max_match column.

max_ratios

Maximum ratio of posterior to prior probability of any class for this document. This matches the ratio of the classification assigned in the ratio_match column.

max_match

Class associated with maximum posterior probability. Contains predicted class for each observation in test.

ratio_match

Class associated with maximum ratio of posterior to prior probability. Contains predicted class for each observation in test.

Author(s)

Matt W. Loftis

Examples

  ## Load data and create document-feature matrices
  train_corpus <- quanteda::corpus(x = training_agendas$text)
  train_matrix <- quanteda::dfm(train_corpus,
                                language = "danish",
                                stem = TRUE,
                                removeNumbers = FALSE)

  test.corpus <- quanteda::corpus(x = test_agendas$text)
  test_matrix <- quanteda::dfm(test.corpus,
                               language = "danish",
                               stem = TRUE,
                               removeNumbers = FALSE)

  ## Convert matrix of frequencies to matrix of indicators
  train_matrix@x[train_matrix@x > 1] <- 1
  test_matrix@x[test_matrix@x > 1] <- 1

  ## Dropping training features not in the test set
  train_matrix <- train_matrix[,
                  (quanteda::features(train_matrix) %in% quanteda::features(test_matrix))]


  est <- trainNB(training_agendas$coding, train_matrix)

  out <- classifyNB(est, test_matrix, test_agendas)

mattwloftis/agendacodeR documentation built on June 5, 2023, 7 p.m.