\documentclass[a4paper]{article}

%\VignetteIndexEntry{Introduction to tbnb}
%\VignetteEngine{utils::Sweave}
%\VignetteKeyword{naive Bayes}
%\VignetteKeyword{text classification}
%\VignetteDepends{tbnb}

\usepackage[utf8]{inputenc}
\usepackage{a4wide}

\title{Introduction to \texttt{tbnb}}
\author{Maurizio Romano \and Claudio Conversano \and Gianpaolo Zammarchi
  \and Giulia Contu \and Francesco Mola}

\begin{document}
\setkeys{Gin}{width=0.7\textwidth}
\maketitle

<<setup, echo=FALSE>>=
.old_options <- options(width = 80, prompt = "R> ", continue = "+  ")
library(tbnb)
@

\texttt{tbnb} implements the \textbf{Threshold-Based Naive
Bayes} (Tb-NB) classifier of Romano \emph{et al.}\ (2024) and its iterative
refinement (iTb-NB), following an idiomatic R interface to the algorithm
published in the reference papers.

<<load-data>>=
library(tbnb)
data(toy_reviews)
head(toy_reviews, 3)
@

\section{Fitting Tb-NB with the formula interface}

<<fit>>=
fit <- itbnb(
  sentiment ~ text,
  data       = toy_reviews,
  preprocess = tbnb_preprocess(language = "english"),
  criterion  = "balanced_error",
  K          = 5
)
fit
@

\section{Summary}

<<summary>>=
summary(fit)
@

\section{Predictions}

\texttt{predict()} supports four output types:

<<predict>>=
head(predict(fit, newdata = toy_reviews, type = "class"))
head(predict(fit, newdata = toy_reviews, type = "score"))
head(predict(fit, newdata = toy_reviews, type = "prob"))
@

\section{Iterative refinement (iTb-NB)}

<<iterative>>=
fit2 <- itbnb(
  sentiment ~ text, data = toy_reviews,
  preprocess = tbnb_preprocess(language = "english"),
  criterion  = "balanced_error", K = 5,
  iterative  = TRUE, iter_mode = "kde",
  p_iter = 0.2, s_iter = 20
)
fit2$decisions
@

The fitted \texttt{\$decisions} table can be used at predict-time when
\texttt{iterative = TRUE} (the default).

\section{Matrix interface}

If you already have a Bag-of-Words matrix (e.g.\ a \texttt{quanteda::dfm} or a
sparse \texttt{Matrix}), pass it directly:

<<matrix, eval=FALSE>>=
fit <- itbnb(x = my_dfm, y = sentiment, criterion = "f1")
@

\section{Optional embedding extension}

Pass a \texttt{tbnb\_embedding()} configuration to enrich the BoW with semantic
neighbours of every token (an approach not present in the original papers):

<<embedding, eval=FALSE>>=
emb <- tbnb_embedding(my_glove_matrix, k = 3, min_similarity = 0.6)
fit <- itbnb(sentiment ~ text, data = reviews, embedding = emb)
@

\section*{References}

\begin{itemize}
\item Romano, M., Contu, G., Mola, F., Conversano, C. (2024).
  Threshold-Based Naive Bayes classifier.
  \emph{Advances in Data Analysis and Classification}.
  doi:10.1007/s11634-023-00536-8
\item Romano, M., Zammarchi, G., Conversano, C. (2024).
  Iterative Threshold-Based Naive Bayes classifier.
  \emph{Statistical Methods \& Applications}.
  doi:10.1007/s10260-023-00721-1
\item Romano, M. (2025). A p-value extension for the Threshold-Based Naive
  Bayes classifier. doi:10.1007/978-3-031-96736-8\_41
\end{itemize}

<<reset, echo=FALSE>>=
options(.old_options)
@

\end{document}
