-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS8004-ProjectDataAnalysis.R
More file actions
223 lines (156 loc) · 6.23 KB
/
DS8004-ProjectDataAnalysis.R
File metadata and controls
223 lines (156 loc) · 6.23 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#-------------------------------------
# Title: DS8004 Project - Data Analysis
# Name: Anil Trikha
# Date: April 21, 2017
#-------------------------------------
df <- read.csv ("CollectedData/all_sports_new.csv", stringsAsFactors = FALSE)
tabulate (df$class)
levels(df$class)
unique(df$class)
library(RTextTools)
library(e1071)
library(twitteR)
library(ggplot2)
set.seed(1234)
labels <- c("basketball", "hockey", "baseball", "tennis", "volleyball")
#df <- df[df$class %in% labels,]
nPerClass = 500 # 4755
df_new <- head (df[df$class==c(1),], nPerClass)
df_new <- rbind (df_new, head (df[df$class==c(2),], nPerClass))
df_new <- rbind (df_new, head (df[df$class==c(3),], nPerClass))
df_new <- rbind (df_new, head (df[df$class==c(4),], nPerClass))
df_new <- rbind (df_new, head (df[df$class==c(5),], nPerClass))
colnames(df_new) <- c("text", "class", "tokenized")
# df<- df_new[df_new$class != c(5),]
df <- df_new
ind <- 1:length(df$class)
trainInd <- sample (ind, size = 0.7 * length(ind), replace = FALSE)
train_tweets <- cbind (df$text[trainInd], labels[df$class[trainInd]])
test_tweets <- cbind (df$text[-trainInd], labels[df$class[-trainInd]])
tweets = rbind(train_tweets, test_tweets)
# Then we can build the document-term matrix:
# build dtm
matrix= create_matrix(tweets[,1], language="english",
removeStopwords=TRUE, removeNumbers=TRUE,
stemWords=FALSE, removeSparseTerms = 0.985)
# Now, we can train the naive Bayes model with the training set. Note that, e1071 asks the response variable to be numeric or factor. Thus, we convert characters to factors here. This is a little trick.
dim(matrix)
trainLen <- nrow(train_tweets)
testLen <- nrow(test_tweets)
# trainInd <- sample (1:trainLen, 1000, replace=F)
# train the model
mat <- as.matrix(matrix)
classifier <- naiveBayes(mat[1:trainLen,], as.factor(tweets[1:trainLen,2]) )
# Now we can step further to test the accuracy.
# test the validity
predicted = predict(classifier, mat[(trainLen+1):(trainLen+testLen),]) #; predicted
t <- table(tweets[(trainLen+1):(trainLen+testLen), 2], predicted)
tmat <- as.matrix(t)
tmat
# tp <- tmat["positive", "positive"]
# fp <- tmat["negative", "positive"]
# tn <- tmat["negative", "negative"]
# fn <- tmat["positive", "negative"]
# precision <- tp / (tp + fp)
# precision
# recall <- tp / (tp + fn)
# recall
# accuracy <- (tp + tn) / (tp + fp + tn + fn)
accuracy <- sum(diag(tmat)) / sum(tmat)
accuracy
# recall_accuracy(tweets[(trainLen+1):(trainLen+testLen), 2], predicted)
cm <- data.frame(tweets[(trainLen+1):(trainLen+testLen), 2], predicted)
names(cm) <- c("Actual", "Predicted")
ggplot(cm, aes(x = Actual)) +
geom_bar(fill = "midnightblue") +
facet_grid (. ~ Predicted, labeller=label_both) +
ylab("Number of Tweets") + xlab("Actual") +
ggtitle("Tweet Prediction Results")
hist (mat[,"nba"], main = "Distribution of NBA term in Document-Term Matrix",
xlab = "Occurrences of NBA term")
#----------------------------------
# Random Forest
#----------------------------------
library(randomForest)
forest_algo <- function (d, ind, runType)
{
lastCol <- dim(d)[2]
d.training <- d[ind==1, 1:lastCol-1]
d.test <- d[ind==2, 1:lastCol-1]
d.trainLabels <- as.factor (d[ind==1, lastCol])
d.testLabels <- as.factor (d[ind==2, lastCol])
results <- list()
if (runType == "Iris") {
rfFormula <- "iris_label ~ ."
} else if (runType == "Lens") {
rfFormula <- "lens_label ~ ."
}
model <- randomForest (d.training, d.trainLabels)
d_pred <- predict(model, d.test) # round this value?
# evaluate model
results[[1]] <- table(d_pred, d.testLabels)
results
}
ind <- rep(0, length(df$class))
ind[trainInd] <- 1
ind[-trainInd] <- 2
lens_data <- as.data.frame(cbind(mat, labels[df$class]))
colnames(lens_data)[ncol(lens_data)] <- "lens_label"
resRNF.lens <- forest_algo (lens_data, ind, "Lens")
getForestAccuracy <- function (xx)
{
x <- as.data.frame(xx)
x$d_pred <- factor (x$d_pred, levels=c(levels (x$d_pred),
setdiff (levels(x$d.testLabels), levels(x$d_pred))))
sum(x[which(x$d_pred == x$d.testLabels),]$Freq) / sum(x$Freq)
}
print (paste ("Random Forest: ", "Lens"))
print (paste ("Test Accuracy: ", getForestAccuracy(resRNF.lens[[1]])))
print (t(resRNF.lens[[1]]))
x <- as.data.frame(resRNF.lens[[1]])
sum(x$Freq)
#----------------------------------------------------
# SVM Algorithm applied to both datasets
#----------------------------------------------------
library(stats)
svm_kernels = c("linear", "polynomial", "radial", "sigmoid")
svm_algo <- function (d, ind, runType)
{
lastCol <- dim(d)[2]
d.training <- sapply(d[ind==1, 1:lastCol-1], as.numeric)
d.test <- sapply(d[ind==2, 1:lastCol-1], as.numeric)
d.trainLabels <- sapply (d[ind==1, lastCol], as.factor)
d.testLabels <- sapply (d[ind==2, lastCol], as.factor)
results <- list()
for (i in 1:length(svm_kernels))
{
model <- svm(d.training, d.trainLabels, kernel = svm_kernels[i])
d_pred <- predict(model, d.test)
# evaluate model
results[[i]] <- table(d_pred, d.testLabels)
}
results
}
resSVM.lens <- svm_algo (lens_data, ind, "Lens")
getSvmAccuracy <- function (xx)
{
x <- as.data.frame(xx)
x$d_pred <- factor (x$d_pred, levels=c(levels (x$d_pred),
setdiff (levels(x$d.testLabels), levels(x$d_pred))))
# x$d.testLabels <- factor (x$d.testLabels, levels=c(levels (x$d.testLabels),
# setdiff (levels(x$d_pred), levels(x$d.testLabels))))
sum(x[which(x$d_pred == x$d.testLabels),]$Freq) / sum(x$Freq)
}
barplot (sapply (resSVM.lens, getSvmAccuracy), names.arg = svm_kernels,
col = 1:length(svm_kernels), main = "SVM Accuracy For Sports Data",
xlab = "SVM Kernel", ylab = "Test Accuracy")
svm_accuracy <- function (res)
{
for (i in 1:length(svm_kernels))
{
print (paste ("SVM Kernel: ", svm_kernels[i]))
print (paste ("Test Accuracy: ", getSvmAccuracy(res[[i]])))
print (t(res[[i]]))
}
}
svm_accuracy (resSVM.lens)