-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_02.R
209 lines (167 loc) · 7.34 KB
/
part_02.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
#------------------------
# Rukshan Dias - w1912792
#------------------------
# import libraries
library(readxl)
library(dplyr)
library(neuralnet)
library(Metrics)
#import dataset
data <- read_excel("G:/Other computers/My Computer/IIT/Level_02/2.ML/1. ML CW/uow_consumption.xlsx", sheet = 1) # opening the excel file with sheet no
####-----Preprocessing----####
# change col name
names(data)[2] <- 'six'
names(data)[3] <- 'seven'
names(data)[4] <- 'eight'
# change date to numeric
date <-factor(data$date)
date <-as.numeric(date)
date
# create data frame
uow_dataFrame <- data.frame(date,data$'six',data$'seven',data$'eight')
# create dataframe for 8
eight_column <- c(uow_dataFrame$data.eight)
plot(eight_column, type = "l")
# create I/O matrix
time_delayed_matrix <- bind_cols(t7 = lag(eight_column,8),
t4 = lag(eight_column,5),
t3 = lag(eight_column,4),
t2 = lag(eight_column,3),
t1 = lag(eight_column,2),
eightHour = eight_column)
# remove NA values
time_delayed_matrix <- na.omit(time_delayed_matrix)
# splitting data to train & test
train_data <- time_delayed_matrix[1:380,]
test_data <- time_delayed_matrix[381:nrow(time_delayed_matrix),]
#-- max min values
origin_min_val <- min(train_data)
origin_max_val <- max(train_data)
original_data_output <- test_data$eightHour
#---normalize---#
# min-max normalizing function
normalize <- function(x){
return ((x - min(x)) / (max(x) - min(x)))
}
# un-normalizing function
unnormalize <- function(x, min, max) {
return( (max - min)*x + min )
}
# apply Normalization
time_delayedNorm <- as.data.frame(lapply(time_delayed_matrix[1:ncol(time_delayed_matrix)], normalize))
#splitting data after normalize test data
train_dataNorm <- time_delayedNorm[1:380,]
test_dataNorm <- time_delayedNorm[381:nrow(time_delayed_matrix),]
#view before & after normalization
boxplot(time_delayed_matrix, main="before normalizing data") #plot before normalizing data
boxplot(time_delayedNorm, main="After normalizing data") #plot after normalization
# Creating testing data for every timeDelay
t1_testData <- as.data.frame(test_dataNorm[, c("t1")]) # use as.data.frame since there's only one column
t2_testData <- test_dataNorm[, c("t1", "t2")]
t3_testData <- test_dataNorm[, c("t1", "t2", "t3")]
t4_testData <- test_dataNorm[, c("t1", "t2", "t3", "t4")]
t7_testData <- test_dataNorm[, c("t1", "t2", "t3", "t4", "t7")]
# function to train AR model
trainModel <- function(formula, hiddenVal, isLinear=TRUE, actFunc="logistic"){
set.seed(1234)
nn <- neuralnet(formula,
data = train_dataNorm, hidden=hiddenVal, act.fct = actFunc, linear.output=isLinear)
plot(nn)
return(nn)
}
testModel <- function(nnModel, testing_df){
nnresults <- compute(nnModel, testing_df)
predicted <- nnresults$net.result
unnormalised_predicted <- unnormalize(predicted, origin_min_val, origin_max_val)
devia = ((original_data_output - unnormalised_predicted)/original_data_output) # calculate deviation
modelAccuracy = 1 - abs(mean(devia))
accuracy = round(modelAccuracy * 100 , digits = 2)
rmse = rmse(original_data_output, unnormalised_predicted)
mae = mae(original_data_output, unnormalised_predicted)
mape = mape(original_data_output, unnormalised_predicted)
smape = smape(original_data_output, unnormalised_predicted)
cat("Model Accuracy:", accuracy, "%\n")
cat("RMSE:", rmse, "\n")
cat("MAE:", mae, "\n")
cat("MAPE:", mape, "\n")
cat("sMAPE:", smape, "\n")
return(unnormalised_predicted)
}
# function to view weighted parameter count
view_weighted_para_count <- function(inputCount, hiddenVals){
weight_para_count = (inputCount + 1) * hiddenVals[1]
for (i in 1:length(hiddenVals)) {
if(i==length(hiddenVals)) {
weight_para_count = weight_para_count + (hiddenVals[i] + 1) * 1
} else{
weight_para_count = weight_para_count + ((hiddenVals[i] + 1) * hiddenVals[i+1])
}
}
cat("Weighted parameter count for ",inputCount,"inputs with",length(hiddenVals),
"hidden layers -> ",weight_para_count,"\n")
return(weight_para_count)
}
# t1
train_t1 <- trainModel(eightHour ~ t1, c(5))
test_t1_predict <- testModel(train_t1, t1_testData)
view_weighted_para_count(inputCount=1 ,hiddenVals=c(5))
train_t1_02 <- trainModel(eightHour ~ t1, c(2,3), isLinear = FALSE, "tanh")
test_t1_predict_02 <- testModel(train_t1_02, t1_testData)
view_weighted_para_count(inputCount=1 ,hiddenVals=c(2,3))
# t2
train_t2 <- trainModel(eightHour ~ t1 + t2, c(5))
test_t2_predict <- testModel(train_t2, t2_testData)
view_weighted_para_count(inputCount=2 ,hiddenVals=c(5))
# t3
train_t3_01 <- trainModel(eightHour ~ t1 + t2 + t3, c(5))
test_t3_predict <- testModel(train_t3_01, t3_testData)
view_weighted_para_count(inputCount=3 ,hiddenVals=c(5))
train_t3_02 <- trainModel(eightHour ~ t1 + t2 + t3, c(15), isLinear = FALSE)
test_t3_predict_02 <- testModel(train_t3_02, t3_testData)
view_weighted_para_count(inputCount=3 ,hiddenVals=c(15))
train_t3_03 <- trainModel(eightHour ~ t1 + t2 + t3, c(6,4))
test_t3_predict_03 <- testModel(train_t3_03, t3_testData)
view_weighted_para_count(inputCount=3 ,hiddenVals=c(6,4))
# t4
train_t4_01 <- trainModel(eightHour ~ t1 + t2 + t3 + t4, c(5))
test_t4_predict_01 <- testModel(train_t4_01, t4_testData)
view_weighted_para_count(inputCount=4 ,hiddenVals=c(5))
train_t4_02 <- trainModel(eightHour ~ t1 + t2 + t3 + t4, c(10))
test_t4_predict_02 <- testModel(train_t4_02, t4_testData)
view_weighted_para_count(inputCount=4 ,hiddenVals=c(10))
train_t4_03 <- trainModel(eightHour ~ t1 + t2 + t3 + t4, c(5,2))
test_t4_predict_03 <- testModel(train_t4_03, t4_testData)
view_weighted_para_count(inputCount=4 ,hiddenVals=c(5,2))
train_t4_04 <- trainModel(eightHour ~ t1 + t2 + t3 + t4, c(10,5))
test_t4_predict_04 <- testModel(train_t4_04, t4_testData)
view_weighted_para_count(inputCount=4 ,hiddenVals=c(10,5))
# t7
train_t7_01 <- trainModel(eightHour ~ t1 + t2 + t3 + t4 + t7, c(5))
test_t7_predict_01 <- testModel(train_t7_01, t7_testData)
view_weighted_para_count(inputCount=7 ,hiddenVals=c(5))
train_t7_02 <- trainModel(eightHour ~ t1 + t2 + t3 + t4 + t7, c(10))
test_t7_predict_02 <- testModel(train_t7_02, t7_testData)
view_weighted_para_count(inputCount=7 ,hiddenVals=c(10))
train_t7_03 <- trainModel(eightHour ~ t1 + t2 + t3 + t4 + t7, c(5,2), actFunc = "tanh")
test_t7_predict_03 <- testModel(train_t7_03, t7_testData)
view_weighted_para_count(inputCount=7 ,hiddenVals=c(5,2))
train_t7_04 <- trainModel(eightHour ~ t1 + t2 + t3 + t4 + t7, c(10,5))
test_t7_predict_04 <- testModel(train_t7_04, t7_testData)
view_weighted_para_count(inputCount=7 ,hiddenVals=c(10,5))
train_t7_05 <- trainModel(eightHour ~ t1 + t2 + t3 + t4 + t7, c(12,5))
test_t7_predict_05 <- testModel(train_t7_05, t7_testData)
view_weighted_para_count(inputCount=7 ,hiddenVals=c(12,5))
#-----draw graph - actual vs predicted - highest accuracy model---
set.seed(234)
actual <- original_data_output
predicted <- test_t7_predict_01 + actual
actualPredictedDf <- data.frame(actual, predicted)
# fit data to a linear model
linear_model <- lm(predicted ~ actual, actualPredictedDf )
# plot predicted vs actual
plot(predict(linear_model), actualPredictedDf$predicted,
main = "Predicted vs Desired outputs (AR)",
xlab = "Predicted Values",
ylab = "Desired Values")
abline(a = 0, b = 1, lwd=2,
col = "red")