classifyNB | R Documentation |
Classify test data using output from trainNB
classifyNB(est, test_matrix, test)
est |
Output object from |
test_matrix |
A quanteda document-feature matrix with the same number of rows as |
test |
The data frame containing the text data from which |
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_ratios |
Maximum ratio of posterior to prior probability of any class for this document. This matches the ratio of the classification assigned in the |
max_match |
Class associated with maximum posterior probability. Contains predicted class for each observation in |
ratio_match |
Class associated with maximum ratio of posterior to prior probability. Contains predicted class for each observation in |
Matt W. Loftis
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.