-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverFunctions.r
executable file
·965 lines (582 loc) · 20.9 KB
/
serverFunctions.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
library(rmongodb)
library(ISLR)
library(pracma)
library(dplyr)
library(jsonlite)
library(reshape2)
library(e1071)
library(kknn)
library(cluster)
library(caret)
library(nnet)
library(neuralnet)
library(gridExtra)
library(rpart)
library(RWeka)
#
#Function to get a acquisition set from a localization test in mongo, which is stored there by some cellphone using the system
#
#
#
#Args:
# queueID: ID from objet with RSSIs and BSSIDs stored by the cellphone on mongo
#
#Output:
# list with measure in the format specified by the SingleTest funcion
#
#
#
getMeasureFromMongo <- function(queueID){
mongo <- mongo.create(host="localhost:27017",username="netmap",password = "brocoliéumvegetal")
if(mongo.is.connected(mongo) == TRUE){
db <- "server_api_production"
queue <- paste(db,"queued_classifications",sep = ".")
#retrive queue that match ID
results<-mongo.find.one(mongo,queue,query=list('_id'=mongo.oid.from.string(queueID)))
#transform in to an R list
results <- mongo.bson.to.list(results)
dataVector <- results$access_points
BSSIDlist <- unlist(lapply(dataVector,function(x) return( x$BSSID )))
RSSIlist <- unlist(lapply(dataVector,function(x) return( x$RSSI )))
#Reshape measure to the format to be worked by the single test function
transposedData <- matrix(nrow=1,ncol=length(BSSIDlist))
transposedData <- data.frame(transposedData)
names(transposedData) <- BSSIDlist
transposedData[1,] <- RSSIlist
return(transposedData)
} else{
print("Could not connect to mongo!")
return("ERROR")
}
}
#
#Function to to be called locally to test our trained models with data
#
#
#
#Args:
# facilityName: Name of facility to be tested
#
#Output:
# Error Rate
#
#
#
#
#
testRealModels<-function(facilityName,savePath,nameGraph){
mongo <- mongo.create(host="52.67.171.39:27017",username="netmap",password = "brocoliéumvegetal")
if(mongo.is.connected(mongo) == TRUE){
db <- "server_api_production"
facilities <- paste(db,"facilities",sep = ".")
#retrive list of facilities that match query and get 1st element which is the ID
results<-mongo.find.all(mongo,facilities,list(name=facilityName))
if(length(results)==0){
print("Could not find that facility name")
return()
}else{
facilityID <- results[[1]][[1]]
}
pathData <- paste("prepared-data/",facilityID,".rds",sep="")
#get datasets so we can use the train set in the KNN prediction
data <- readRDS(pathData)
idZ <- data$idZ
data <- dplyr::select(data,-idZ)
#NON PCA SCALING
preProc <- caret::preProcess(data)
scaled <- predict(preProc, data)
scaled <- cbind(idZ,scaled)
#get possible idZs
factors <- levels(scaled$idZ)
#names of features used for training
names <- names(data)[-1]
#split train and test sets
index <- sample(1:nrow(scaled),round(0.8*nrow(scaled)))
#Train and test SCALED
train_s <- scaled[index,]
test_s <- scaled[-index,]
#train SMO
SMOmodel <- SMO(idZ~.,data=train_s)
#train Adaboost + tree
treeAdamodel <- AdaBoostM1(idZ~. , data = train_s ,control = Weka_control(W = list(J48, M=5)))
#KNN TRAIN
knnModel<-kknn(formula=idZ ~. , k=4,distance=1, train=train_s,test=test_s,kernel="optimal")
listaModelos <- list("SMO"=SMOmodel,"KNN"=knnModel,"treeAda"=treeAdamodel)
#initialize summed support vector
sumProb <- vector(mode="numeric",length = length(factors))
algoRates <- NULL
for (model in listaModelos){
temp <- predictionWrapper(model,test_s,probabilities=TRUE)
resultsIDZt <- factors[apply (temp,1,function(x) which.max(x))]
rateSuccesst <- 100*mean(resultsIDZt==test_s$idZ)
algoRates <- cbind(algoRates,rateSuccesst)
#WEIGHTED VOTING RULE
sumProb <- sumProb + temp
}
resultsIDZ <- factors[apply (sumProb,1,function(x) which.max(x))]
rateSuccess <- 100*mean(resultsIDZ==test_s$idZ)
algoRates <- cbind(algoRates,rateSuccess)
fullPath = paste(savePath,"/",nameGraph,".jpeg",sep = "")
jpeg(fullPath, width = 2000, height = 1700, units = 'px', res = 300)
par(mar=c(6,9,5,3)) # increase y-axis margin.
par(las=2) # make label text perpendicular to axis
name <- paste("Taxa de acerto (%)",nameGraph)
barplot(algoRates,xlim = c(90,100),xpd = FALSE,main = name,horiz = TRUE,names.arg=c("SMO", "KNN", "Arvore + Adaboost","Votação Ponderada"))
dev.off()
return(rateSuccess)
}else{
return("error: Could not connect to mongo")
}
}
#prepare UCI dataset, first by findind it in "path"
#then, take only measures specified by floor and building
#justInside is a boolean that specifies if the data points should be the ones measured INSIDE the rooms
prepareUCIdata2 <- function (path,building,floor,zones=NULL,justInside=FALSE){
#building: 0, 1 , 2
#floor : 0,1,2,3,4
filePath <- file.path(path,"trainingData.csv")
dataset <- read.csv(filePath,header = TRUE,sep=",")
if(is.null(zones) && justInside==FALSE ){ fdataset<-dplyr::filter(dataset,FLOOR==floor, BUILDINGID == building)}
else if (!is.null(zones) && justInside==FALSE) { fdataset<-dplyr::filter(dataset,FLOOR==floor, BUILDINGID == building,SPACEID%in%zones) }
else if (!is.null(zones) && justInside==TRUE){fdataset<-dplyr::filter(dataset,FLOOR==floor, BUILDINGID == building,SPACEID%in%zones,RELATIVEPOSITION ==1)}
else if (is.null(zones) && justInside==TRUE){fdataset<-dplyr::filter(dataset,FLOOR==floor, BUILDINGID == building,RELATIVEPOSITION ==1)}
zonas <- unique(fdataset$SPACEID)
assign("zonas",zonas,.GlobalEnv)
names(fdataset)[525] <- "idZ"
tidyData <- dplyr::select(fdataset,WAP001:WAP520,idZ)
tidyData$idZ <- as.factor(tidyData$idZ)
#Eliminate useless features
bol <- tidyData == 100
tidyData[bol] = -120
discard <- NULL
for (col in 1:ncol(bol)){
if( mean( bol[,col]) == 1){
discard <- cbind(discard,col)
}
}
#remove all entries from discard list
tidyData <- tidyData[,-discard]
# Scaling data
idZ <- tidyData$idZ
tidyData <- dplyr::select(tidyData,-idZ) + 120
tidyData <- cbind(idZ,tidyData)
#PCA .95 threshold
PCA <- caret::preProcess(dplyr::select(tidyData,-idZ),method=c("center","scale","pca"))
#save PCA parameters for future conversion
assign("PCA",PCA,.GlobalEnv)
saveRDS(PCA,"pca.rds")
#project data into PCA space
scaledPCA <- predict(PCA, dplyr::select(tidyData,-idZ))
#IDZ IS NOW ON INDEX 1! REMEMBER THAT FOR GOD'S SAKE!
scaledPCA <- cbind(idZ,scaledPCA)
#NON PCA SCALING
preProc <- caret::preProcess(tidyData)
#saveRDS(preProc,"trainedModels/scale.rds")
#scaled <- predict(preProc, tidyData)
scaled <- tidyData
assign("preProc",preProc,.GlobalEnv)
#IDZ IS NOW ON INDEX 1! REMEMBER THAT FOR GOD'S SAKE!
scaled <- cbind(idZ,dplyr::select(scaled,-idZ))
assign("scaled",scaled,.GlobalEnv)
#set.seed(31415)
#TRAIN AND TEST SET SPLIT
index <- sample(1:nrow(tidyData),round(0.8*nrow(tidyData)))
#Train and test UNESCALED
train <- tidyData[index,]
test <- tidyData[-index,]
assign("train",train,.GlobalEnv)
assign("test",test,.GlobalEnv)
#Train and test SCALED
train_s <- scaled[index,]
test_s <- scaled[-index,]
assign("train_s",train_s,.GlobalEnv)
assign("test_s",test_s,.GlobalEnv)
#Train and test in PCA space
train_pca <- scaledPCA[index,]
test_pca <- scaledPCA[-index,]
assign("train_pca",train_pca,.GlobalEnv)
assign("test_pca",test_pca,.GlobalEnv)
dataList <- list("train" = train, "train_s" = train_s,"train_pca" = train_pca,"test" =test, "test_s" = test_s,"test_pca" = test_pca)
return(dataList)
}
#
#REMOVE ZONE ID FROM VECTOR
#
#WEIGHTED VOTE
#
#TEST VECTOR FORMAT:
#RSSID1 RSSID2 RSSID3 ...
# -30 -39 -29
#
#
#
#
#Use trained models to provide a single classification answer from testVector
#
#
#
#Args:
# testVector: The format must be as specified below:
#
# RSSID1 RSSID2 RSSID3 ...
# -30 -39 -29
#
# train: dataset that was used to train the models
#
# modelsList: List with the trained models
#Output:
# predicted zone ID
#
#
#
#
#
prediction.from.models <- function(testVector,train,modelsList){
#get possible idZs
factors <- levels(train$idZ)
#names of features used for training
names <- names(train)[-1]
#creates dummy vector with BSSIDs used to train the classifier
dummyVector <- t(as.data.frame(x=rep(NA,length(names)),names))
#merge testVector with dummyVector in a way that if there is a BSSID missing in the testVector, it is created with -120
#commonNames <- intersect(names,names(testVector))
#diff <- outersect(commonNames,names(testVector))
#newAPs <- !(names(testVector) %in% commonNames)
#get values that are present in testVector
#print("Dummy Vector:")
#print(dummyVector)
#print("Test Vector:")
#print(testVector)
#print("-------------")
mergedVector <- merge(dummyVector,testVector,all.y=TRUE)
mergedVector[is.na(mergedVector)] <- -120
#scale new data!
#scaledVector <- predict(modelsList$preProc,mergedVector)
scaledVector <- mergedVector
print(setdiff(names(scaledVector),names(train)))
print("-------------")
print(scaledVector)
print("-------------")
#KNN TRAIN
knnModel<-kknn(formula=idZ ~. , k=4,distance=1, train=train,test=scaledVector,kernel="optimal")
listaModelos <- list("SMO"=modelsList$SMO,"KNN"=knnModel,"treeAda"=modelsList$Tree)
#initialize summed support vector
sumProb <- vector(mode="numeric",length = length(factors))
for (model in listaModelos){
temp <- predictionWrapper(model,scaledVector,probabilities=TRUE)
#WEIGHTED VOTING RULE
sumProb <- sumProb + temp
}
#results
print(which.max(sumProb))
print(factors[which.max(sumProb)])
confValue <- max(sumProb)
idZBayas <- factors[which.max(sumProb)]
print(idZBayas)
#return output zone_id
if(!is.na(idZBayas)){
zoneName <- aws.getNamefromID(idZBayas)
output <- list("ZonaName"=zoneName,"Confidence"=confValue)
jsonOut <- jsonlite::toJSON(output)
jsonOut <- gsub(pattern = "[",jsonOut,replacement="",fixed=TRUE)
jsonOut <- gsub(pattern = "]",jsonOut,replacement="",fixed=TRUE)
return(jsonOut)
}
return (idZBayas)
}
#
# Wrapper function to unify call to predition functions from the models used
#
#
#
#Args:
# model: model object in which the prediction is being made
# test: dataframe in which the test is going to be made
# can be just a single line or multiple ones
#
# probabilities: boolean to specify if the output is the class probabilities (TRUE) or
# just the class names (FALSE)
#
#
#
#Output:
# Prediction in the form of probabilities or class name
#
predictionWrapper<- function(model,test,probabilities=TRUE,...) {
if(grepl("SMO",model$call) || grepl("Ada",model$call) || grepl("J48",model$call)){
if(!probabilities){
pred <- predict(model,test)
}
else{
pred <- predict(model,test,type="probability")
}
} else if(grepl("neuralnet",model$call,fixed = TRUE)){
factors<- model$model.list$response
factors <- gsub("`",'',factors)
nnProb<-neuralnet::compute(model,test)$net.result
if(!probabilities){
nnPrediction <-apply(nnProb,1,function(x) which.max(x))
pred <- as.factor(as.numeric(as.character(factors[nnPrediction])))
levels(pred)<-factors
}
else{
pred<-nnProb
}
} else if(grepl("kknn",model$call,fixed = TRUE)){
if(!probabilities){
pred <- model$fitted.values
}else{
pred <- model$prob
}
} else if(grepl("svm",model$call,fixed=TRUE)){
if(!probabilities){
pred <- predict(model,test)
}else{
pred <- attr(predict(model,test,probability=TRUE),"probabilities")
}
}
return(pred)
}
#Get ID from a facility name in mongo
#
#Input: facility name
#
#
#Output: facility ID
#
#
aws.getIDfromName <- function(name){
print("Trying to connect to mongo...")
mongo <- mongo.create(host="localhost:27017",username="netmap",password = "brocoliéumvegetal")
if (mongo.is.connected(mongo) == TRUE) {
print("Succesfully connected to mongo")
db <- "server_api_production"
facilities <- paste(db,"facilities",sep = ".")
#retrive list of facilities that match query and get 1st element which is the ID
facilityID<-mongo.find.all(mongo,facilities,list(name=name))[[1]][[1]]
return (facilityID)
}
else{
print("Name not found on db!")
return()
}
}
#Get ID from a facility name in mongo
#
#Input: zone ID
#
#
#Output: zone name
#
#
aws.getNamefromID <- function(zoneID){
print("Trying to connect to mongo...")
mongo <- mongo.create(host="52.67.171.39:27017",username="netmap",password = "brocoliéumvegetal")
if (mongo.is.connected(mongo) == TRUE) {
print("Succesfully connected to mongo")
db <- "server_api_production"
zones <- paste(db,"zones",sep = ".")
#retrive all zones from that facility
zoneName <- mongo.find.all(mongo,zones,list('_id'=mongo.oid.from.string(zoneID)))[[1]][[2]]
return (zoneName)
}
else{
print("Name not found on db!")
return()
}
}
#
#
#Input: facilityID
#
#
#Output: Get dataset from mongo and prepare it for training
#
#
#* @get /prepare
aws.PrepareData <- function (facilityID){
#ec2-52-67-171-39.sa-east-1.compute.amazonaws.com
#aws.PrepareData("580264eabde5c6211d18821b")
print("Trying to connect to mongo...")
mongo <- mongo.create(host="localhost:27017",username="netmap",password = "brocoliéumvegetal")
if (mongo.is.connected(mongo) == TRUE) {
print("Succesfully connected to mongo")
db <- "server_api_production"
zones <- paste(db,"zones",sep = ".")
#retrive all zones from that facility
listZones <- mongo.find.all(mongo,zones,list(facility_id=facilityID))
rawData <- NULL
#for each zone found
for (z in 1:length(listZones)){
#get ID of that zone from list of lists
zoneID <- listZones[[z]][[1]]
acquisition <- paste(db,"acquisitions",sep = ".")
listAcquisitions <- mongo.find.all(mongo,acquisition,list(zone_id=zoneID))
lista <- NULL
if(length(listAcquisitions)>1){
for (a in 1:length(listAcquisitions)){
listAP <- listAcquisitions[[a]]$access_points
acquiID <- listAcquisitions[[a]]$`_id`
#get relevant informantion from list
temp <- lapply(listAP,function(x) return (c(x$BSSID,x$RSSI)))
#reshape list and add relevant IDs
temp2<- lapply(temp,montaLista,zoneID=zoneID,acquiID=acquiID)
#create list in desirable format
#unlist lists
for (l in 1:length(temp2)){
lista <- rbind(lista,unlist(temp2[[l]]))
}
}
rawData <- rbind(rawData,lista)
}
else{
print("No point found for this zone! Passing by it")
}
}
#we DONT want strings to be turned to factors!
rawDataDF<-data.frame(rawData,stringsAsFactors = FALSE)
names(rawDataDF)<- c("BSSID","RSSI","idZ","acquiID")
rawDataDF$RSSI <- as.numeric(rawDataDF$RSSI)
molten <- melt(rawDataDF,id.vars=c("idZ","acquiID","BSSID"), value.name = "RSSI",measure.vars = c("RSSI"))
molten$variable <- NULL
#gambiarra
#molten <- molten[-14,]
attach(rawDataDF)
tidyData <- reshape2::dcast(molten, acquiID+ idZ ~ BSSID, value.var = 'RSSI')
detach(rawDataDF)
}else{
print("Could not connect to Mongo! DAMN IT LIRA")
return()
}
#convert to numbers
#zones <- as.numeric(args)
#source("serverFunctions.r")
#setwd("~/Documents/machinelearning_R")
dataPath <- "prepared-data"
#remove Aquisition ID, as we don't really need it from now on
tidyData<- tidyData[,-1]
#transform idZ into factor!
tidyData[,1] <- as.factor(tidyData[,1])
#REMOVE NAs
tidyData[is.na(tidyData)] <- -120
print (paste(dataPath,"/",facilityID,".rds",sep=""))
#save file with facilityID as name
saveRDS(tidyData,paste(dataPath,"/",facilityID,".rds",sep=""))
rm(list = setdiff(ls(), lsf.str()))
return("ok")
}
#
#Make a zone prediction based on input data from cellphone
#
#Args:
# jsonMeasure: object with measures and facility from which the trained models will be used
#
#Output:
# ID of zone
#
#
#* @post /singleTest
aws.SingleTest <- function (facility_id,access_points){
dataVector <- access_points
facilityID <- facility_id
BSSIDlist <- dataVector$BSSID
RSSIlist <- dataVector$RSSI
#row vector
transposedData <- matrix(nrow=1,ncol=length(BSSIDlist))
transposedData <- data.frame(transposedData)
names(transposedData) <- BSSIDlist
transposedData[1,] <- RSSIlist
#get queued measure from mongo
#transposedData <- getMeasureFromMongo(queueID)
pathModels <- paste("trained-models/",facilityID,".rds",sep="")
#get trained models
trainedModels <- readRDS(pathModels)
print("Got models!")
#deserialize Java J48 and SMO objects
invisible(rJava::.jstrVal(trainedModels$Tree$classifier))
invisible(rJava::.jstrVal(trainedModels$SMO$classifier))
pathData <- paste("prepared-data/",facilityID,".rds",sep="")
#get datasets so we can use the train set in the KNN prediction
dataset <- readRDS(pathData)
print("Got data!")
return(prediction.from.models(transposedData,dataset,trainedModels))
}
#* @get /train
aws.trainModels <- function (facilityID){
#setwd("~/Documents/machinelearning_R")
pathData <- paste("prepared-data/",facilityID,".rds",sep="")
tidyData <- readRDS(pathData)
#SCALE DATA
idZ <- tidyData$idZ
tidyData <- dplyr::select(tidyData,-idZ)
#NON PCA SCALING
preProc <- caret::preProcess(tidyData)
#scaled <- predict(preProc, tidyData)
scaled <- tidyData
scaled <- cbind(idZ,scaled)
#now, we train the models with data already scaled
trainedModels <- trainModels(scaled)
#save scaling object with models!
trainedModels<- c(trainedModels,"preProc"=list(preProc))
pathModels <- paste("trained-models/",facilityID,".rds",sep="")
saveRDS(trainedModels,pathModels)
rm(list = setdiff(ls(), lsf.str()))
return("ok")
}
#
#
#TRAIN MODELS WITH TRAIN SET IN FORMAT SPECIFIED IN ANOTHER FUNCTIONS
#
#
#
trainModels <- function(train){
#DECISION TREE
#tree <- J48(idZ~.,data=train)
treeAda <- AdaBoostM1(idZ~. , data = train ,control = Weka_control(W = list(J48, M=5)))
#serialize java object object
rJava::.jcache(treeAda$classifier)
#NEURALNETWORK
#transforms factors in binary dummy vectors
#ASSUMING IDZ IS IN COLUMN 1!!!!!!!!!
usingNN=FALSE
if(usingNN==TRUE){
nnData <- cbind(dplyr::select(train,-idZ),nnet::class.ind(train[,1]))
addq <- function(x) paste0("`", x, "`")
#adds `x` to every name in data
names(nnData) <- addq(names(nnData))
n <- names(nnData)
#gets indexes of dummy id columns
indexId <- grep("^[[:punct:]][[:digit:]]*[[:punct:]]$",n)
lhseq <- paste(names(nnData[,indexId]),collapse="+")
rhseq <- paste(names(nnData[,-indexId]),collapse="+")
#creates formula
f<-as.formula(paste(lhseq,rhseq,sep = " ~ "))
#for some reason, remove quotes and it works
nnData <- cbind(dplyr::select(train,-idZ),nnet::class.ind(train[,1]))
#TRAIN neuralnet!
neuron <- 210
nn <- neuralnet::neuralnet(f,data=nnData,hidden=c(neuron),linear.output=FALSE)
}
#assign("NeuralNet",nn,.GlobalEnv)
#saveRDS(nn,"NeuralNet.rds")
#SUPPORT VECTOR MACHINE
SMO <- SMO(idZ~.,data=train)
assign("SMO",SMO,.GlobalEnv)
rJava::.jcache(SMO$classifier)
#saveRDS(mylogit1,"SVM.rds")
if(usingNN){
modelList <- list("NeuralNet" = nn,"SMO" = SMO,"Tree" = treeAda)
}
else{
modelList <- list("SMO" = SMO,"Tree" = treeAda)
}
return (modelList)
}
#Function to be used as FUN argument in lapply
montaLista<- function(x,zoneID,acquiID){
return (list(BSSID=x[1],RSSI=x[2],idZ=zoneID,acquiID=acquiID))
}
outersect <- function(x, y) {
sort(c(setdiff(x, y),
setdiff(y, x)))
}