-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAssignment 5 Spam email.R
151 lines (119 loc) · 3.85 KB
/
Assignment 5 Spam email.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Unit 5 - Spam
# Read in the data
emails = read.csv("emails.csv", stringsAsFactors=FALSE)
str(emails)
table(emails$spam)
#install.packages("tm")
library(tm)
#install.packages("SnowballC")
library(SnowballC)
corpus = Corpus(VectorSource(emails$text))
corpus = tm_map(corpus, tolower)
corpus = tm_map(corpus, PlainTextDocument)
corpus = tm_map(corpus, removePunctuation)
corpus = tm_map(corpus, removeWords, stopwords("english"))
corpus = tm_map(corpus, stemDocument)
dtm = DocumentTermMatrix(corpus)
dtm
# Remove sparse terms
spdtm = removeSparseTerms(dtm, 0.95)
spdtm
# Convert to a data frame
emailsSparse = as.data.frame(as.matrix(spdtm))
# Make all variable names R-friendly
colnames(emailsSparse) = make.names(colnames(emailsSparse))
# Add dependent variable
emailsSparse$spam = emails$spam
sort(colSums(subset(emailsSparse, spam == 0)))
sort(colSums(subset(emailsSparse, spam == 1)))
# Split the data
library(caTools)
emailsSparse$spam = as.factor(emailsSparse$spam)
set.seed(123)
split = sample.split(emailsSparse$spam, SplitRatio = 0.7)
train = subset(emailsSparse, split==TRUE)
test = subset(emailsSparse, split==FALSE)
# Logistic Regression
spamLog = glm(spam ~ ., data=train, family="binomial")
summary(spamLog)
predictLog = predict(spamLog, type="response")
library(ROCR)
ROCRpred = prediction(predictLog, train$spam)
as.numeric(performance(ROCRpred, "auc")@y.values)
table(predictLog<0.00001)
table(predictLog>0.99999)
table(predictLog>=0.00001&predictLog<=0.99999)
table(train$spam, predictLog>0.5)
# CART
library(rpart)
library(rpart.plot)
set.seed(123)
spamCART = rpart(spam ~ ., data=train, method="class")
#predCART = predict(spamCART, type = "class")
predCART = predict(spamCART)[,2]
table(train$spam, predCART>0.5)
library(ROCR)
ROCRpred = prediction(predCART, train$spam)
as.numeric(performance(ROCRpred, "auc")@y.values)
# RANDOM FOREST
library(randomForest)
# Build random forest model
set.seed(123)
spamRF = randomForest(spam ~ ., data=train, method="class")
spamRF = randomForest(spam~., data=train)
#predRF = predict(spamRF, type ="class")
predRF = predict(spamRF, type="prob")[,2]
table(train$spam, predRF>0.5)
library(ROCR)
ROCRpred = prediction(predRF, train$spam)
as.numeric(performance(ROCRpred, "auc")@y.values)
# Logistic Regression over test
predictLog = predict(spamLog, newdata=test, type="response")
table(test$spam, predictLog>0.5)
library(ROCR)
ROCRpred = prediction(predictLog, test$spam)
as.numeric(performance(ROCRpred, "auc")@y.values)
# CART over test
set.seed(123)
spamCART = rpart(spam ~ ., data=train, method="class")
predictCART = predict(spamCART, newdata=test, type="prob")[,2]
table(test$spam, predictCART>0.5)
library(ROCR)
ROCRpred = prediction(predictCART, test$spam)
as.numeric(performance(ROCRpred, "auc")@y.values)
# RF over test
set.seed(123)
spamRF = randomForest(spam~., data=train)
#predRF = predict(spamRF, type ="class")
predRF = predict(spamRF, newdata=test, type="prob")[,2]
table(test$spam, predRF>0.5)
library(ROCR)
ROCRpred = prediction(predictCART, test$spam)
as.numeric(performance(ROCRpred, "auc")@y.values)
# Video 7
# Build a CART model
library(rpart)
library(rpart.plot)
emailCART = rpart(Negative ~ ., data=trainSparse, method="class")
prp(emailCART)
# Evaluate the performance of the model
predictCART = predict(emailCART, newdata=testSparse, type="class")
table(testSparse$spam, predictCART)
# Compute accuracy
#(294+18)/(294+6+37+18)
# Baseline accuracy
table(testSparse$spam)
#300/(300+55)
# Random forest model
library(randomForest)
set.seed(123)
emailRF = randomForest(Negative ~ ., data=trainSparse)
# Make predictions:
predictRF = predict(emailRF, newdata=testSparse)
table(testSparse$spam, predictRF)
# Accuracy:
#(293+21)/(293+7+34+21)
set.seed(123)
emaillm = glm(Negative ~ ., data=trainSparse, family="binomial")
predictlm = predict(emaillm, newdata=testSparse, type="response")
table(testSparse$spam, predictlm>=0.5)