Browse Source

Working with old 1-year dataset

Petra Lamborn 5 years ago
parent
commit
8009428408
1 changed files with 21 additions and 20 deletions
  1. 21
    20
      R/clusterviz.R

+ 21
- 20
R/clusterviz.R View File

@@ -27,7 +27,7 @@ fextract <- function(x, y, keep = 1, top = TRUE) {
27 27
 p <- import("pandas")
28 28
 sns <- import("seaborn")
29 29
 cbp <- as.character(p$Series(sns$color_palette("colorblind", as.integer(9))$as_hex()))
30
-aggdf <- p$read_pickle("../data/9-clusters-1617.agg.pkl")
30
+aggdf <- p$read_pickle("../data/9-clusters.agg.pkl")
31 31
 # aggdf <- as.data.frame(aggdf)
32 32
 aggdf$cluster <- factor(aggdf$cluster)
33 33
 str(aggdf)
@@ -37,7 +37,7 @@ ggplot(aggdf, aes(y = kwh_tot_mean, x = cluster)) + geom_boxplot()
37 37
 
38 38
 facall <- ggplot(aggdf, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) + 
39 39
     geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
40
-    labs(title = "Cluster behaviour over 2016 and 2017", x = "Date", y = "kwh") +
40
+    labs(title = "Cluster behaviour over 2017", x = "Date", y = "kwh") +
41 41
     scale_color_manual(values = cbp) +
42 42
     scale_fill_manual(values = cbp) +
43 43
     theme(legend.position = "none") +
@@ -101,16 +101,16 @@ facoct <- ggplot(midoct, aes(x = read_time, y = kwh_tot_mean, color = cluster, f
101 101
 octcon <- facoct + facet_grid(cluster ~ .)
102 102
 octfre <- facoct + facet_grid(cluster ~ ., scales = "free")
103 103
 
104
-ggsave("all-9-fix-1617.png", allcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
105
-ggsave("all-9-fre-1617.png", allfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
106
-ggsave("jan-9-fix-1617.png", jancon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
107
-ggsave("jan-9-fre-1617.png", janfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
108
-ggsave("apr-9-fix-1617.png",  apcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
109
-ggsave("apr-9-fre-1617.png",  apfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
110
-ggsave("jul-9-fix-1617.png", julcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
111
-ggsave("jul-9-fre-1617.png", julfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
112
-ggsave("oct-9-fix-1617.png", octcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
113
-ggsave("oct-9-fre-1617.png", octfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
104
+# ggsave("all-9-fix-1617.png", allcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
105
+# ggsave("all-9-fre-1617.png", allfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
106
+# ggsave("jan-9-fix-1617.png", jancon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
107
+# ggsave("jan-9-fre-1617.png", janfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
108
+# ggsave("apr-9-fix-1617.png",  apcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
109
+# ggsave("apr-9-fre-1617.png",  apfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
110
+# ggsave("jul-9-fix-1617.png", julcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
111
+# ggsave("jul-9-fre-1617.png", julfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
112
+# ggsave("oct-9-fix-1617.png", octcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
113
+# ggsave("oct-9-fre-1617.png", octfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
114 114
 
115 115
 
116 116
 # ----
@@ -142,8 +142,8 @@ wcorr <- ggplot(acfm, aes(x = day, y = acorr, color = cluster)) + geom_line(size
142 142
     theme(legend.position = "none") + coord_cartesian(xlim = c(0, 15), expand = FALSE) +
143 143
     labs(title = "Autocorrelation plot (two weeks)", y = "Autocorrelation", x = "lag (days)")
144 144
 
145
-ggsave("full-autocorr-1617.png", fcorr, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
146
-ggsave("week-autocorr-1617.png", wcorr, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
145
+# ggsave("full-autocorr-1617.png", fcorr, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
146
+# ggsave("week-autocorr-1617.png", wcorr, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
147 147
 
148 148
 perd <- bind_rows(perd)
149 149
 
@@ -260,18 +260,19 @@ hist(resid(cmodi), breaks = 100)
260 260
 qqnorm(resid(cmodi))
261 261
 
262 262
 
263
-clust = "6"
263
+clust = "9"
264 264
 
265 265
 cts <- filter(aggdf, cluster == clust)$kwh_tot_mean
266 266
 ctsy <- ts(cts, frequency = floor(48 * 365.25))
267 267
 ctsw <- ts(cts, frequency = floor(48 * 7))
268 268
 ctsd <- ts(cts, frequency = floor(48))
269
-harm.y <- harmonic(ctsy, 5)
269
+harm.y <- harmonic(ctsy, 4)
270 270
 harm.w <- harmonic(ctsw, 3)
271
-harm.d <- harmonic(ctsd, 3)
271
+harm.d <- harmonic(ctsd, 4)
272 272
 cmodi <- lm(cts ~ harm.y * harm.w * harm.d)
273 273
 summary(cmodi)
274
+cldf <- data.frame(x = unique(aggdf$read_time), y = cts, f = fitted(cmodi), r = resid(cmodi))
275
+ggplot(cldf, aes(x = x, y = y)) + geom_line(aes(y = f), color = "blue", size = 2) + geom_point() +
276
+    geom_point(aes(y = r), color = "darkgreen") +
277
+    coord_cartesian(xlim = c(as.POSIXct("2017-01-01", tz = "UTC"), as.POSIXct("2017-12-31", tz = "UTC")))
274 278
 
275
-plot(fitted(cmodi))
276
-plot(cts)
277
-plot(resid(cmodi))