-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.R
169 lines (127 loc) · 4.28 KB
/
models.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
#HEADER#
#WifiPositioningProject- Only Training Data
#Loading Libraries
library(plyr)
library(caret)
library(dplyr)
library(lubridate)
library(ggplot2)
library(e1071)
library(rpart)
#read data
trainingData <- read.csv("data/trainingData.csv", sep = ",")
dataValidation <- read.csv("data/validationData.csv", sep = ",")
#no connection = -105
trainingData[trainingData == 100] <- -105
#Eliminating Duplicates
trainingData <- distinct(trainingData)
# Transforming Variables
factors <- c("FLOOR", "BUILDINGID", "SPACEID", "RELATIVEPOSITION", "USERID", "PHONEID")
trainingData[factors] <- lapply(trainingData[factors], factor)
numeric <- c("LONGITUDE", "LATITUDE")
trainingData[numeric] <- lapply(trainingData[numeric], as.numeric)
trainingData$TIMESTAMP <- as_datetime(trainingData$TIMESTAMP)
## NZV
remove_cols <- nearZeroVar(
trainingData,
freqCut = 1500,
uniqueCut = 0.1,
saveMetrics = FALSE,
names = FALSE,
foreach = FALSE,
allowParallel = TRUE
)
trainingNzv <- trainingData %>%
select(-remove_cols)
#selecting only numerical values
pcaDf <- trainingNzv %>%
select(starts_with("WAP"),
LONGITUDE,
LATITUDE)
#CorerelatedPredictors
desCor <- cor(pcaDf)
highCorr <- sum(abs(desCor[upper.tri(desCor)]) > .950)
summary(desCor[upper.tri(desCor)])
highlyCor <- findCorrelation(desCor, cutoff = .95)
pcaDf <- pcaDf[,-highlyCor]
desCor2 <- cor(pcaDf)
summary(desCor2[upper.tri(desCor2)])
#SAMPLING
sample <- pcaDf %>%
sample_n(1000) %>%
select(starts_with("WAP"), LATITUDE, LONGITUDE)
#Splitting Data
#select 75/100 for training the model
set.seed(300)
indxTrain <- createDataPartition(y = sample$LATITUDE,
p = 0.75,
list = FALSE)
training <- sample[indxTrain,]
testing <- sample[-indxTrain,]
###KNN###
my_knn_model<- train(LATITUDE ~ .,
method = "knn",
trControl = trainControl(method = "repeatedcv",
number = 10,
repeats = 10),
data = training,
tuneGrid = expand.grid(k = 2))
ctrl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 10)
model <- train(LATITUDE ~ ., data = training, method = "knn",
preProcess = c('zv', 'pca'),
trControl = ctrl,
tuneGrid = expand.grid(k = 2))
#another knn model
set.seed(3527)
subjects <- sample(1:20, size = 80, replace = TRUE)
folds <- groupKFold(subjects, k = 15)
my_knn_model_2<- train(LATITUDE ~ .,
method = "knn",
trControl = trainControl(method = "repeatedcv",
number = 10,
repeats = 10,
index = folds),
data = training,
tuneGrid = expand.grid(k = 2))
#SVR#
#Fitting the SVR to the dataset
regressor <- svm(formula = LATITUDE ~ .,
data = training,
type = "eps-regression",
cross = 7)
y_pred <- predict(regressor, testing)
set.seed(825)
svmFit <- train(LATITUDE ~ ., data = training,
method = "svmRadial",
preProcess = "pca",
trControl = ctrl,
tuneLength = 8)
#DECISION TREE REGRESSION#
#Fitting Decision Tree
regressor_decision <- rpart(formula = LATITUDE ~.,
data = training)
y_pred <- predict(regressor_decision, testing)
#Bayesian Regularized Neural Networkds
set.seed(805)
bayesianFit <- train(LATITUDE ~ ., data = training,
method = "bayesglm",
trControl = ctrl,
tuneLength = 8)
###POLYNOMINAL REGRESSION###
#SAMPLING
samplePN <- train_set %>%
sample_n(1000) %>%
select(starts_with("WAP"), LATITUDE)
sampleTest <- test_set %>%
sample_n(1000) %>%
select(starts_with("WAP"), LATITUDE)
# sampleTest <- test_set
reg <- lm(formula = LATITUDE ~.,
data = samplePN)
ggplot() +
geom_point(aes(x = samplePN$WAP001,
y = samplePN$LATITUDE)) +
geom_line(aes(x = samplePN$WAP001,
y = predict(reg, newdata = samplePN)))