knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

torchts

CRAN status R build status Codecov test coverage Buy hex
stciker

Time series models with torch

"Buy Me A Coffee"

Installation

You can install the released version of torchts from CRAN with:

The development version from GitHub with:

# install.packages("devtools")
devtools::install_github("krzjoa/torchts")

parsnip models

library(torchts)
library(torch)
library(rsample)
library(dplyr, warn.conflicts = FALSE)
library(parsnip)
library(timetk)
library(ggplot2)

tarnow_temp <- 
  weather_pl %>% 
  filter(station == "TRN") %>% 
  select(date, tmax_daily)

# Params
EPOCHS    <- 3
HORIZON   <- 1
TIMESTEPS <- 28

# Splitting on training and test
data_split <- 
  time_series_split(
    tarnow_temp, date, 
    initial = "18 years",
    assess  = "2 years", 
    lag     = TIMESTEPS
  )

# Training 
rnn_model <- 
  rnn(
    timesteps    = TIMESTEPS,
    horizon      = HORIZON,
    epochs       = EPOCHS,
    learn_rate   = 0.01,
    hidden_units = 20,
    batch_size   = 32,
    scale        = TRUE 
  ) %>% 
  set_device('cpu')  %>% 
  fit(tmax_daily ~ date, 
      data = training(data_split))

prediction <-
  rnn_model %>%
  predict(new_data = testing(data_split))

plot_forecast(
  data     = testing(data_split),
  forecast = prediction,
  outcome  = tmax_daily
)

Transforming data.frames to tensors

In as_tensor function we can specify columns, that are used to create a tensor out of the input data.frame. Listed column names are only used to determine dimension sizes - they are removed after that and are not present in the final tensor.

temperature_pl <- 
  weather_pl %>% 
  select(station, date, tmax_daily)

# Expected shape
c(
  n_distinct(temperature_pl$station),
  n_distinct(temperature_pl$date),
  1
)

temperature_tensor <- 
  temperature_pl %>% 
  as_tensor(station, date)

dim(temperature_tensor)
temperature_tensor[1, 1:10]

temperature_pl %>% 
  filter(station == "SWK") %>% 
  arrange(date) %>% 
  head(10)

Similar projects in Python



krzjoa/torchts documentation built on June 24, 2022, 5:30 a.m.