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

weathmod.R 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. library(TSA)
  2. library(caTools)
  3. library(dplyr)
  4. library(forecast)
  5. library(ggplot2)
  6. library(reticulate)
  7. library(tidyr)
  8. theme_set(theme_bw())
  9. use_virtualenv("../venv/")
  10. # Load, transform data
  11. p <- import("pandas")
  12. tempdf <- p$read_pickle("../data/2016-18-weather.pkl") %>%
  13. select(-record_no, -station) %>%
  14. mutate(temp_date = as.Date(temp_date))
  15. str(tempdf)
  16. # Get a data frame of all the dates/times we want
  17. wdts <- seq.POSIXt(min(tempdf$temp_timestamp), max(tempdf$temp_timestamp), by = "30 mins")
  18. hhdt <- as.difftime("30", format = "%M")
  19. wantdf <- data.frame(temp_timestamp = wdts, temp_date = as.Date(wdts)) %>% arrange(temp_timestamp)
  20. str(wantdf)
  21. # Recursively impute NAs from previous non-NA value; if first value is NA replaced by median but this shouldn't happen
  22. # If all values are NA will hang, but this shouldn't happen either
  23. fulltemp <- left_join(wantdf, tempdf, by = c("temp_timestamp", "temp_date"))
  24. str(fulltemp)
  25. fulltemp <- fill(fulltemp, tmax_c:rhmean)
  26. sum(is.na(fulltemp$tmin_c))
  27. fulltemp$runmin <- runmin(fulltemp$tmin_c, 48, endrule = "min", align = "right")
  28. fulltemp$runmax <- runmax(fulltemp$tmax_c, 48, endrule = "max", align = "right")
  29. str(fulltemp)
  30. # Temperature plots
  31. tplot <- ggplot(fulltemp, aes(x = temp_timestamp, y = tmin_c)) + geom_line() +
  32. geom_line(aes(y = tmax_c), color = "magenta") +
  33. geom_line(aes(y = runmin), color = "blue") +
  34. geom_line(aes(y = runmax), color = "red")
  35. # tplot
  36. # tplot + coord_cartesian(xlim = c(as.POSIXct("2016-12-01"), as.POSIXct("2017-03-01")))
  37. # Create a harmonic (sine wave) model for minimum temperature
  38. yharm <- harmonic(ts(1:nrow(fulltemp), frequency = floor(365.25 * 48)), 2) %>% as.data.frame()
  39. #dharm <- harmonic(ts(1:nrow(fulltemp), frequency = floor(48)), 1)
  40. hmod <- lm(fulltemp$runmin ~ ., data = yharm)
  41. summary(hmod)
  42. hmdf <- data.frame(x = fulltemp$temp_timestamp, y = fulltemp$runmin, f = fitted(hmod), r = resid(hmod))
  43. tmplot <- ggplot(hmdf, aes(x = x, y = y)) + geom_line(aes(y = f), color = "blue", size = 2) + geom_point() +
  44. geom_point(aes(y = r), color = "darkgreen")
  45. # tmplot
  46. # tmplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01", tz = "UTC"), as.POSIXct("2017-06-01", tz = "UTC")))
  47. maxhmod <- lm(fulltemp$runmax ~ ., data = yharm)
  48. summary(maxhmod)
  49. mhmdf <- data.frame(x = fulltemp$temp_timestamp, y = fulltemp$runmax, f = fitted(maxhmod), r = resid(maxhmod))
  50. mtmplot <- ggplot(mhmdf, aes(x = x, y = y)) + geom_line(aes(y = f), color = "blue", size = 2) + geom_point() +
  51. geom_point(aes(y = r), color = "darkgreen")
  52. # mtmplot
  53. # mtmplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01", tz = "UTC"), as.POSIXct("2017-06-01", tz = "UTC")))
  54. hmdf.comb <- left_join(hmdf, mhmdf, by = "x", suffix = c(".min", ".max"))
  55. ctmplot <- ggplot(hmdf.comb, aes(x = x, y = f.min)) + geom_line(color = "blue", size = 2) +
  56. geom_line(aes(y = f.max), color = "red", size = 2) +
  57. geom_point(aes(y = y.max), color = "magenta") +
  58. geom_point(aes(y = y.min), color = "lightblue")
  59. # ctmplot
  60. # ctmplot + coord_cartesian(xlim = c(as.POSIXct("2017-05-01", tz = "UTC"), as.POSIXct("2017-06-01", tz = "UTC")))
  61. write.csv(hmdf.comb, "../data/weatherharm.csv", row.names = FALSE)
  62. saveRDS(hmod, "../models/weatherMinharmonic.rds")
  63. saveRDS(maxhmod, "../models/weatherMaxharmonic.rds")