Browse Source

Weather including rolling maximum

Petra Lamborn 5 years ago
parent
commit
5e15298c2a
2 changed files with 42 additions and 8 deletions
  1. 12
    5
      R/combmodels.R
  2. 30
    3
      R/weathmod.R

+ 12
- 5
R/combmodels.R View File

@@ -19,7 +19,8 @@ clusters <- levels(aggdf$cluster)
19 19
 str(aggdf)
20 20
 mtempdf <- read.csv("../data/weatherharm.csv", stringsAsFactors = FALSE) %>% 
21 21
     mutate(x = as.POSIXct(x, tz = "UTC")) %>%
22
-    rename(read_time = x, rollingmin = y, fitmin = f, resmin = r)
22
+    rename(read_time = x, rollingmin = y.min, fitmin = f.min, resmin = r.min,
23
+           rollingmax = y.max, fitmax = f.max, resmax = r.max)
23 24
 str(mtempdf)
24 25
 sns <- import("seaborn")
25 26
 cbp <- as.character(p$Series(sns$color_palette("colorblind", as.integer(9))$as_hex()))
@@ -50,10 +51,13 @@ ycols <- paste(colnames(harm.y), collapse = " + ")
50 51
 wcols <- paste(colnames(harm.w), collapse = " + ") 
51 52
 dcols <- paste(colnames(harm.d), collapse = " + ") 
52 53
 
53
-nform.full <- sprintf("kwh ~ %s + %s + %s + (%s):(%s) + (%s):(%s) + (%s):(%s) + resmin + resmin:(%s) + resmin:(%s) + resmin:(%s)", 
54
-        ycols, wcols, dcols, ycols, wcols, ycols, dcols, wcols, dcols, ycols, wcols, dcols) %>% formula()
55
-nform.comp <- sprintf("kwh ~ %s + %s + %s + (%s):(%s) + (%s):(%s) + resmin + resmin:(%s) + resmin:(%s) + resmin:(%s)", 
56
-        ycols, wcols, dcols, ycols, dcols, wcols, dcols, ycols, wcols, dcols) %>% formula()
54
+nform.full <- sprintf(paste0("kwh ~ %s + %s + %s + (%s):(%s) + (%s):(%s) + (%s):(%s) + resmin",
55
+                             " + resmin:(%s) + resmin:(%s) + resmin:(%s)",
56
+                             " + resmax + resmax:(%s) + resmax:(%s) + resmax:(%s)"), 
57
+        ycols, wcols, dcols, ycols, wcols, ycols, dcols, wcols, dcols, ycols, wcols, dcols, ycols, wcols, dcols) %>% formula()
58
+nform.comp <- sprintf(paste0("kwh ~ %s + %s + %s + (%s):(%s) + (%s):(%s) + resmin + resmin:(%s) + resmin:(%s) + resmin:(%s)",
59
+                             " + resmax + resmax:(%s) + resmax:(%s) + resmax:(%s)"), 
60
+        ycols, wcols, dcols, ycols, dcols, wcols, dcols, ycols, wcols, dcols, ycols, wcols, dcols) %>% formula()
57 61
 nform.now <- sprintf("kwh ~ %s + %s + %s + (%s):(%s) + (%s):(%s)", 
58 62
         ycols, wcols, dcols, ycols, dcols, wcols, dcols) %>% formula()
59 63
 nform.min <- formula("kwh ~ 1")
@@ -116,3 +120,6 @@ predplot <-ggplot(predf, aes(x = x, y = y)) + geom_line(aes(y = f), color = "blu
116 120
 predplot
117 121
 
118 122
 predplot + coord_cartesian(xlim = c(as.POSIXct("2018-03-01", tz = "UTC"), as.POSIXct("2018-04-01", tz = "UTC")))
123
+
124
+mean(abs(predf$r))
125
+sd(predf$r)

+ 30
- 3
R/weathmod.R View File

@@ -38,15 +38,18 @@ for (coln in names(fulltemp)[3:7]) {
38 38
 }
39 39
 sum(is.na(fulltemp$tmin_c))
40 40
 fulltemp$runmin <- runmin(fulltemp$tmin_c, 48, endrule = "min", align = "right")
41
+fulltemp$runmax <- runmax(fulltemp$tmax_c, 48, endrule = "max", align = "right")
41 42
 str(fulltemp)
42 43
 
43 44
 # Temperature plots
44 45
 tplot <- ggplot(fulltemp, aes(x = temp_timestamp, y = tmin_c)) + geom_line() +
45
-    geom_line(aes(y = runmin), color = "blue")
46
+    geom_line(aes(y = tmax_c), color = "magenta") +
47
+    geom_line(aes(y = runmin), color = "blue") +
48
+    geom_line(aes(y = runmax), color = "red")
46 49
 
47 50
 tplot
48 51
 
49
-tplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01"), as.POSIXct("2017-06-01")))
52
+tplot + coord_cartesian(xlim = c(as.POSIXct("2016-12-01"), as.POSIXct("2017-03-01")))
50 53
 
51 54
 # Create a harmonic (sine wave) model for minimum temperature
52 55
 yharm <- harmonic(ts(1:nrow(fulltemp), frequency = floor(365.25 * 48)), 2)
@@ -62,4 +65,28 @@ tmplot
62 65
 
63 66
 tmplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01", tz = "UTC"), as.POSIXct("2017-06-01", tz = "UTC")))
64 67
 
65
-write.csv(hmdf, "../data/weatherharm.csv", row.names = FALSE)
68
+
69
+
70
+maxhmod <- lm(fulltemp$runmax ~ yharm)
71
+summary(maxhmod)
72
+mhmdf <- data.frame(x = fulltemp$temp_timestamp, y = fulltemp$runmax, f = fitted(maxhmod), r = resid(maxhmod))
73
+
74
+mtmplot <- ggplot(mhmdf, aes(x = x, y = y)) + geom_line(aes(y = f), color = "blue", size = 2) + geom_point() +
75
+    geom_point(aes(y = r), color = "darkgreen")
76
+
77
+mtmplot
78
+
79
+mtmplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01", tz = "UTC"), as.POSIXct("2017-06-01", tz = "UTC")))
80
+
81
+hmdf.comb <- left_join(hmdf, mhmdf, by = "x", suffix = c(".min", ".max"))
82
+
83
+ctmplot <- ggplot(hmdf.comb, aes(x = x, y = f.min)) + geom_line(color = "blue", size = 2) +
84
+    geom_line(aes(y = f.max), color = "red", size = 2) +
85
+    geom_point(aes(y = y.max), color = "magenta") +
86
+    geom_point(aes(y = y.min), color = "lightblue")
87
+
88
+ctmplot
89
+
90
+ctmplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01", tz = "UTC"), as.POSIXct("2017-06-01", tz = "UTC")))
91
+
92
+write.csv(hmdf.comb, "../data/weatherharm.csv", row.names = FALSE)