Project 4: Document Classification — Approach

Author

Chanice McKenzie

Introduction

Email spam filtering is a practical application of document classification in which previously labeled messages are used to predict the class of new messages. For this project, I plan to use the SpamAssassin public corpus to develop a classification model that distinguishes spam from legitimate email (ham). My primary question is: How accurately can the textual content of an email be used to predict whether it is spam, and which classification method provides the best balance between detecting spam and avoiding the misclassification of legitimate messages?

Tackling the Problem

Data Collection and Preparation

I will download the labeled spam and ham email collections from the SpamAssassin public corpus, extract the compressed archives, and import the individual email files into R. I will organize the messages into a data frame containing a unique document identifier, the email text, and its known classification. Each row will represent one email. I will inspect the class distribution, identify empty or unreadable documents, and check for duplicate messages before modeling.

I plan to use the tidyverse, tidytext, and quanteda packages to prepare the text for analysis. Email messages may contain headers, HTML markup, URLs, attachments, and other non-text content. I will focus on the subject and readable message body, excluding routing headers and other metadata that could reveal the classification indirectly. I will standardize capitalization, tokenize the text, and remove unnecessary punctuation and common stop words where appropriate. I will also consider preserving meaningful indicators such as URLs and currency symbols through standardized tokens rather than automatically discarding them.

Feature Extraction and Model Development

Because classification algorithms require numerical inputs, I will transform the cleaned text into a document-feature matrix using word frequencies. I will also investigate term frequency-inverse document frequency (TF-IDF) as an alternative representation that reduces the influence of words appearing frequently across many documents.

I will divide the labeled documents into training and testing datasets using an approximately 80/20 stratified split to preserve the proportion of spam and ham in each dataset. Duplicate or near-duplicate messages identified during preparation will be removed or kept within the same partition to reduce data leakage. Vocabulary selection and any learned feature transformations will be based only on the training data.

I plan to compare a multinomial Naive Bayes classifier using word counts with a regularized logistic regression classifier using numerical text features. Naive Bayes is a useful starting point for text classification because it estimates class probabilities based on observed word frequencies. Logistic regression provides an alternative that can help identify which textual features are associated with spam. I will use cross-validation within the training dataset to compare model configurations before evaluating the selected models on the withheld test data. A majority-class prediction will serve as a simple baseline.

Anticipated Data Challenges

One challenge is that the emails are unstructured text files rather than a conventional rectangular dataset. Differences in encoding, MIME formatting, HTML content, and message structure may make extracting readable text difficult. I will inspect problematic files and document any exclusions or cleaning decisions.

Another concern is class imbalance. If one class is substantially larger than the other, a model may achieve high accuracy simply by favoring the majority class. Stratified sampling and class-specific evaluation metrics will help address this issue. If needed, I will investigate class weighting or resampling within the training data only.

Data leakage is another important consideration. Email headers may contain spam-filter annotations, and duplicate messages may appear across the collections. Including these indicators or allowing duplicate messages into both training and testing datasets could produce unrealistically high performance. I will exclude label-revealing metadata and keep the test data separate from feature selection and model tuning.

Finally, the corpus contains historical emails that may not reflect current spam tactics. Spam messages can also contain deliberately misspelled words, unusual symbols, and misleading language that complicate text preprocessing. Therefore, strong performance on this dataset would not necessarily indicate that the model would perform equally well on modern email traffic.

References

Apache SpamAssassin. (n.d.). Public mail corpus. https://spamassassin.apache.org/old/publiccorpus/

Silge, J., & Robinson, D. (2017). Text mining with R: A tidy approach. O’Reilly Media. https://www.tidytextmining.com/