Browse Source

Harmonic stuff

Petra Lamborn 5 years ago
parent
commit
3dae6b02ac
1 changed files with 82 additions and 0 deletions
  1. 82
    0
      R/clusterviz.R

+ 82
- 0
R/clusterviz.R View File

@@ -193,3 +193,85 @@ ggplot(nft, aes(x, res)) + geom_line() + scale_x_datetime(date_breaks = "1 week"
193 193
 
194 194
 ggplot(nft, aes(y = res)) + geom_boxplot()
195 195
 ggplot(nft, aes(x = res)) + geom_histogram()
196
+
197
+
198
+c9ts <- filter(aggdf, cluster == "9")$kwh_tot_mean
199
+c9ft <- as.numeric(abs(fft(c9ts)))
200
+c9ftdf <- data.frame(x = 1:length(c9ft), ft = c9ft)
201
+# %>% arrange(desc(ft)) %>% mutate(xt = 1/x)
202
+
203
+c9df <- data.frame(x = 1:length(c9ts), y = c9ts, ly = log(c9ts + min(c9ts) + 1))
204
+c9harm. <- harmonic(ts(c9ts, frequency = 17544), 2)
205
+sinmod <- lm(c9df$y ~ c9harm.)
206
+c9df$fit <- fitted(sinmod)
207
+c9df$res <- resid(sinmod)
208
+
209
+ggplot(c9df, aes(x = x, y = ly)) + geom_point() +
210
+    geom_line(aes(y = fit), size = 2, colour = "blue")
211
+
212
+plot(c(c9df$res))
213
+hist(log(c9df$y), breaks = 30)
214
+
215
+n <- 1.5
216
+ytra <- c9df$y / c9df$fit^n - 1/c9df$fit^(n-1)
217
+plot(ytra)
218
+
219
+
220
+yts <- msts(ytra, seasonal.periods = c(48, 48*7))
221
+autoplot(mstl(yts))
222
+
223
+ytsd <- ts(ytra, frequency = 48)
224
+ydharm. <- harmonic(ytsd, 10)
225
+sdmod <- lm(ytra ~ ydharm.)
226
+sddf <- data.frame(x = 1:length(ytra), y = ytra, f = fitted(sdmod), r = resid(sdmod))
227
+ggplot(sddf[1:(48*7*2), ], aes(x = x, y = y)) + geom_point() +
228
+    geom_line(aes(y = f), colour = "blue", size = 2)
229
+
230
+plot(resid(sdmod)[1:(48*7*2)])
231
+
232
+
233
+
234
+
235
+
236
+
237
+c9tsy <- ts(c9ts, frequency = floor(48 * 365.25))
238
+c9tsw <- ts(c9ts, frequency = floor(48 * 7))
239
+c9tsd <- ts(c9ts, frequency = floor(48))
240
+
241
+harm.y <- harmonic(c9tsy, 2)
242
+harm.w <- harmonic(c9tsw, 2)
243
+harm.d <- harmonic(c9tsd, 6)
244
+
245
+cmod <- lm(c9ts ~ harm.y + harm.w + harm.d)
246
+summary(cmod)
247
+plot(fitted(cmod))
248
+plot(resid(cmod))
249
+
250
+
251
+cmodi <- lm(c9ts ~ harm.y * harm.w * harm.d)
252
+
253
+summary(cmodi)
254
+
255
+plot(fitted(cmodi))
256
+plot(c9ts)
257
+plot(resid(cmodi))
258
+hist(resid(cmodi), breaks = 100)
259
+# Heavy tails
260
+qqnorm(resid(cmodi))
261
+
262
+
263
+clust = "6"
264
+
265
+cts <- filter(aggdf, cluster == clust)$kwh_tot_mean
266
+ctsy <- ts(cts, frequency = floor(48 * 365.25))
267
+ctsw <- ts(cts, frequency = floor(48 * 7))
268
+ctsd <- ts(cts, frequency = floor(48))
269
+harm.y <- harmonic(ctsy, 5)
270
+harm.w <- harmonic(ctsw, 3)
271
+harm.d <- harmonic(ctsd, 3)
272
+cmodi <- lm(cts ~ harm.y * harm.w * harm.d)
273
+summary(cmodi)
274
+
275
+plot(fitted(cmodi))
276
+plot(cts)
277
+plot(resid(cmodi))