-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel Development.R
More file actions
436 lines (327 loc) · 15.1 KB
/
Copy pathModel Development.R
File metadata and controls
436 lines (327 loc) · 15.1 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
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
# Download the data files and put them in the same directory
# if your are using Rstudio, you can use the Session button from the main menu
# to set the working directory
# Make sure you have the following libraries installed for the analysis:
# lubridate
# ggplot2
# grid
# gridExtra
# scales
# Hmisc
# corrplot
# randomForest
# rattle
# caret
# e1071
# RGTk2 (for rattle)
#
# Since the training of the models is computationally intensive, it is suggested
#to save
# all the R objects created in the session by running the command:
# save.image("yourfilenamechoice.Rdata")
# Be ware that you save the objects after loading the Rdata, else you can loose
# Previously saved data.
#setwd("//umons.ac.be/users/531323/Desktop/Occupancy/data_paper/")
setwd("D:/Aakash_Documents/MS_Collections/AcceptanceFromSaintPeters/ClassStuff/DS630_MachineLearning/FInalProjectWork/Occupancy-detection-data-master")
# Run the save and load commands according to your training progress.
# Pay attention to not
# overwrite the file with empty or less data
save.image(file="occupancy_last.RData")
#load("occupancy_last.RData")
# Loading data
datatraining <- read.table("datatraining.txt",header=TRUE,sep=",")
datatesting <- read.table("datatest.txt",header=TRUE,sep=",")
datatesting2 <- read.table("datatest2.txt",header=TRUE,sep=",")
#Reviewing the data classes
str(datatraining)
str(datatesting)
str(datatesting2)
datatraining$Occupancy <- as.factor(datatraining$Occupancy)
datatesting$Occupancy <- as.factor(datatesting$Occupancy)
datatesting2$Occupancy <- as.factor(datatesting2$Occupancy)
# Formating the date class for all the files
datatraining$date <- as.POSIXct(datatraining$date,tz="UTC")
datatesting$date <- as.POSIXct(datatesting$date,tz="UTC")
datatesting2$date <- as.POSIXct(datatesting2$date,tz="UTC")
weekend_weekday <- function(x) {
val <- weekdays(x)
if (val == "Saturday" | val == "Sunday") {
val2 = "Weekend"
}
else {
val2= "Weekday"
}
return(val2)
}
# Used for plotting
Relevel_weekend <- function(x) {
if (x == "Weekend") {
val2 = 0
}
else {
val2= 1
}
return(val2)
}
# Used to add the seconds and weekend weekday columns
datatraining_b <- datatraining
# Use to add the weekend/weekday label
datatraining_b$WeekStatus <-unlist(lapply(datatraining$date,weekend_weekday))
summary(datatraining_b)
str(datatraining_b)
datatraining_b$WeekStatus <-as.factor(datatraining_b$WeekStatus)
str(datatraining_b)
# for datatesting
datatesting_b <- datatesting
# to add the weekend/weekday label
datatesting_b$WeekStatus <-unlist(lapply(datatesting_b$date,weekend_weekday))
summary(datatesting_b)
str(datatesting_b)
datatesting_b$WeekStatus <-as.factor(datatesting_b$WeekStatus)
# for datatesting 2
datatesting2_b <- datatesting2
# to add the weekend/weekday label
datatesting2_b$WeekStatus <-unlist(lapply(datatesting2_b$date,weekend_weekday))
summary(datatesting2_b)
str(datatesting2_b)
datatesting2_b$WeekStatus <-as.factor(datatesting2_b$WeekStatus)
str(datatesting2_b)
# Turning the Occupancy variable into a factor
datatraining$Occupancy <- as.factor(datatraining$Occupancy)
datatesting$Occupancy <- as.factor(datatesting$Occupancy)
datatesting2$Occupancy <- as.factor(datatesting2$Occupancy)
# Reviewing the data types again
str(datatraining)
str(datatraining_b)
str(datatesting)
str(datatesting2)
# Examining the classes distribution for skewness
prop.table(table(datatraining$Occupancy))
# 0 1
# 0.7876704 0.21232
prop.table(table(datatesting$Occupancy))
# 0 1
#0.635272 0.364728
prop.table(table(datatesting2$Occupancy))
# 0 1
# 0.7898893 0.2101107
# Checking that there are not NAs in the data sets... neccesary for model training.
summary(datatraining)
summary(datatesting)
summary(datatesting2)
# Exploratory Figure
library(ggplot2)
library(grid)
library(gridExtra)
library(scales)
pushViewport(viewport(layout = grid.layout(6, 1)))
myplot1 <- ggplot(datatesting,aes(date))+geom_line(color="Red",aes(y=Temperature))+ylab("Temperature")+xlab("Time")+
scale_x_datetime(breaks=date_breaks("60 min"),labels=date_format("%H:%M"),
limits=as.POSIXct(c("2015-02-03 8:00","2015-02-04 9:00"),tz="GMT"))+
theme(axis.text.x=element_text(angle=90,hjust=1))
myplot2 <- ggplot(datatesting,aes(date))+geom_line(color="Blue",aes(y=Humidity))+ylab("Humidity")+xlab("Time")+
scale_x_datetime(breaks=date_breaks("60 min"),labels=date_format("%H:%M"),
limits=as.POSIXct(c("2015-02-03 8:00","2015-02-04 9:00"),tz="GMT"))+
theme(axis.text.x=element_text(angle=90,hjust=1))
myplot3 <- ggplot(datatesting,aes(date))+geom_line(color="deepskyblue1",aes(y=HumidityRatio))+ylab("HumidityRatio")+xlab("Time")+
scale_x_datetime(breaks=date_breaks("60 min"),labels=date_format("%H:%M"),
limits=as.POSIXct(c("2015-02-03 8:00","2015-02-04 9:00"),tz="GMT"))+
theme(axis.text.x=element_text(angle=90,hjust=1))
myplot4 <- ggplot(datatesting,aes(date))+geom_line(color="Green",aes(y=CO2))+ylab("CO2 (ppm)")+xlab("Time")+
scale_x_datetime(breaks=date_breaks("60 min"),labels=date_format("%H:%M"),
limits=as.POSIXct(c("2015-02-03 8:00","2015-02-04 9:00"),tz="GMT"))+
theme(axis.text.x=element_text(angle=90,hjust=1))
myplot5 <- ggplot(datatesting,aes(date))+geom_line(color="gold4",aes(y=Light))+ylab("Light (Lux)")+xlab("Time")+
scale_x_datetime(breaks=date_breaks("60 min"),labels=date_format("%H:%M"),
limits=as.POSIXct(c("2015-02-03 8:00","2015-02-04 9:00"),tz="GMT"))+
theme(axis.text.x=element_text(angle=90,hjust=1))
myplot6 <- ggplot(datatesting,aes(date))+geom_line(color="Black",aes(y=as.numeric(Occupancy)))+ylab("Occupancy")+xlab("Time")+
scale_x_datetime(breaks=date_breaks("60 min"),labels=date_format("%H:%M"),
limits=as.POSIXct(c("2015-02-03 8:00","2015-02-04 9:00"),tz="GMT"))+
theme(axis.text.x=element_text(angle=90,hjust=1))
#ggplot(dataaggregated3bclenaed,aes(date))+geom_line(color="Orange",aes(y=HumidityRatio))+ylab("HumidityRatio (kg/kg)")+xlab("Time")+
# scale_x_datetime(breaks=date_breaks("30 min"),labels=date_format("%H:%M"),
# limits=as.POSIXct(c("2015-02-10 11:50","2015-02-10 18:50"),tz="GMT"))+theme(axis.text.x=element_text(angle=90,hjust=1))
#
print(myplot1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(myplot2, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(myplot3, vp = viewport(layout.pos.row = 3, layout.pos.col = 1))
print(myplot4, vp = viewport(layout.pos.row = 4, layout.pos.col = 1))
print(myplot5, vp = viewport(layout.pos.row = 5, layout.pos.col = 1))
print(myplot6, vp = viewport(layout.pos.row = 6, layout.pos.col = 1))
# These commands are used to ensure that the time axis for the
# plots are all aligned
myplot1 <- ggplot_gtable(ggplot_build(myplot1))
myplot2 <- ggplot_gtable(ggplot_build(myplot2))
myplot3 <- ggplot_gtable(ggplot_build(myplot3))
myplot4 <- ggplot_gtable(ggplot_build(myplot4))
myplot5 <- ggplot_gtable(ggplot_build(myplot5))
myplot6 <- ggplot_gtable(ggplot_build(myplot6))
maxWidth = unit.pmax(myplot1$widths[2:3],myplot2$widths[2:3],myplot3$widths[2:3],
myplot4$widths[2:3],myplot5$widths[2:3],myplot6$widths[2:3])
myplot1$widths[2:3] <- maxWidth
myplot2$widths[2:3] <- maxWidth
myplot3$widths[2:3] <- maxWidth
myplot4$widths[2:3] <- maxWidth
myplot5$widths[2:3] <- maxWidth
myplot6$widths[2:3] <- maxWidth
grid.arrange(myplot1, myplot2,
myplot3, myplot4,
myplot5, myplot6,ncol=1)
# Run the commented line if you want to safe the plot in a png file
# png(file="occupancy2.png",width=1250,height=1200,res=75)
grid.arrange(myplot1, myplot2,
myplot3, myplot4,
myplot5, myplot6,ncol=1)
ModelLDA <- train(Occupancy~.-date,method="lda",data=datatraining)
ModelLDA
plot(varImp(ModelLDA,scale=TRUE))
# Pairs plot
dataset = data.frame(occupancystatus = datatraining[,"Occupancy"],
lda=predict(ModelLDA,datatraining),
temperature=datatraining[,"Temperature"],
Light=datatraining[,"Light"])
plda <- ggplot(dataset) + geom_point(aes(temperature,Light, colour = occupancystatus, shape = occupancystatus), size = 2.5)
plot(plda)
cols2 <- character(nrow(datatraining))
cols2[] <- "black"
cols2[datatraining$Occupancy %in% c("0")] <- "green"
cols2[datatraining$Occupancy %in% c("1")] <- "blue"
pairs(datatraining[2:6], col=cols2, cex=1.1, cex.labels=1.5)
# weekend_weekday
cols2 <- character(nrow(datatraining_b))
cols2[] <- "black"
cols2[datatraining_b$Occupancy %in% c("0")] <- "green"
cols2[datatraining_b$Occupancy %in% c("1")] <- "blue"
#pairs(datatraining_b[c(2,3,4,5,6,8,9)], col=cols2, cex=1.1, cex.labels=1.5)
pairs(datatraining_b[c(2,3,4,5,6,8)], col=cols2, cex=1.1, cex.labels=1.5)
#releveling the weekend_weekday for visualization purposes
datatraining_b_2 <-datatraining_b
datatraining_b_2$WeekStatus <- unlist(lapply(datatraining_b_2$WeekStatus,Relevel_weekend))
pairs(datatraining_b_2[c(2,3,4,5,6,8)], col=cols2, cex=1.1, cex.labels=1.5)
# Obtaining correlation between variables
#cor(datatraining[2:6])
cor(datatraining_b_2[c(2,3,4,5,6,8)])
library(Hmisc)
correlation_result<-rcorr(as.matrix(datatraining[2:6]))
correlation_result
correlation_result$P
rcorr(as.matrix(datatraining_b_2[c(2,3,4,5,6,8,9)]),type = "pearson")
rcorr(as.matrix(datatraining_b_2[c(2,3,4,5,6,8,9)]),type = "spearman")
correlation_result_b<-rcorr(as.matrix(datatraining_b_2[c(2,3,4,5,6,8,9)]))
correlation_result_b
correlation_result_b$P
# Plotting the correlation plot
library(corrplot)
corrplot(correlation_result_b$r,type="upper", order="hclust", tl.col="black", tl.srt=45)
# Loading caret, randomForest, svm libraries
library(randomForest)
library(rattle)
library(MASS)
library(ISLR)
#
# When training the models, also execute the set.seed command to ensure
# the tranined model is reproducible
# The models with ***_b include training data with NS and WS
#
set.seed(1234)
ModelRF_ALL_b <- train(Occupancy~.-date,method="rf",data=datatraining_b)
ModelRF_ALL_b
# Accuracy 0.993
plot(ModelRF_ALL_b)
ModelRF_ALL_b$finalModel
varImp(ModelRF_ALL_b)
plot(varImp(ModelRF_ALL_b,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b,datatraining_b))
#100.0
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b,datatesting_b))/dim(datatesting_b)[1]*100
# 95.53% acuracy
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b,datatesting2_b))/dim(datatesting2_b)[1]*100
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b,datatesting_b))
# 0.9553 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b,datatesting2_b))
# 0.9806 Acuracy
ModelRF_ALL_b$finalModel
# Creating a plot of the random forest error
dev.off()
plot(1:500,ModelRF_ALL_b$finalModel$err.rate[,1],
ylim=range(c(ModelRF_ALL_b$finalModel$err.rate[,1], ModelRF_ALL_b$finalModel$err.rate[,2],
ModelRF_ALL_b$finalModel$err.rate[,3])),type='l',col='blue',axes=TRUE,xlab="Number of Trees",ylab="Error")
par(new=TRUE)
plot(1:500,ModelRF_ALL_b$finalModel$err.rate[,2],
ylim=range(c(ModelRF_ALL_b$finalModel$err.rate[,1], ModelRF_ALL_b$finalModel$err.rate[,2],
ModelRF_ALL_b$finalModel$err.rate[,3])),type='l',col="red",axes=TRUE,xlab="Number of Trees",ylab="Error")
par(new=TRUE)
plot(1:500,ModelRF_ALL_b$finalModel$err.rate[,3],ylim=range(c(ModelRF_ALL_b$finalModel$err.rate[,1],
ModelRF_ALL_b$finalModel$err.rate[,2],
ModelRF_ALL_b$finalModel$err.rate[,3])),type='l',xlab="Number of Trees",ylab="Error")
legend("topright",legend= c("Not Occupied (0)","Occupied (1)"), bty ="n", lwd=1,
col=c("blue","red","black"))
##
# T, H, Light, W, NS, WS (no CO2, no Light)
set.seed(1234)
ModelRF_ALL_b_no_CO2noLight <- train(Occupancy~.-date-CO2-Light,method="rf",data=datatraining_b)
ModelRF_ALL_b_no_CO2noLight
# Accuracy 0.9924
plot(ModelRF_ALL_b_no_CO2noLight)
ModelRF_ALL_b_no_CO2noLight$finalModel
varImp(ModelRF_ALL_b_no_CO2noLight)
plot(varImp(ModelRF_ALL_b_no_CO2noLight,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_no_CO2noLight,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_no_CO2noLight,datatesting_b))/dim(datatesting_b)[1]*100
# 94.86% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_no_CO2noLight,datatesting_b))
# 94.86 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_no_CO2noLight,datatesting2_b))
# 91.66 Acuracy
# Creating a plot of the random forest error
dev.off()
par(new=TRUE)
plot(1:500,ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,2],
ylim=range(c(ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,1], ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,2],
ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,3])),type='l',col="red",axes=TRUE,xlab="Number of Trees",ylab="Error")
par(new=TRUE)
plot(1:500,ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,3],ylim=range(c(ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,1],
ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,2],
ModelRF_ALL_b_no_CO2noLight$finalModel$err.rate[,3])),type='l',xlab="Number of Trees",ylab="Error")
legend("topright",legend= c("Not Occupied (0)","Occupied (1)"), bty ="n", lwd=1,
col=c("red","black"))
## lda models
ModelLDA_ALL_b <- train(Occupancy~.-date,method="lda",data=datatraining_b)
ModelLDA_ALL_b$finalModel$xNames
ModelLDA_ALL_b$finalModel
ModelLDA_ALL_b
varImp(ModelLDA_ALL_b)
plot(varImp(ModelLDA_ALL_b,scale=TRUE))
#0.9878
confusionMatrix(datatraining_b$Occupancy,predict(ModelLDA_ALL_b,datatraining_b))
#0.9885
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelLDA_ALL_b,datatesting_b))
# 0.979 Accuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelLDA_ALL_b,datatesting2_b))
# 0.9933 Accuracy
ModelLDA_ALL_b$finalModel
### no CO2 no Light, with WS, NS
ModelLDA_noCO2NoLight_b <- train(Occupancy~.-date-CO2-Light,method="lda",data=datatraining_b)
ModelLDA_noCO2NoLight_b$finalModel$xNames
ModelLDA_noCO2NoLight_b$finalModel
# 0.8571
confusionMatrix(datatraining_b$Occupancy,predict(ModelLDA_noCO2NoLight_b,datatraining_b))
#0.8578
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelLDA_noCO2NoLight_b,datatesting_b))
# 0.8593 Accuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelLDA_noCO2NoLight_b,datatesting2_b))
# 0.867 Accuracy
ModelLDA_noCO2NoLight_b