Project 4

Author

Mubin Ejaz

Approach

My approach to this project will be to build a text classification model that can distinguish between spam and non-spam (ham) emails using the provided corpus.

  1. Read in the training email documents: I will start by downloading and extracting the corpus and reading the email documents from the ham and spam directories into R. I will assign each email a unique ID and a class label identifying it as either spam or ham.

  2. Clean and prepare the email text: I will remove email headers and metadata, such as sender, recipient, subject, and date, so that the analysis focuses primarily on the content of the email. I will also prepare the text for analysis by converting it to lowercase and removing punctuation, numbers, and common stop words where appropriate.

  3. Build a text corpus: I will use the Corpus(), VCorpus(), or SimpleCorpus() functions from the tm package to organize the cleaned email documents into a text corpus.

  4. Create a Document-Term Matrix (DTM): Using the DocumentTermMatrix() function from the tm package, I will convert the email corpus into a Document-Term Matrix. Each row will represent an email, each column will represent a term, and the values will represent the frequency of each term in an email. This will transform the email text into a format that can be used for classification.

  5. Prepare the training and testing data: I will separate the labeled emails into training and testing datasets. The training data will be used to build the classification model, while the testing data will be used to evaluate how well the model classifies emails it has not previously seen.

  6. Build and evaluate the classification model: I will use the training data to develop a model that predicts whether an email is spam or ham. I will then apply the model to the testing data and evaluate its performance using a confusion matrix and classification metrics such as accuracy, precision, recall, and F1 score.

  7. Test the model on new emails: Finally, I will use the trained model to classify new email documents that were not included in the training data and determine whether they are spam or ham.

The overall goal is to use the words and patterns found in previously classified emails to train a model that can accurately identify the classification of new, unseen emails.

Code Chunks I’ve tried

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidytext)
library(dplyr)
library(janeaustenr)
library(stringr)
library(tidytext)
library(tm)
library(quanteda)
Package version: 4.5.1
Unicode version: 15.1
ICU version: 74.1
Parallel computing: 20 of 20 threads used.
See https://quanteda.io for tutorials and examples.

Attaching package: 'quanteda'

The following objects are masked from 'package:tm':

    meta, meta<-, stopwords

Building file lists from spam and ham directories

ham_path<-list.files("easy_ham_2", full.names=TRUE)
spam_path<-list.files("spam_2", full.names=TRUE)

Reading in the spam and ham emails:

ham_data<-tibble(email_id=basename(ham_path), class = "ham", 
                 text=map_chr(ham_path, readr::read_file))
spam_data<-tibble(email_id=basename(spam_path), class = "spam", 
                 text=map_chr(spam_path, readr::read_file))
email_binded_Data <- bind_rows(ham_data, spam_data)

email_words <- email_binded_Data |>
  unnest_tokens(word, text)

email_counts <- email_words |>
  count(email_id, word)