Repository for Petra's work at ampli Jan-Feb 2019

clusterviz.R 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. library(reticulate)
  2. library(ggplot2)
  3. library(dplyr)
  4. library(tidyr)
  5. library(TSA)
  6. library(forecast)
  7. theme_set(theme_bw())
  8. use_virtualenv("../venv/")
  9. fextract <- function(x, y, keep = 1, top = TRUE) {
  10. sdy <- sd(y)
  11. my <- mean(y)
  12. stany <- (y - my) / sdy
  13. ftf <- fft(stany)
  14. if (top) {
  15. ftf[rank(-abs(ftf)) > keep] <- 0
  16. } else {
  17. ftf[(keep + 1):length(ftf)] <- 0
  18. }
  19. rfv <- Re(fft(ftf, inverse = TRUE))
  20. dfr <- data.frame(x = x, y = y, f = (rfv - mean(rfv)) / sd(rfv) * sdy + my)
  21. dfr$res <- dfr$y - dfr$f
  22. return(dfr)
  23. }
  24. p <- import("pandas")
  25. sns <- import("seaborn")
  26. cbp <- as.character(p$Series(sns$color_palette("colorblind", as.integer(9))$as_hex()))
  27. aggdf <- p$read_pickle("../data/9-clusters-1617.agg.pkl")
  28. # aggdf <- as.data.frame(aggdf)
  29. aggdf$cluster <- factor(aggdf$cluster)
  30. str(aggdf)
  31. clusters = levels(aggdf$cluster)
  32. ggplot(aggdf, aes(y = kwh_tot_mean, x = cluster)) + geom_boxplot()
  33. facall <- ggplot(aggdf, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  34. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  35. labs(title = "Cluster behaviour over 2016 and 2017", x = "Date", y = "kwh") +
  36. scale_color_manual(values = cbp) +
  37. scale_fill_manual(values = cbp) +
  38. theme(legend.position = "none") +
  39. scale_x_datetime(date_breaks = "1 month", date_labels = "%-d %b %y")
  40. allcon <- facall + facet_grid(cluster ~ .)
  41. allfre <- facall + facet_grid(cluster ~ ., scales = "free")
  42. midjan <- filter(aggdf, read_time >= as.POSIXct("2017-01-15", tz = "UTC"), read_time <= as.POSIXct("2017-01-22", tz = "UTC"))
  43. facjan <- ggplot(midjan, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  44. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  45. labs(title = "Cluster behaviour over third week of January", x = "Date", y = "kwh") +
  46. scale_color_manual(values = cbp) +
  47. scale_fill_manual(values = cbp) +
  48. theme(legend.position = "none") +
  49. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  50. jancon <- facjan + facet_grid(cluster ~ .)
  51. janfre <- facjan + facet_grid(cluster ~ ., scales = "free")
  52. midap <- filter(aggdf, read_time >= as.POSIXct("2017-04-16", tz = "UTC"), read_time <= as.POSIXct("2017-04-23", tz = "UTC"))
  53. facap <- ggplot(midap, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  54. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  55. labs(title = "Cluster behaviour over third week of April 2017", x = "Date", y = "kwh") +
  56. scale_color_manual(values = cbp) +
  57. scale_fill_manual(values = cbp) +
  58. theme(legend.position = "none") +
  59. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  60. apcon <- facap + facet_grid(cluster ~ .)
  61. apfre <- facap + facet_grid(cluster ~ ., scales = "free")
  62. midjul <- filter(aggdf, read_time >= as.POSIXct("2017-07-16", tz = "UTC"), read_time <= as.POSIXct("2017-07-23", tz = "UTC"))
  63. facjul <- ggplot(midjul, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  64. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  65. labs(title = "Cluster behaviour over third week of July 2017", x = "Date", y = "kwh") +
  66. scale_color_manual(values = cbp) +
  67. scale_fill_manual(values = cbp) +
  68. theme(legend.position = "none") +
  69. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  70. julcon <- facjul + facet_grid(cluster ~ .)
  71. julfre <- facjul + facet_grid(cluster ~ ., scales = "free")
  72. midoct <- filter(aggdf, read_time >= as.POSIXct("2017-10-15", tz = "UTC"), read_time <= as.POSIXct("2017-10-22", tz = "UTC"))
  73. facoct <- ggplot(midoct, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  74. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  75. labs(title = "Cluster behaviour over third week of October 2017", x = "Date", y = "kwh") +
  76. scale_color_manual(values = cbp) +
  77. scale_fill_manual(values = cbp) +
  78. theme(legend.position = "none") +
  79. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  80. octcon <- facoct + facet_grid(cluster ~ .)
  81. octfre <- facoct + facet_grid(cluster ~ ., scales = "free")
  82. ggsave("all-9-fix-1617.png", allcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  83. ggsave("all-9-fre-1617.png", allfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  84. ggsave("jan-9-fix-1617.png", jancon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  85. ggsave("jan-9-fre-1617.png", janfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  86. ggsave("apr-9-fix-1617.png", apcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  87. ggsave("apr-9-fre-1617.png", apfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  88. ggsave("jul-9-fix-1617.png", julcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  89. ggsave("jul-9-fre-1617.png", julfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  90. ggsave("oct-9-fix-1617.png", octcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  91. ggsave("oct-9-fre-1617.png", octfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  92. # ----
  93. cacf <- list()
  94. perd <- list()
  95. for (c in clusters) {
  96. cagg <- filter(aggdf, cluster == c)
  97. cacf[[c]] <- acf(cagg$kwh_tot_mean, lag.max = 48*365, plot = FALSE)$acf
  98. per <- periodogram(cagg$kwh_tot_mean, plot = FALSE)
  99. perd[[c]] <- data.frame(freq = per$freq, spec = per$spec) %>% mutate(cluster = c, period = (1/freq))
  100. #%>% arrange(desc(spec)) %>% head(5)
  101. }
  102. acfm <- sapply(cacf, as.numeric) %>% as.data.frame() %>% mutate(hour = ((1:length(`1`)) - 1)/2) %>%
  103. gather(key = "cluster", value = "acorr", clusters) %>% mutate(day = hour / 24, week = hour / (24 * 7))
  104. fcorr <- ggplot(acfm, aes(x = week, y = acorr, color = cluster)) + geom_line(size = 1.5) +
  105. scale_color_manual(values = cbp) + facet_grid(cluster ~ .) + coord_cartesian(expand = FALSE) +
  106. theme(legend.position = "none") + labs(title = "Autocorrelation plot (full year)",
  107. y = "Autocorrelation", x = "lag (weeks)")
  108. wcorr <- ggplot(acfm, aes(x = day, y = acorr, color = cluster)) + geom_line(size = 1.5) +
  109. scale_color_manual(values = cbp) + facet_grid(cluster ~ .) +
  110. scale_x_continuous(breaks = unique(floor(acfm$day / 7)) * 7) +
  111. theme(legend.position = "none") + coord_cartesian(xlim = c(0, 15), expand = FALSE) +
  112. labs(title = "Autocorrelation plot (two weeks)", y = "Autocorrelation", x = "lag (days)")
  113. ggsave("full-autocorr-1617.png", fcorr, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  114. ggsave("week-autocorr-1617.png", wcorr, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  115. perd <- bind_rows(perd)
  116. ggplot(perd, aes(x = freq, y = spec)) +
  117. geom_line() + facet_grid(cluster ~ ., scales = "free") +
  118. scale_x_continuous(breaks = 1 / (c(48, 48*7, 48*7*4, 48*365)))
  119. c1ts <- filter(aggdf, cluster == "1")$kwh_tot_mean
  120. cts <- ts(c1ts, frequency = 48, start = c(1, 1))
  121. # carima <- auto.arima(cts, trace = TRUE)
  122. # plot(forecast(carima, h = 480))
  123. ctsnp <- msts(c1ts, c(48, 48*7))
  124. ctbats <- tbats(ctsnp)
  125. plot(forecast(ctbats, h = 48 * 7 * 4))
  126. c9ts <- filter(aggdf, cluster == "9")$kwh_tot_mean
  127. ctsnp <- msts(c9ts, c(48, 48*7, 48*7*365.25))
  128. ctbats <- tbats(ctsnp)
  129. plot(forecast(ctbats, h = 48 * 7))
  130. p <- periodogram(c1ts)
  131. dd <- data.frame(freq = p$freq, spec = p$spec) %>% mutate(per = 1/freq)
  132. dd %>% arrange(desc(spec)) %>% mutate(d = per / 48) %>% head()
  133. c9ts <- filter(aggdf, cluster == "9")
  134. ggplot(c9ts, aes(x = read_time, y = kwh_tot_mean)) + geom_line()
  135. nft <- fextract(c9ts$read_time, c9ts$kwh_tot_mean, keep = 15)
  136. ggplot(nft, aes(x, y)) + geom_line() +
  137. geom_line(aes(x, f), color = "blue") +
  138. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y") +
  139. coord_cartesian(xlim = c(as.POSIXct("2016-07-16", tz = "UTC"), as.POSIXct("2016-07-23", tz = "UTC")), expand = TRUE)
  140. clus <- "9"
  141. kp <- 50
  142. cts <- filter(aggdf, cluster == clus)
  143. # ggplot(cts, aes(x = read_time, y = kwh_tot_mean)) + geom_line()
  144. nft <- fextract(cts$read_time, cts$kwh_tot_mean, keep = kp)
  145. ggplot(nft, aes(x, y)) + geom_line() +
  146. geom_line(aes(x, f), color = "blue") +
  147. scale_x_datetime(date_breaks = "2 days", date_labels = "%a, %-d %b %Y") +
  148. coord_cartesian(xlim = c(as.POSIXct("2017-09-14", tz = "UTC"), as.POSIXct("2017-09-28", tz = "UTC")), expand = TRUE)
  149. ggplot(nft, aes(x, res)) + geom_line() + scale_x_datetime(date_breaks = "1 week") +
  150. coord_cartesian(xlim = c(as.POSIXct("2017-08-01", tz = "UTC"), as.POSIXct("2017-09-28", tz = "UTC")), expand = TRUE)
  151. ggplot(nft, aes(y = res)) + geom_boxplot()
  152. ggplot(nft, aes(x = res)) + geom_histogram()