-
Notifications
You must be signed in to change notification settings - Fork 59
/
Model Development.R
3187 lines (2197 loc) · 85.5 KB
/
Model Development.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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), it is recommended you install his package before rattle
# Refer to the link below for installation
# https://gist.github.com/sebkopf/9405675
# rattle refer to the link below for rattle installation in case you
# run into problems during installation
# http://rattle.togaware.com/rattle-install-mswindows.html
# If during installation of the packages the R session ask you to install dependencies
# please do so.
# Since the training of the models is computationally intensive, it is suggested to save
# all the R objects created in the seession 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("E:/Dropbox/Occupancy/data_paper")
# 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")
library(lubridate)
# helper functions for treating date/time stamp
second_day <- function(x) {
# x is an object in posixct format
s <- hour(x)*3600+minute(x)*60+second(x)
}
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
datatraining_b$NSM <- second_day(datatraining_b$date)
# 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
datatesting_b$NSM <- second_day(datatesting_b$date)
# 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
datatesting2_b$NSM <- second_day(datatesting2_b$date)
# 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)
# Pairs plot
dataset = data.frame(occupancystatus = datatraining[,"Occupancy"],
lda=predict(ModelLDA_ALL,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)
# time in seconds and 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,9)], col=cols2, cex=1.1, cex.labels=1.5)
cor(datatraining_b[c(2,3,4,5,6,8,9)])
# Obtaining correlation between variables
#cor(datatraining[2:6])
cor(datatraining_b_2[c(2,3,4,5,6,8,9)])
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(caret)
library(e1071)
#
# 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
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
# 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("OOB","Not Occupied (0)","Occupied (1)"), bty ="n", lwd=1,
col=c("blue","red","black"))
##
# T, H, Co2, W, NS, WS (no light)
set.seed(1234)
ModelRF_ALL_b_no_light <- train(Occupancy~.-date-Light,method="rf",data=datatraining_b)
ModelRF_ALL_b_no_light
# Accuracy 0.9926
plot(ModelRF_ALL_b_no_light)
ModelRF_ALL_b_no_light$finalModel
varImp(ModelRF_ALL_b_no_light)
plot(varImp(ModelRF_ALL_b_no_light,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_no_light,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_no_light,datatesting_b))/dim(datatesting_b)[1]*100
# 94.63% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_no_light,datatesting_b))
# 94.63 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_no_light,datatesting2_b))
# 64.86 Acuracy
# T, H, Light, W, NS, WS (no CO2)
set.seed(1234)
ModelRF_ALL_b_no_CO2 <- train(Occupancy~.-date-CO2,method="rf",data=datatraining_b)
ModelRF_ALL_b_no_CO2
# Accuracy 0.9936
plot(ModelRF_ALL_b_no_CO2)
ModelRF_ALL_b_no_CO2$finalModel
varImp(ModelRF_ALL_b_no_CO2)
plot(varImp(ModelRF_ALL_b_no_CO2,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_no_CO2,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_no_CO2,datatesting_b))/dim(datatesting_b)[1]*100
# 95.64% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_no_CO2,datatesting_b))
# 95.65 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_no_CO2,datatesting2_b))
# 97.21 Acuracy
# 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
# T, Light, NS, WS
set.seed(1234)
ModelRF_ALL_b_T_Light <- train(Occupancy~.-date-CO2-Humidity-HumidityRatio,method="rf",data=datatraining_b)
ModelRF_ALL_b_T_Light
# Accuracy 0.99397
plot(ModelRF_ALL_b_T_Light)
ModelRF_ALL_b_T_Light$finalModel
varImp(ModelRF_ALL_b_T_Light)
plot(varImp(ModelRF_ALL_b_T_Light,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_T_Light,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_T_Light,datatesting_b))/dim(datatesting_b)[1]*100
# 95.497% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_T_Light,datatesting_b))
# 95.5 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_T_Light,datatesting2_b))
# 97.28 Acuracy
# Humidity, Light, NS, WS
set.seed(1234)
ModelRF_ALL_b_Humidity_Light <- train(Occupancy~.-date-CO2-Temperature-HumidityRatio,method="rf",data=datatraining_b)
ModelRF_ALL_b_Humidity_Light
# Accuracy 0.9939
plot(ModelRF_ALL_b_Humidity_Light)
ModelRF_ALL_b_Humidity_Light$finalModel
varImp(ModelRF_ALL_b_Humidity_Light)
plot(varImp(ModelRF_ALL_b_Humidity_Light ,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_Humidity_Light,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_Humidity_Light,datatesting_b))/dim(datatesting_b)[1]*100
# 96.92% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_Humidity_Light,datatesting_b))
# 96.92 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_Humidity_Light,datatesting2_b))
# 98.31 Acuracy
# CO2, Light, NS, WS
set.seed(1234)
ModelRF_ALL_b_CO2_Light <- train(Occupancy~.-date-Humidity-Temperature-HumidityRatio,method="rf",data=datatraining_b)
ModelRF_ALL_b_CO2_Light
# Accuracy 0.9941
plot(ModelRF_ALL_b_CO2_Light)
ModelRF_ALL_b_CO2_Light$finalModel
varImp(ModelRF_ALL_b_CO2_Light)
plot(varImp(ModelRF_ALL_b_CO2_Light,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_CO2_Light,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_CO2_Light,datatesting_b))/dim(datatesting_b)[1]*100
# 96.06% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_CO2_Light,datatesting_b))
# 96.06 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_CO2_Light,datatesting2_b))
# 97.42 Acuracy
# CO2, Temp, NS, WS
set.seed(1234)
ModelRF_ALL_b_CO2_Temp <- train(Occupancy~.-date-Humidity-Light-HumidityRatio,method="rf",data=datatraining_b)
ModelRF_ALL_b_CO2_Temp
# Accuracy 0.9925
plot(ModelRF_ALL_b_CO2_Temp)
ModelRF_ALL_b_CO2_Temp$finalModel
varImp(ModelRF_ALL_b_CO2_Temp)
plot(varImp(ModelRF_ALL_b_CO2_Temp,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_CO2_Temp,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_CO2_Temp,datatesting_b))/dim(datatesting_b)[1]*100
# 95.64% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_CO2_Temp,datatesting_b))
# 95.65 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_CO2_Temp,datatesting2_b))
# 57.85 Acuracy
# Light,W, NS, WS
set.seed(1234)
ModelRF_ALL_b_Light_W <- train(Occupancy~.-date-Humidity-Temperature-CO2,method="rf",data=datatraining_b)
ModelRF_ALL_b_Light_W
# Accuracy 0.9936
plot(ModelRF_ALL_b_Light_W)
ModelRF_ALL_b_Light_W$finalModel
varImp(ModelRF_ALL_b_Light_W)
plot(varImp(ModelRF_ALL_b_Light_W,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_Light_W,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_Light_W,datatesting_b))/dim(datatesting_b)[1]*100
# 96.47% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_Light_W,datatesting_b))
# 96.47 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_Light_W,datatesting2_b))
# 98.58 Acuracy
# Temperature,Humidity, NS, WS
set.seed(1234)
ModelRF_ALL_b_Temperature_Humidity <- train(Occupancy~.-date-Light-HumidityRatio-CO2,method="rf",data=datatraining_b)
ModelRF_ALL_b_Temperature_Humidity
# Accuracy 0.9930
plot(ModelRF_ALL_b_Temperature_Humidity)
ModelRF_ALL_b_Temperature_Humidity$finalModel
varImp(ModelRF_ALL_b_Temperature_Humidity)
plot(varImp(ModelRF_ALL_b_Temperature_Humidity,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_Temperature_Humidity,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_Temperature_Humidity,datatesting_b))/dim(datatesting_b)[1]*100
# 96.67% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_Temperature_Humidity,datatesting_b))
# 94.67 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_Temperature_Humidity,datatesting2_b))
# 91.91 Acuracy
# Co2, NS, WS
set.seed(1234)
ModelRF_ALL_b_CO2 <- train(Occupancy~.-date-Light-HumidityRatio-Temperature-Humidity,method="rf",data=datatraining_b)
ModelRF_ALL_b_CO2
# Accuracy 0.9923
plot(ModelRF_ALL_b_CO2)
ModelRF_ALL_b_CO2$finalModel
varImp(ModelRF_ALL_b_CO2)
plot(varImp(ModelRF_ALL_b_CO2,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_CO2,datatraining_b))
#100.0
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_CO2,datatesting_b))/dim(datatesting_b)[1]*100
# 96.36% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_CO2,datatesting_b))
# 96.36 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_CO2,datatesting2_b))
# 65.2 Acuracy
# Temperature,NS, WS
set.seed(1234)
ModelRF_ALL_b_Temperature <- train(Occupancy~.-date-Light-Humidity-HumidityRatio-CO2,method="rf",data=datatraining_b)
ModelRF_ALL_b_Temperature
# Accuracy 0.99
plot(ModelRF_ALL_b_Temperature)
ModelRF_ALL_b_Temperature$finalModel
varImp(ModelRF_ALL_b_Temperature)
plot(varImp(ModelRF_ALL_b_Temperature,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_Temperature,datatraining_b))
#99.88
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_Temperature,datatesting_b))/dim(datatesting_b)[1]*100
# 95.08% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_Temperature,datatesting_b))
# 95.08 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_Temperature,datatesting2_b))
# 92.52 Acuracy
# Light,NS, WS
set.seed(1234)
ModelRF_ALL_b_Light <- train(Occupancy~.-date-Temperature-Humidity-HumidityRatio-CO2,method="rf",data=datatraining_b)
ModelRF_ALL_b_Light
# Accuracy 0.991
plot(ModelRF_ALL_b_Light)
ModelRF_ALL_b_Light$finalModel
varImp(ModelRF_ALL_b_Light)
plot(varImp(ModelRF_ALL_b_Light,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining_b$Occupancy,predict(ModelRF_ALL_b_Light,datatraining_b))
#99.88
set.seed(1234)
sum(datatesting_b$Occupancy==predict(ModelRF_ALL_b_Light,datatesting_b))/dim(datatesting_b)[1]*100
# 96.99% acuracy
set.seed(1234)
confusionMatrix(datatesting_b$Occupancy,predict(ModelRF_ALL_b_Light,datatesting_b))
# 97.00 Acuracy
set.seed(1234)
confusionMatrix(datatesting2_b$Occupancy,predict(ModelRF_ALL_b_Light,datatesting2_b))
# 97.81 Acuracy
# First obtaining the random forest models
set.seed(1234)
ModelRF_ALL <- train(Occupancy~.-date,method="rf",data=datatraining)
ModelRF_ALL
# Accuracy 0.993
plot(ModelRF_ALL)
ModelRF_ALL$finalModel
varImp(ModelRF_ALL)
plot(varImp(ModelRF_ALL,scale=TRUE))
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_ALL,datatraining))
#100.0
sum(datatesting$Occupancy==predict(ModelRF_ALL,datatesting))/dim(datatesting)[1]*100
# 95.05% acuracy
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_ALL,datatesting))
# 0.9505 Acuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_ALL,datatesting2))
# 0.9716 Acuracy
## without Light
set.seed(1234)
ModelRF_noLight <- train(Occupancy~.-date-Light,method="rf",data=datatraining)
ModelRF_noLight
# Accuracy 0.9864
plot(ModelRF_noLight)
ModelRF_noLight$finalModel
varImp(ModelRF_noLight)
plot(varImp(ModelRF_noLight))
set.seed(1234)
sum(datatesting$Occupancy==predict(ModelRF_noLight,datatesting))/dim(datatesting)[1]*100
# 69.3059% acuracy
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_noLight,datatraining))
# 0.9998
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_noLight,datatesting))
# 0.6863 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_noLight,datatesting2))
# 0.3268 Accuracy
## without CO2
set.seed(1234)
ModelRF_noCO2 <- train(Occupancy~.-date-CO2,method="rf",data=datatraining)
ModelRF_noCO2
# Accuracy 0.9925
plot(ModelRF_noCO2)
ModelRF_noCO2$finalModel
varImp(ModelRF_noCO2)
plot(varImp(ModelRF_noCO2))
set.seed(1234)
sum(datatesting$Occupancy==predict(ModelRF_noCO2,datatesting))/dim(datatesting)[1]*100
# 93.9962 % acuracy
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_noCO2,datatraining))
#99.95
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_noCO2,datatesting))
# .9403 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_noCO2,datatesting2))
# .9673 Accuracy
## without Light and CO2
set.seed(1234)
ModelRF_noCO2noLight <- train(Occupancy~.-date-CO2-Light,method="rf",data=datatraining)
ModelRF_noCO2noLight
# Accuracy 0.9697
plot(ModelRF_noCO2noLight)
ModelRF_noCO2noLight$finalModel
varImp(ModelRF_noCO2noLight)
plot(varImp(ModelRF_noCO2noLight))
set.seed(1234)
sum(datatesting$Occupancy==predict(ModelRF_noCO2noLight,datatesting))/dim(datatesting)[1]*100
# 75.68 % acuracy
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_noCO2noLight,datatraining))
#99.36
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_noCO2noLight,datatesting))
# 0.7568 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_noCO2noLight,datatesting2))
# 0.6259 Accuracy
#
## without Light and CO2 and HUR
set.seed(1234)
ModelRF_noCO2noLightnoHumidityRatio <- train(Occupancy~.-date-CO2-Light-HumidityRatio
,method="rf",data=datatraining)
ModelRF_noCO2noLightnoHumidityRatio
# 0.968753
ModelRF_noCO2noLightnoHumidityRatio$finalModel$xNames
plot()
ModelRF_noCO2noLightnoHumidityRatio$finalModel
varImp(ModelRF_noCO2noLightnoHumidityRatio)
plot(varImp(ModelRF_noCO2noLightnoHumidityRatio))
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_noCO2noLightnoHumidityRatio,datatraining))
# 99.36
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_noCO2noLightnoHumidityRatio,datatesting))
# 0.7565 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_noCO2noLightnoHumidityRatio,datatesting2))
# 0.673 Accuracy
## RF CO2 and Temperature
set.seed(1234)
ModelRF_CO2andTemp <- train(Occupancy~.-date-Humidity-Light-HumidityRatio
,method="rf",data=datatraining)
ModelRF_CO2andTemp
# 0.9633583
ModelRF_CO2andTemp$finalModel$xNames
plot()
ModelRF_CO2andTemp$finalModel
varImp(ModelRF_CO2andTemp)
plot(varImp(ModelRF_CO2andTemp))
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_CO2andTemp,datatraining))
# 99.88
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_CO2andTemp,datatesting))
# 0.7531 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_CO2andTemp,datatesting2))
# 0.3693 Accuracy
# Model Random forest (only Temperature)
set.seed(1234)
ModelRF_Temp <- train(Occupancy~.-date-CO2-Light-HumidityRatio-Humidity
,method="rf",data=datatraining)
ModelRF_Temp
# 0.86305
ModelRF_Temp$finalModel$xNames
plot()
ModelRF_Temp$finalModel
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_Temp,datatraining))
# 87.87
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_Temp,datatesting))
# 0.7073 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_noCO2noLightnoHumidityRatio,datatesting2))
# 0.673 Accuracy
# Model Random forest (only LIGHT)
set.seed(1234)
ModelRF_Light<- train(Occupancy~.-date-CO2-Temperature-HumidityRatio-Humidity
,method="rf",data=datatraining)
ModelRF_Light
# 0.9826
ModelRF_Light$finalModel$xNames
plot()
ModelRF_Temp$finalModel
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_Light,datatraining))
# 0.992
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_Light,datatesting))
# 0.9568 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_Light,datatesting2))
# 0.9791 Accuracy
# Model Random forest (only CO2)
set.seed(1234)
ModelRF_CO2 <- train(Occupancy~.-date-Light-Temperature-HumidityRatio-Humidity
,method="rf",data=datatraining)
ModelRF_CO2
# 0.9826
ModelRF_CO2$finalModel$xNames
plot()
ModelRF_Temp$finalModel
set.seed(1234)
confusionMatrix(datatraining$Occupancy,predict(ModelRF_CO2,datatraining))
# 0.9753
set.seed(1234)
confusionMatrix(datatesting$Occupancy,predict(ModelRF_CO2,datatesting))
# 0.7876 Accuracy
set.seed(1234)
confusionMatrix(datatesting2$Occupancy,predict(ModelRF_CO2,datatesting2))
# 0.6421 Accuracy