knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(biggr)
library(ggplot2)
library(lubridate)
data(biggr)

Transformation

Multiple helper functions are provided

Decompose calendar

Decompose the time in date, day of the year, day of the week, day of the weekend, working day, non-working day, season, month, hour, minute, ...

create_serie <- function(n, values, timestep = "hours", start = ymd_hms("2020-01-01 00:00:00"), featuresName = c("value")) {
  funcs <- list(
    "mins" = minutes,
    "hours" = hours,
    "days" = days,
    "months" = months,
    "years" = years
  )
  func <- funcs[[timestep]]
  tmp <- (
    data.frame(
      time = seq(start, start + func(n - 1), by = timestep),
      value = values
    )
  )
  colnames(tmp) <- c("time", featuresName)
  return(tmp)
}
# Get time serie calendar properties
start <- ymd_hms("2020-07-01 00:00:00")
testdata <- create_serie(5, rep(1, 5), timestep = "hours", start = start)
calendar_components(testdata, "Europe/Madrid")

Calculate deegree raw and degree days

Calculate the difference between outdoor temperature and a base temperature

start <- ymd_hms("2020-12-27 00:00:00")
featuresName <- c("temperature")
testdata <- create_serie(7, c(26, 27, 28, 29, 30, 31, 32), timestep = "days",
  featuresName = featuresName)
outputFeaturesName <- c("cooling")
baseTemperature <- 25
newtestdata <- degree_raw(testdata, featuresName, baseTemperature,
    outputFeaturesName, mode = "cooling")

print(testdata)
print(newtestdata)
start <- ymd_hms("2020-12-27 00:00:00")
featuresName <- c("temperature")
testdata <- create_serie(7, c(26, 27, 28, 29, 30, 31, 32), timestep = "days",
  featuresName = featuresName)
outputFeaturesName <- c("cooling")
baseTemperature <- 25
newtestdata <- degree_raw(testdata, featuresName, baseTemperature,
    outputFeaturesName, mode = "cooling")

print(testdata)
print(newtestdata)

Normalization

Multiple normalization methods are provided

Range int normalization

testdata <- data.frame(
  value1 = c(0, 0, 25, 25, 100, 100),
  value2 = c(0, 0, 50, 50, 100, 100)
)
newtestdata <- normalise_range(testdata, lower = 0, upper = 1)

print(testdata)
print(newtestdata)

Daily normalization

serie <- electricity5 %>%
  mutate(
    time=ymd_hms(time)
  ) %>%
  rename(c("value"="Qe")) %>%
  select(time, value) %>%
  head(24*6)
newserie <- normalise_daily(serie, localTimeZone = "Europe/Madrid")

serie %>% ggplot(aes(x=time, y=value)) + geom_point() + ggtitle("ante")
newserie %>% ggplot(aes(x=time, y=value)) + geom_point() + ggtitle("post")

Zscore normalization

serie <- electricity5 %>%
  mutate(
    time=ymd_hms(time)
  ) %>%
  rename(c("value"="Qe")) %>%
  select(time, value) %>%
  head(24*6)
newvalues <- normalise_zscore(serie$value)
newserie <- as.data.frame(cbind(serie$time, newvalues$values)) 
colnames(newserie) <- c("time", "value")

serie %>% ggplot(aes(x=time, y=value)) + geom_point() + ggtitle("ante")
newserie %>% ggplot(aes(x=time, y=value)) + geom_point() + ggtitle("post")


biggproject/biggr documentation built on Oct. 2, 2024, 11:13 p.m.