-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerm_Project_VT2.R
More file actions
280 lines (246 loc) · 11.8 KB
/
Copy pathTerm_Project_VT2.R
File metadata and controls
280 lines (246 loc) · 11.8 KB
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
library(ggplot2)
library(dplyr)
library(depmixS4)
library(stats)
library(stringr)
library(zoo)
# TODO:
# test HMM learning with 3 features
# learn how to use fitted model on new data
# test the trained model, find almost equal log_lik
# use tested training model to find anamolous dataset
# setwd("C:/Users/24312/Documents/code/R") # is Isaac's path
setwd("C:/Users/thund/Desktop/Cmpt 318") # is Vincent's path
# a random seed setup ensures reproducable results
set.seed(3)
df <- read.table("Term_Project_Dataset.txt", header = TRUE, sep = ",")
## Part 1
# interpolate N/A values
df_time_info_header <- df[, 1 : 2]
df_no_time_info <- df[, 3 : 9]
df_no_time_info <- na.approx(df_no_time_info)
# the first row has a N/A value that cannot be interpolated
# so I manually set it the same as that of the second row
df_no_time_info[1, 1] <- df_no_time_info[2, 1]
df <- cbind(df_time_info_header, df_no_time_info)
# Selecting subset for PCA,
# specifically Saturdays from 16/12/2006 to 1/12/2009 from 2:00pm - 7:00pm
df1 <- df %>% filter(Time >= "13:59:59" & Time <= "19:00:00")
# We know that in a 5 hour interval,
# there will be 60*5+1 = 301 observations per day
# (the first minute of the next hour is included)
# Along with 52 weeks in a year, from 16/12/2006 to 1/12/2009,
# that means we need to find 3 + 52 + 52 + 47 = 154 Saturdays
df2 <- df1[1:97, ]
tempdf <- tail(df1, 325185 - 1903)
for (i in 0:153) {
temp <- tempdf[(1 + (301 * 7 * i)) : (301 + (301 * 7 * i)), ]
df2 <- rbind(df2, temp)
}
selecteddata <- df2
dfpca <- as.data.frame(selecteddata[3 : 8])
pca <- prcomp(dfpca, scale = TRUE)
pcasum <- summary(pca)
biplot(pca,
cex = c(0.3, 0.5),
col = c("black", "red"),
arrow.len = 0.1,
ylim = c(-0.03, 0.03),
xlim = c(-0.03, 0.015))
# sort the different fields by their importance to PC1
# PCn denotes the Principal Component with nth highest portion of variance
# the Principle component can explain in the data
# the importance of a field in the data to the Principal Component is measured
# by the absolute value of of its loading score in that Principal Component
# pick the top 2 or 3 as our features to train HMM with
importance <- sort(abs(pca$rotation[, 1]), decreasing = TRUE)
importanceFeatures <- names(importance[1:3])
pca$rotation[importanceFeatures, 1]
# results show that Global_active_power, Global_intensity and Sub_metering_1
# are the most important features in PC1
pcasum
# we can see that the first two PCs can explain about 60% of the data's
# variance, and thus we do not consider the rest PCs, as it brings
# unnecessary extra complexity to training the model, and possibly
# also more noise to be introduced
# Global_active_power, Global_intensity and Sub_metering_1 are the most
# important features in PC1, and since they are still very important
# considering their weighted average loading score, they are a good choice for
# features when training the HMM model.
# we may want to remove the feature with least importance to PC1 among the
# three, Sub_metering_1, later if doing so improves the performance of the model
## Part 2, Work In Progress
dfresult <- selecteddata[, c(3, 6)]
weeks_before_2009 <- 154 - 47
obs_per_week <- 301
obs_first_week <- 97
entries_before_2009 <- obs_first_week + (obs_per_week * (weeks_before_2009 - 1))
df_train <- dfresult[1: entries_before_2009, ]
#I think this is an issue because setdiff removes all the rows in dfresult that are also in df_train.
#However, these data frames only contain two columns, meaning it is completely possible for them to be equal by coincidence
#By running tail(dfresult,obs_per_week*47), I found an additional 5000 entries than in df_test
df_test <- setdiff(dfresult, df_train)
model_4states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_train,
nstates = 4,
ntimes = c(obs_first_week,
rep(obs_per_week, weeks_before_2009 - 1)),
family = list(gaussian(), gaussian()))
fit_model_4states <- fit(model_4states)
summary(fit_model_4states)
log_lik_4states <- logLik(fit_model_4states)
BIC_4states <- BIC(fit_model_4states)
model_8states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_train,
nstates = 8,
ntimes = c(obs_first_week,
rep(obs_per_week, weeks_before_2009 - 1)),
family = list(gaussian(), gaussian()))
fit_model_8states <- fit(model_8states)
summary(fit_model_8states)
log_lik_8states <- logLik(fit_model_8states)
BIC_8states <- BIC(fit_model_8states)
model_12states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_train,
nstates = 12,
ntimes = c(obs_first_week,
rep(obs_per_week, weeks_before_2009 - 1)),
family = list(gaussian(), gaussian()))
fit_model_12states <- fit(model_12states)
summary(fit_model_12states)
log_lik_12states <- logLik(fit_model_12states)
BIC_12states <- BIC(fit_model_12states)
model_14states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_train,
nstates = 14,
ntimes = c(obs_first_week,
rep(obs_per_week, weeks_before_2009 - 1)),
family = list(gaussian(), gaussian()))
fit_model_14states <- fit(model_14states)
summary(fit_model_14states)
log_lik_14states <- logLik(fit_model_14states)
BIC_14states <- BIC(fit_model_14states)
model_16states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_train,
nstates = 16,
ntimes = c(obs_first_week,
rep(obs_per_week, weeks_before_2009 - 1)),
family = list(gaussian(), gaussian()))
fit_model_16states <- fit(model_16states)
summary(fit_model_16states)
log_lik_16states <- logLik(fit_model_16states)
BIC_16states <- BIC(fit_model_16states)
model_18states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_train,
nstates = 18,
ntimes = c(obs_first_week,
rep(obs_per_week, weeks_before_2009 - 1)),
family = list(gaussian(), gaussian()))
fit_model_18states <- fit(model_18states)
summary(fit_model_18states)
log_lik_18states <- logLik(fit_model_18states)
BIC_18states <- BIC(fit_model_18states)
# model_20states <- depmix(response = list(Global_intensity ~ 1,
# Global_active_power ~ 1,
# Sub_metering_1 ~ 1),
# data = df_train,
# nstates = 20,
# ntimes = c(obs_first_week,
# rep(obs_per_week, weeks_before_2009 - 1)),
# family = list(gaussian(), gaussian(), gaussian()))
# fit_model_20states <- fit(model_20states)
# summary(fit_model_20states)
# log_lik_20states <- logLik(fit_model_20states)
# BIC_20states <- BIC(fit_model_20states)
#
# model_24states <- depmix(response = list(Global_intensity ~ 1,
# Global_active_power ~ 1,
# Sub_metering_1 ~ 1),
# data = df_train,
# nstates = 24,
# ntimes = c(obs_first_week,
# rep(obs_per_week, weeks_before_2009 - 1)),
# family = list(gaussian(), gaussian(), gaussian()))
# fit_model_24states <- fit(model_24states)
# summary(fit_model_24states)
# log_lik_24states <- logLik(fit_model_24states)
# BIC_24states <- BIC(fit_model_24states)
model_performance <- data.frame(
state_count = c(4, 8, 12, 14, 16, 18, 20, 24),
log_liks = c(log_lik_4states,
log_lik_8states,
log_lik_12states,
log_lik_14states,
log_lik_16states,
log_lik_18states,
log_lik_20states,
log_lik_24states),
BICs = c(BIC_4states,
BIC_8states,
BIC_12states,
BIC_14states,
BIC_16states,
BIC_18states,
BIC_20states,
BIC_24states)
)
p4 <- model_performance %>% ggplot()
performance_plot <- p4 +
geom_line(aes(x = state_count,
y = log_liks,
colour = "red")) +
geom_line(aes(x = state_count,
y = BICs,
colour = "blue")) +
scale_color_discrete(name = "",
labels = c("BICs", "Log_Likelihood")) +
labs(title = "Performance of Log Likelihood and BIC per Number of States",
x = "Number of States",
y = "")
performance_plot
obs_last_week = 9935 - (301 * 33)
test_14states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_test,
nstates = 14,
ntimes = c(rep(obs_per_week,33),
obs_last_week),
family = list(gaussian(), gaussian()))
fit_test_14states <- setpars(test_14states, getpars(fit_model_14states))
log_lik_test_14states <- forwardbackward(fit_test_14states)$logLike
test_16states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_test,
nstates = 16,
ntimes = c(rep(obs_per_week,33),
obs_last_week),
family = list(gaussian(), gaussian()))
fit_test_16states <- setpars(test_16states, getpars(fit_model_16states))
log_lik_test_16states <- forwardbackward(fit_test_16states)$logLike
test_18states <- depmix(response = list(Global_intensity ~ 1,
Global_active_power ~ 1),
data = df_test,
nstates = 18,
ntimes = c(rep(obs_per_week,33),
obs_last_week),
family = list(gaussian(), gaussian()))
fit_test_18states <- setpars(test_18states, getpars(fit_model_18states))
log_lik_test_18states <- forwardbackward(fit_test_18states)$logLike
normalized_train_14states_loglik <- log_lik_14states / 32003
normalized_train_16states_loglik <- log_lik_16states / 32003
normalized_train_18states_loglik <- log_lik_18states / 32003
normalized_test_14states_loglik <- log_lik_test_14states / 9935
normalized_test_16states_loglik <- log_lik_test_16states / 9935
normalized_test_18states_loglik <- log_lik_test_18states / 9935
log_lik_diff_14states <- abs(normalized_test_14states_loglik -
normalized_train_14states_loglik)
log_lik_diff_16states <- abs(normalized_test_16states_loglik -
normalized_train_16states_loglik)
log_lik_diff_18states <- abs(normalized_test_18states_loglik -
normalized_train_18states_loglik)