-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintro_to_classifiers.Rmd
168 lines (107 loc) · 3.41 KB
/
intro_to_classifiers.Rmd
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
title: "Classifiers"
subtitle: "logistic regression and knn"
author: "Neeraj and Sahil"
date: "7/1/2017"
output: html_document
---
```{r setup, include=FALSE}
library(nutshell)
library(ggplot2)
library(caTools)
library(e1071)
```
# An intro to the data & problem
We have a dataset of word frequencies in a sample of emails. The emails are marked as either spam or not. We want to identify frequently used words to help us automatically classify emails as either spam or not.
```{r}
data('spambase')
head(spambase)
#str(spambase)
```
```{r}
#subset to training and testing dataset
```
## run linear regression
```{r}
spambase_num = spambase
spambase_num$is_spam = as.numeric(as.character(spambase$is_spam))
fit_lm = lm(formula = is_spam~., data = spambase_num)
```
## compare response and prediction
```{r}
plot(spambase_num$is_spam, fit_lm$fitted.values,
xlab = "truth", ylab = "prediction (linear regression)",
main = "Is it really SPAM?")
```
```{r}
plot(density(fit_lm$fitted.values),
main = "Distribution of predicted values\nusing linear regression")
```
Use a cuttoff of 0.5 and see
```{r}
table(fit_lm$fitted.values > 0.5, spambase_num$is_spam)
```
```{r}
predict <- predict(fit_lm, type = 'response')
library(ROCR)
ROCRpred <- prediction(predict, spambase$is_spam)
ROCRperf <- performance(ROCRpred, 'tpr','fpr')
plot(ROCRperf, colorize = TRUE, text.adj = c(-0.2,1.7))
```
Not to bad, right? Is this serviceable? Normally, when you are talking about a classification problem, you'll use logistict regression. But why? And what is logistic regression?
The simple form of logistic regression model is
`g(E(y)) = α + βx1 + γx2`
You can think of logistic regression as like linear regression, except as our dependent variable, we're using the log of the odds of the class being one or the other (i.e. 0 or 1). By fitting the data to a _logit_ function, we can be sure to see only predictions between zero and one. You may recall in the linear regression example, we saw some predictions below zero (!).
```{r}
# logistic regression
fit <- glm(is_spam ~ ., family='binomial', data=spambase[, !(names(spambase) %in% c("char_freq_left_paren","char_freq_left_bracket"))])
fit <- glm(is_spam ~ ., family='binomial', data = spambase)
plot(spambase$is_spam, fit$fitted.values,
xlab = "truth", ylab = "prediction (logistic regression)",
main = "Is it really SPAM?")
summary(fit)
```
# density of probabilities
```{r}
plot(density(fit$fitted.values))
```
# confusion matrix
```{r}
table(fit$fitted.values > 0.5, spambase$is_spam)
```
```{r}
table(fit$fitted.values > 0.5, spambase$is_spam)
```
# ROC
```{r}
predict <- predict(fit, type = 'response')
library(ROCR)
ROCRpred <- prediction(predict, spambase$is_spam)
ROCRperf <- performance(ROCRpred, 'tpr','fpr')
plot(ROCRperf, colorize = TRUE, text.adj = c(-0.2,1.7))
```
# pretty ROC
```{r}
roc_perf_df <- data.frame([email protected][[1]],
[email protected][[1]],
[email protected][[1]])
ggplot(roc_perf_df, aes(x=x, y=y, color=a)) +
geom_line() +
scale_color_gradientn(colors=c("#FFED28", "#CC1120", "#5BB5CC"))
```
```{r}
library(randomForest)
set.seed(123)
rf.fit = randomForest(is_spam ~ .,
ntree = 100,
data = spambase)
plot(rf.fit)
```
```{r}
print(rf.fit)
```
```{r}
varImpPlot(rf.fit,
sort=T,
n.var=10)
```