-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassification and Regression models.r
363 lines (269 loc) · 11.1 KB
/
Classification and Regression 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
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
#So we are building classification model based on the variable "Poraba"
modelEval <- function( data, formula, modelFun, evalFun, fType, modelType,
toPrune=F) {
localTrain <- rep(F, times = nrow(goodInfo))
outPut <- vector()
for(i in 1:11) {
cat("ITERATION: ", i, "\n")
flush.console()
localTrain <- localTrain | goodInfo$month == i
localTest <- goodInfo$month == i + 1
model <- modelFun(formula, data[localTrain,])
if(toPrune) {
model <- modelFun(formula, data=data[localTrain,], cp=0)
tab <- printcp(model)
row <- which.min(tab[,"xerror"])
th <- mean(c(tab[row, "CP"], tab[row-1, "CP"]))
model <- prune(model, cp=th)
}
if(modelType == "class") {
observed <- data$norm_poraba[localTest]
obsMat <- class.ind(data$norm_poraba[localTest])
predicted <- predict(model, data[localTest,], type="class")
predMat <- predict(model, data[localTest,], type = fType)
if(evalFun == "CA") {
outPut[i] <- CA(observed, predicted)
}
else if(evalFun == "brier") {
outPut[i] <- brier.score(obsMat, predMat)
}
}
else if (modelType == "reg") {
predicted <- predict(model, data[localTest,])
observed <- data$poraba[localTest]
if (evalFun == "rmse") {
outPut[i] <- rmse(observed, predicted, mean(data$poraba[localTrain]))
}
else if (evalFun == "rmae") {
outPut[i] <- rmae(observed, predicted, mean(data$poraba[localTrain]))
}
else if (evalFun == "mae") {
outPut[i] <- mae(observed, predicted)
}
else if (evalFun == "mse") {
outPut[i] <- mse(observed, predicted)
}
}
}
outPut
}
modelEvalKNN <- function(data, formula, evalFun) {
localTrain <- rep(F, times = nrow(goodInfo))
outPut <- vector()
for(i in 1:11) {
cat("ITERATION: ", i, "\n")
flush.console()
localTrain <- localTrain | goodInfo$month == i
localTest <- goodInfo$month == i + 1
model <- kknn(formula, data[localTrain,], data[localTest,], k = 5)
predicted <- fitted(model)
observed <- data$poraba[localTest]
if (evalFun == "rmse") {
outPut[i] <- rmse(observed, predicted, mean(data$poraba[localTrain]))
}
else if (evalFun == "rmae") {
outPut[i] <- rmae(observed, predicted, mean(data$poraba[localTrain]))
}
else if (evalFun == "mae") {
outPut[i] <- mae(observed, predicted)
}
else if (evalFun == "mse") {
outPut[i] <- mse(observed, predicted)
}
}
outPut
}
porabat <- read.csv("data.csv", sep=",", stringsAsFactors = T)
porabat <- na.omit(porabat)
porabat$datum <- NULL
porabat$poraba <- NULL
porabat$avgPoraba<-NULL
porabat$maxPoraba<-NULL
porabat$minPoraba<-NULL
porabat$sumPoraba<-NULL
porabat$month<-NULL
porabat$X <- NULL
porabat$ura <- as.factor(porabat$ura)
set.seed(0)
samplec<- sample(1:nrow(porabat), size = as.integer(nrow(porabat) * 0.7), replace = F)
train<- porabat[samplec,]
test<- porabat[-samplec,]
table(train$norm_poraba)
table(test$norm_poraba)
library(nnet)
obsMat <- class.ind(test$norm_poraba)
library(caret)
observed <- test$norm_poraba
CA <- function(observed, predicted)
{
mean(observed == predicted)
}
brier.score <- function(observedMatrix, predictedMatrix)
{
sum((observedMatrix - predictedMatrix) ^ 2) / nrow(predictedMatrix)
}
#classification model 1 decision tree
library(rpart)
dt <- rpart(norm_poraba ~ ., data = train, cp =0)
printcp(dt)
tab <- printcp(dt)
row <- which.min(tab[,"xerror"])
th <- mean(c(tab[row, "CP"], tab[row-1, "CP"]))
th
dt <- prune(dt, cp=th)
predicted <- predict(dt, test, type="class")
CA(observed, predicted) #0.815472
predMat <- predict(dt, test, type = "prob")
brier.score(obsMat, predMat) #0.2818836
###
dt1 <- rpart(norm_poraba ~ namembnost + leto_izgradnje + povrsina, data = train, cp = 0)
printcp(dt1)
tab1 <- printcp(dt1)
row1 <- which.min(tab1[,"xerror"])
th1 <- mean(c(tab1[row1, "CP"], tab1[row1-1, "CP"]))
th1
dt1 <- prune(dt1, cp=th1)
predicted1 <- predict(dt1, test, type="class")
CA(observed, predicted1) #0.6408792
predMat1 <- predict(dt1, test, type = "prob")
brier.score(obsMat, predMat1) #0.4768113
modelEval(data = porabat, formula = as.formula(norm_poraba ~ .), modelFun = rpart,
evalFun = "brier", fType = "prob", modelType = "class", toPrune = T)
modelEval(data = porabat, formula = as.formula(norm_poraba ~ namembnost + leto_izgradnje + povrsina),
modelFun = rpart, evalFun = "brier", fType = "prob", modelType = "class", toPrune = T)
#Classification model 2 with random forest
library(CORElearn)
rf <- CoreModel(norm_poraba ~ ., data = train, model="rf")
predicted <- predict(rf, test, type="class")
CA(observed, predicted) #0.8312868
predMat <- predict(rf, test, type = "prob")
brier.score(obsMat, predMat) #0.2446596
###
rf1 <- CoreModel(norm_poraba ~ namembnost + leto_izgradnje + povrsina, data = train, model="rf")
predicted1 <- predict(rf1, test, type="class")
CA(observed, predicted1) #0.6408792
predMat1 <- predict(rf1, test, type = "prob")
brier.score(obsMat, predMat1) #0.4557486
modelEval(data = porabat, formula = as.formula(norm_poraba ~ .), modelFun = CoreModel,
evalFun = "brier", fType = "prob", modelType = "class")
modelEval(data = porabat, formula = as.formula(norm_poraba ~ namembnost + leto_izgradnje + povrsina), modelFun = CoreModel,
evalFun = "brier", fType = "prob", modelType = "class")
#Classification model 3 with naive Bayes
library(e1071)
nb <- naiveBayes(norm_poraba ~ ., data = train)
predicted <- predict(nb, test, type="class")
CA(observed, predicted) #0.3799943
predMat <- predict(nb, test, type = "raw")
brier.score(obsMat, predMat) #0.7411347
###
nb1 <- naiveBayes(norm_poraba ~ namembnost + leto_izgradnje + povrsina, data = train)
predicted1 <- predict(nb1, test, type="class")
CA(observed, predicted1) #0.3734106
predMat1 <- predict(nb1, test, type = "raw")
brier.score(obsMat, predMat1) #0.7360255
modelEval(data = porabat, formula = as.formula(norm_poraba ~ namembnost + leto_izgradnje + povrsina), modelFun = naiveBayes,
evalFun = "brier", fType = "raw", modelType = "class")
modelEval(data = porabat, formula = as.formula(norm_poraba ~ .), modelFun = naiveBayes,
evalFun = "brier", fType = "raw", modelType = "class")
##############################################Regresija############################################################################
# linearna regresija
porabac <- read.csv("data.csv", sep=",", stringsAsFactors = T)
porabac <- na.omit(porabac)
porabac$datum <- NULL
porabac$norm_poraba<-NULL
porabac$month<-NULL
porabac$season<-NULL
porabac$X <- NULL
porabac$weather <- NULL
porabac$ura <- as.factor(porabac$ura)
set.seed(0)
samplec<- sample(1:nrow(porabac), size = as.integer(nrow(porabac) * 0.7), replace = F)
train<- porabac[samplec,]
test<- porabac[-samplec,]
rmae <- function(obs, pred, mean.val)
{
sum(abs(obs - pred)) / sum(abs(obs - mean.val))
}
rmse <- function(obs, pred, mean.val)
{
sum((obs - pred)^2)/sum((obs - mean.val)^2)
}
mae <- function(obs, pred)
{
mean(abs(obs - pred))
}
mse <- function(obs, pred)
{
mean((obs - pred)^2)
}
##########################################
meanVal <- mean(train$poraba)
meanVal
predTrivial <- rep(meanVal, nrow(test))
#Linear regression model 1
model <- lm(poraba ~ ., train)
predicted <- predict(model, test)
observed <- test$poraba
rmse(observed, predicted, mean(train$poraba)) #0.06430346
rmae(observed, predicted, mean(train$poraba)) #0.1989883
###
model1 <- lm(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba, train)
predicted <- predict(model1, test)
observed <- test$poraba
rmse(observed, predicted, mean(train$poraba)) #0.0713572
rmae(observed, predicted, mean(train$poraba)) #0.1962335
modelEval(data = porabac, formula = as.formula(poraba ~ .), modelFun = lm,
evalFun = "rmse", fType = "", modelType = "reg")
modelEval(data = porabac, formula = as.formula(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba),
modelFun = lm, evalFun = "rmse", fType = "", modelType = "reg")
#################################################################################################
#Regression model 2 regression tree
library(rpart)
library(rpart.plot)
rt.model <- rpart(poraba ~ ., data=train)
predicted <- predict(rt.model, test)
rmse(test$poraba, predicted, mean(train$poraba)) #0.09481967
rmae(test$poraba, predicted, mean(train$poraba)) #0.2785586
###
rt.model1 <- rpart(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba, data=train)
predicted <- predict(rt.model1, test)
rmse(test$poraba, predicted, mean(train$poraba)) #0.09481967
rmae(test$poraba, predicted, mean(train$poraba)) #0.2785586
modelEval(data = porabac, formula = as.formula(poraba ~ .), modelFun = rpart,
evalFun = "rmse", fType = "", modelType = "reg")
modelEval(data = porabac, formula = as.formula(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba),
modelFun = rpart, evalFun = "rmse", fType = "", modelType = "reg")
#Regression model 3 nevronska mreza
library(nnet)
set.seed(0)
min_vals <- apply(train[c(3, 5:14, 18:21)], 2, min)
max_vals <- apply(train[c(3, 5:14, 18:21)], 2, max)
normTrain <- as.data.frame(scale(train[c(3, 5:14, 18:21)], center = min_vals, scale = max_vals - min_vals))
normTrain$poraba <- train$poraba
normTest <- as.data.frame(scale(test[c(3, 5:14, 18:21)], center = min_vals, scale = max_vals - min_vals))
normTest$quality <- test$quality
nn.model <- nnet(poraba ~ ., normTrain, size = 5, decay = 0.0001, maxit = 10000, linout = T)
predicted <- predict(nn.model, normTest)
rmse(test$poraba, predicted, mean(normTrain$poraba)) #0.06995541
rmae(test$poraba, predicted, mean(normTrain$poraba)) #0.1985665
###
nn.model1 <- nnet(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba,
normTrain, size = 5, decay = 0.0001, maxit = 10000, linout = T)
predicted <- predict(nn.model1, normTest)
rmse(test$poraba, predicted, mean(normTrain$poraba)) #0.07099577
rmae(test$poraba, predicted, mean(normTrain$poraba)) #0.195312
##############k nearest neighbor######################################################################
library(kknn)
knn.model <- kknn(poraba ~ ., train, test, k = 5)
predicted <- fitted(knn.model)
rmse(test$poraba, predicted, mean(train$poraba)) #0.05474145
rmae(test$poraba, predicted, mean(train$poraba)) #0.1970113
###
knn.model1 <- kknn(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba, train, test, k = 5)
predicted <- fitted(knn.model1)
rmse(test$poraba, predicted, mean(train$poraba)) #0.08148342
rmae(test$poraba, predicted, mean(train$poraba)) #0.211432
modelEvalKNN(data = porabac, formula = as.formula(poraba ~ .), evalFun = "rmse")
modelEvalKNN(data = porabac,
formula = as.formula(poraba ~ maxPoraba + avgPoraba + minPoraba + sumPoraba),
evalFun = "rmse")