-
Notifications
You must be signed in to change notification settings - Fork 3
/
.Rhistory
512 lines (512 loc) · 19.4 KB
/
.Rhistory
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action, fill = action)) + facet_wrap(~ action)
(gg <- ggplotly(p_set1))
for (i in 1:length(set1$action))
{
if(set1$action[i]==0)
set1$action[i]="Touch Down Traces"
else if(set1$action[i]==1)
set1$action[i]="Touch Up Traces"
else
set1$action[i]="Finger Movement Traces"
}
set1 <- ph1[ph1$uid==36,]
for (i in 1:length(set1$action))
{
if(set1$action[i]==0)
set1$action.name[i]="Touch Down Traces"
else if(set1$action[i]==1)
set1$action.name[i]="Touch Up Traces"
else
set1$action.name[i]="Finger Movement Traces"
}
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action, fill = action)) + facet_wrap(~ action)
(gg <- ggplotly(p_set1))
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action.name, fill = action.name)) +
facet_wrap(~ action.name)
(gg <- ggplotly(p_set1))
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Touch Traces of User ID=36") +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action.name, fill = action.name)) +
facet_wrap(~ action.name)
(gg <- ggplotly(p_set1))
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Touch Traces of User ID=36") +
labs(x="X Coodinates", y="Y Coordinates") +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action.name, fill = action.name)) +
facet_wrap(~ action.name)
(gg <- ggplotly(p_set1))
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Touch Traces of User ID=36 in Nexus 1-Experimenter E") +
labs(x="X Coodinates", y="Y Coordinates") +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action.name, fill = action.name)) +
facet_wrap(~ action.name)
(gg <- ggplotly(p_set1))
ph2 <- dataset[dataset$p_id == 5,]
set2 <- ph2[ph2$uid==36,]
length(set2$p_id)
ph2 <- dataset[dataset$p_id == 4,]
set2 <- ph2[ph2$uid==36,]
length(set2$p_id)
unique(set1$p_id)
unique(set1$uid)
unique(set1$ph1)
unique(ph1$uid)
a<-ph1[ph1$uid==5]
a<-ph1[ph1$uid==5,]
unique(a$uid)
unique(a$p_id)
a<-ph1[ph1$uid==39,]
unique(a$p_id)
a<-ph1[ph1$uid==16,]
unique(a$p_id)
a<-ph1[ph1$uid==18,]
unique(a$p_id)
a<-ph1[ph1$uid==12,]
unique(a$p_id)
(gg <- ggplotly(p_doc))
for (i in 1:length(set1$docid))
{
if(set1$docid[i]==0)
set1$doc.name[i]="Wikipedia Artice 1"
else if(set1$docid[i]==1)
set1$doc.name[i]="Wikipedia Article 2"
else if(set1$docid[i]==2)
set1$doc.name[i]="Wikipedia Article 3"
else if(set1$docid[i]==3)
set1$doc.name[i]="Image Comparion Game 1"
else if(set1$docid[i]==4)
set1$doc.name[i]="Image Comparion Game 2"
else if(set1$docid[i]==5)
set1$doc.name[i]="Wikipedia Article 4"
else
set1$doc.name[i]="Image Comparion Game 3"
}
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
geom_point(aes(text = paste( "Area :", area)), size = 4) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
smallset<-dataset[sample(nrow(dataset),50000),]
dataS1<-smallset[c(1,9)]
phset<-ddply(dataS1, .(p_id), summarise, prsr_mean=mean(pressure), prsr_sd=sd(pressure))
library("plyr")
phset<-ddply(dataS1, .(p_id), summarise, prsr_mean=mean(pressure), prsr_sd=sd(pressure))
ggplot(dataS1, aes(x=p_id, y=pressure)) + geom_point() + geom_point(data=phset, aes(y=prsr_mean), color='red', size=4)
head(phset)
ggplot(dataS1, aes(x=p_id, y=pressure)) + geom_point() + geom_point(data=phset, aes(y=prsr_sd), color='blue', size=3)
ggplot(dataS1, aes(x=p_id, y=pressure, xlab="Phone_id",ylab="Standard Deviation of Pressure")) +
geom_point() +
geom_point(data=phset, aes(y=prsr_sd), color='blue', size=4) +
geom_point(data=phset, aes(y=prsr_mean), color='red', size=4)
usrdataS1<-ddply(smallset, .(uid), summarise, prsr_mean=mean(pressure), prsr_sd=sd(pressure))
usrdataS1<-ddply(smallset, .(uid), summarise, prsr_mean=mean(pressure), prsr_sd=sd(pressure))
set2<-smallset[c(2,9)]
head(usrdataS1)
usrdataS1
head(usrdataS1)
ggplot(set2, aes(x=uid,y=pressure)) +
geom_point() +
geom_point(data=usrdataS1,aes(y=prsr_mean),color='red',size=4)+
geom_point(data=usrdataS1,aes(y=prsr_sd),color='blue',size=4)
qplot(uid,area,data=smallset,geom = c("point", "smooth"),alpha=I(1/100))
qplot(uid,area,data=smallset,geom = c("point", "smooth"))
qplot(uid,area,data=smallset,gam = c("point", "smooth"))
qplot(uid,area,data=smallset,geom = c("point", "smooth"))
qplot(uid,area,data=smallset,geom = c("point", "smooth"),alpha=I(1/100))
qplot(sqrt(pressure),data=smallset,geom="histogram")
qplot((pressure),data=smallset,geom="histogram")
qplot(pressure,data=smallset,geom="histogram")
qplot(area,data=smallset,geom="histogram")
names(smallset)
qplot(t_ms,data=smallset,geom="histogram")
qplot(action,data=smallset,geom="histogram")
qplot(action,data=smallset,geom="density")
str(dataset)
levels(smallset)
#subset user with id 1
user1 <- dataset[dataset$uid==1,]
#length(user1$p_id)
#check the first few items of the subset
#head(user1)
#Orientation of a particular user
orientation <- function(user,orient)
{
qplot(data = user[(user$ph_ori==orient) ,], x = x_cord,
y= y_cord)
}
#function call to get the various users
#pattern in landscape and portrait
#Orient = 1 : Phone is in vertical position
#Orient = 2 : Phone is in landscape position
orientation(user1,2)
#function to get the pattern for a particular device
#qplot(area,data = user1[(user1$ph_ori)],geom="histogram")
ph1 <- dataset[dataset$p_id == 1,]
#ph1_s <- sample(ph1,11)
#Plot Pressure of users for a random of 100 training sets
ph1_s <- ph1[sample(nrow(ph1),100),]
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size = uid)
#Plot Touch Traces of User ID 36 in Nexus 1-Experimenter E
#Roll the cursor over the graph to check pressure points
set1 <- ph1[ph1$uid==36,]
for (i in 1:length(set1$action))
{
if(set1$action[i]==0)
set1$action.name[i]="Touch Down Traces"
else if(set1$action[i]==1)
set1$action.name[i]="Touch Up Traces"
else
set1$action.name[i]="Finger Movement Traces"
}
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Touch Traces of User ID 36 in Nexus 1-Experimenter E") +
labs(x="X Coodinates", y="Y Coordinates") +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action.name, fill = action.name)) +
facet_wrap(~ action.name)
(gg <- ggplotly(p_set1))
#Plot Touch Traces of User ID 36 in Droid Incredible-Experimenter E Phone
#Plot traces of accessing wikipedia article & image comparison game
for (i in 1:length(set1$docid))
{
if(set1$docid[i]==0)
set1$doc.name[i]="Wikipedia Artice 1"
else if(set1$docid[i]==1)
set1$doc.name[i]="Wikipedia Article 2"
else if(set1$docid[i]==2)
set1$doc.name[i]="Wikipedia Article 3"
else if(set1$docid[i]==3)
set1$doc.name[i]="Image Comparion Game 1"
else if(set1$docid[i]==4)
set1$doc.name[i]="Image Comparion Game 2"
else if(set1$docid[i]==5)
set1$doc.name[i]="Wikipedia Article 4"
else
set1$doc.name[i]="Image Comparion Game 3"
}
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
geom_point(aes(text = paste( "Area :", area)), size = 4) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size = uid)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size = area)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
length(user1$p_id)
storeUser1 <- write.csv(user1, file="userid_1.csv")
dir()
path()
(gg <- ggplotly(p_set1))
(gg <- ggplotly(p_doc))
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
geom_point(aes(text = paste( "Area :", area)), size = 5) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
labs(x = "X Coordinate", y = "Y Coordinate")
geom_point(aes(text = paste( "Area :", area)), size = 5) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
labs(x = "X Coordinate", y = "Y Coordinate") +
geom_point(aes(text = paste( "Area :", area)), size = 5) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
labs(x = "X Coordinate", y = "Y Coordinate") +
geom_point(aes(text = paste( "Area :", area)), size = 3) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle("Traces of User ID 36 in various applications") +
labs(x = "X Coordinate", y = "Y Coordinate") +
geom_point(aes(text = paste( "Area :", area)), size = 4) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
(gg <- ggplotly(p_doc))
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
ph1_s <- ph1[sample(nrow(ph1),100),]
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=4)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=1)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=0.001)
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
help(plot_ly)
plot_ly(z = ph_ori, type = "surface")
plot_ly(ph1_s, z = ph_ori, type = "surface")
plot_ly(ph1_s, x = x_cord , y = y_cord,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
plot_ly(ph1_s, x = x_cord , y = y_cord, title = "NAme",
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
plot_ly(ph1_s, x = x_cord , y = y_cord, title = NAme,
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
plot_ly(ph1_s, x = x_cord , y = y_cord, title("Name")
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
plot_ly(ph1_s, x = x_cord , y = y_cord, title("Name"),
text=paste("Pressure: ",pressure, "UserID: ", uid),
mode="markers", color = uid, size=uid)
layout = go.Layout (
title='Plot Title')
import plotly.graph_objs as go
import plotly.plotly as py
import plotly.plotly as py
import plotly.graph_objs as go
remove(list=ls())
setwd("D:/Dropbox/UNCC/Courses Fall 15/SPL_5102_Dewan Ahmed/Project/phase3")
#Load required packages
library("ggplot2")
library("plotly")
library("plyr")
library("gdata")
#Removing unused variables from global environment
remove(list=ls())
#loading the dataset
dataset <- read.csv(file="data.csv",header = T, sep=",")
#check the data set
head(dataset,3)
#checking the levels
str(dataset)
#Orientation plots of a user
#Function call to get the various users pattern in landscape and portrait
#The function needs 2 input values:-
# 1. User id - values range from 0 to 40
# 2. Phone orientation - values can be 1 or 2
orientation <- function(userid,orient)
{
#subset user with their respective ID
user <- dataset[dataset$uid == userid,]
qplot(data = user[(user$ph_ori == orient) ,], x = x_cord,
main = toupper(orientname(orient)),
y= y_cord)
}
orientname <- function(orient){
val1 <- NULL
if(orient==1)
{
val1 = "portrait"
}
else
{
val1 = "landscape"
}
val1 = (paste(val1,"orientation plot"))
return (val1)
}
#Orient = 1 : Phone is in portrait/vertical position
#Orient = 2 : Phone is in landscape position
orientation(1,2)
orientation(1,1)
#Sample data of 50000 training sets
smallset<-dataset[sample(nrow(dataset),50000),]
#Taking phone id and pressure
dataS1<-smallset[c(2,9)]
#Calculate mean & standard deviation of pressure for every user
phset<-ddply(dataS1, .(uid), summarise, prsr_mean=mean(pressure), prsr_sd=sd(pressure))
#Plot mean & sd points of pressure for every users (ID: 0 to 40)
ggplot(dataS1, aes(x=uid, y=pressure)) +
labs(x = "User ID", y = "Pressure Deviation") +
geom_point() + ggtitle("Pressure & Standard Deviation Points of all users")+
geom_point(data=phset, aes(y=prsr_sd), color='blue', size=4) +
geom_point(data=phset, aes(y=prsr_mean), color='red', size=4)
#Subset dataset with phone id 1
ph1 <- dataset[dataset$p_id == 1,]
#Plot Pressure of users for a random of 100 training sets
ph1_s <- ph1[sample(nrow(ph1),100),]
plot_ly(ph1_s, x = x_cord , y = y_cord,main = "Pressure dots of users for a random of 100 training sets",
text=paste("Pressure: ",pressure, "Area: ", area),
mode="markers", color = uid, size=uid )
#Plot Touch Traces of users in Nexus 1-Experimenter E
#Roll the cursor over the graph to check pressure points
#Valid users for phone_id=1 are 36,5,39,16,18,12
user_finger_trace <- function(userid)
{
set1 <- ph1[ph1$uid==userid,]
for (i in 1:length(set1$action))
{
if(set1$action[i]==0)
set1$action.name[i]="Touch Down Traces"
else if(set1$action[i]==1)
set1$action.name[i]="Touch Up Traces"
else
set1$action.name[i]="Finger Movement Traces"
}
p_set1 <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle((paste("Touch Traces of User ID ",userid,
"in Nexus 1-Experimenter E"))) +
labs(x="X Axis", y="Y Axis") +
geom_point(aes(text = paste("Pressure :", pressure)), size = 4) +
geom_smooth(aes(color = action.name, fill = action.name)) +
facet_wrap(~ action.name)
ggplotly(p_set1)
}
user_finger_trace(36)
#Plot traces of accessing wikipedia article & image comparison game
#Valid users for phone_id=1 are 36,5,39,16,18,12
user_doc_traces <- function(userid)
{
set1 <- ph1[ph1$uid== userid,]
for (i in 1:length(set1$docid))
{
if(set1$docid[i]==0)
set1$doc.name[i]="Wikipedia Artice 1"
else if(set1$docid[i]==1)
set1$doc.name[i]="Wikipedia Article 2"
else if(set1$docid[i]==2)
set1$doc.name[i]="Wikipedia Article 3"
else if(set1$docid[i]==3)
set1$doc.name[i]="Image Comparion Game 1"
else if(set1$docid[i]==4)
set1$doc.name[i]="Image Comparion Game 2"
else if(set1$docid[i]==5)
set1$doc.name[i]="Wikipedia Article 4"
else
set1$doc.name[i]="Image Comparion Game 3"
}
p_doc <- ggplot(data = set1, aes(x = x_cord, y = y_cord)) +
ggtitle((paste("Traces of User ID", userid,
"in various applications"))) +
labs(x = "X Axis", y = "Y Axis") +
geom_point(aes(text = paste( "Area :", area)), size = 4) +
geom_smooth(aes(color = doc.name, fill = doc.name)) +
facet_wrap(~ doc.name)
ggplotly(p_doc)
}
user_doc_traces(5)
setwd("D:/Dropbox/UNCC/Courses Fall 15/SSDI_6112_AliSever/Project Predictive Analysis")
setwd("D:/Dropbox/UNCC/Courses Fall 15/SSDI_6112_AliSever/Project Predictive Analysis/Final Analysis/Prediction_App")
remove(list=ls())
#Predicting items with highest sales
#Algorithm: Apriori
#Packages Required:
# 1. gdata - To read CSV files
# 2. arules - For manipulating and analyzing transaction data and patterns
# - Also provides interfaces to C implementations of the association mining algorithms Apriori and Eclat
# 3. e1071 - Library for modelling svm.
#About Dataset:
# Total Training Sets = 983
# Total Products = 98
#Load the libraries
library("arules")
library("e1071")
library("gdata")
#Load the dataset
dataset<-read.csv(file="data.csv",header=T,sep=",")
subset<-dataset[c(2,5)]
subset<-subset[!duplicated(subset[c(1,2)]),]
aggSubSet<-split(subset$Product.Name,subset$Customer.Name)
trns<-as(aggSubSet,"transactions")
Rules<-apriori(trns,parameter=list(supp=0.05,conf=0.6,target="rules",
minlen=2))
ItemSet<-inspect(Rules[1:100])
#write.csv(ItemSet, file= "ItenSet.csv")
#create an item frequency bar plot for inspecting the item frequency distribution of products sold
#Top 10 items being purchased
itemFrequencyPlot(trns, topN = 30, type="absolute",
xlab="Most Frequent Item (MFI)",
ylab="Frequency",main="Top 30 Frequent Items")
#Compare Item Frequencies
#Transactions of the item with large frequencr (frankfurter)
#with the average in dataset
trns.high<-trns[trns %in% "frankfurter"]
# plot with the averages of the population plotted as a line
#(for first 20 items)
#itemFrequencyPlot(trns.high[, 1:20],type="relative",population = trns[, 1:20])
# plot lift ratio (frequency in x / frequency in population)
# for items with a support of 20% in the population
itemFrequencyPlot(trns.high, population = trns, support = 0.2,lift = TRUE, horiz = TRUE)
barplot(sort(itemFrequency(trns), decreasing=TRUE))
########################################################################################################
########################################################################################################
################ Analyzing purchase of Most frequent Item using SVM ################################
#######################Item Analyzed is Frankfurter ####################################################
svmSubset<-dataset[c(3,5,6,7)]
svmSubset <- svmSubset[svmSubset$Product.Name =="frankfurter",]
svmSubset<-svmSubset[c(1,3,4)]
##svmSubset$Quantity <- svmSubset$Unit.Price * svmSubset$Quantity
svmSubset$Total.Cost <- svmSubset$Unit.Price * svmSubset$Quantity
####names(svmSubset)[3] <- "Total.Cost"
svmSubset$Order.Year <- as.numeric(format(as.Date(svmSubset$Order.Date,"%m/%d/%Y"),"%Y"))
svmSubset$Order.Date <- as.numeric(format(as.Date(svmSubset$Order.Date,"%m/%d/%Y"),"%m"))
names(svmSubset)[1] <- "Order.Month"
for(i in 1:length(svmSubset$Total.Cost))
{
med <- median(svmSubset$Total.Cost)
if((svmSubset$Total.Cost[i])>med)
svmSubset$Order.Val[i]="High"
else
svmSubset$Order.Val[i]="Low"
}
svmSubset2 <- svmSubset[c(6,1,3)]
processData <- write.csv(svmSubset2, file= "newdata.csv")
#processData<-read.csv(file="newdata.csv",header=T,sep="," )
cleanData <- read.csv(file="newdata.csv",header=T,sep=",")
cleanData <- cleanData[c(2,3,4)]
model<-svm(Order.Val ~ .,data=cleanData)
#model<-svm(Order.Val ~ .,data=cleanData,kernel="polynomial",degree=3,coef0=0.030,cost=1.3,tolerance=0.008)
#model<-svm(Order.Val ~ .,data=cleanData,kernel="radial",gamma=0.01,cost=10)
#plot(model,cleanData)
#Now, next step is to do the prediction.
#So we classify 75% of dataset as training dataset and 25% as testing dataset
#Create an index
index<-1:nrow(cleanData)
#Create testindex to sample out the 30% of the dataset
testindex<-sample(index,trunc(length(index)*25/100))
#Segregate the testdataset and trainingdataset using the testindex
testset<-cleanData[testindex,]
trainingset<-cleanData[-testindex,]
#Plot model on trainingset
plot(model,trainingset)
#Prediction using the model which has been trained using the trainingset
prediction<-predict(model,testset[,-1])
#Generate the confusion matrix to find the accuracy
tab <-table(pred=prediction,true=testset[,1])
accuracy <- ((tab[1]+tab[4])/sum(tab))*100
accuracy
tab
remove(list=ls())