Create a time series dataset from a torch_tensor matrix

ts_dataset(
  data,
  timesteps,
  horizon,
  jump = horizon,
  predictors_spec = list(x = NULL),
  outcomes_spec = list(y = NULL),
  categorical = NULL,
  sample_frac = 1,
  scale = TRUE,
  extras = NULL,
  ...
)

Arguments

data

(torch_tensor) An input data object. For now it only accepts two-dimensional tensor, i.e. matrices. Each row is a timestep of a single time series.

timesteps

(integer) Number of timesteps for input tensor.

horizon

(integer) Forecast horizon: number of timesteps for output tensor.

jump

(integer) Jump length. By default: horizon length.

predictors_spec

(list) Input specification. It should be a list with names representing names of tensors served by dataset, and values being feature indices.

outcomes_spec

(list) Target specification. It should be a list with names representing names of tensors served b

categorical

(character) Names of specified column subsets considered as categorical. They will be provided as integer tensors.

scale

(logical or list) Scale feature columns. Boolean flag or list with mean and sd values.

extras

(list) List of extra object to be stored inside the ts_dataset object.

sample_fram

(numeric) A numeric value > 0. and <= 1 to sample a subset of data.

Note

If scale is TRUE, only the input variables are scale and not the outcome ones.

See: Is it necessary to scale the target value in addition to scaling features for regression analysis? (Cross Validated)

Examples

library(dplyr, warn.conflicts = FALSE)
library(torchts)

weather_pl_tensor <-
  weather_pl %>%
  filter(station == "TRN") %>%
  select(-station, -rr_type) %>%
  as_tensor(date)

# We obtained a matrix (i.e. tabular data in the form of 2-dimensional tensor)
dim(weather_pl_tensor)
#> [1] 7305    7

weather_pl_dataset <-
   ts_dataset(
     data = weather_pl_tensor,
     timesteps = 28,
     horizon = 7,
     predictors_spec = list(x = 2:7),
     outcomes_spec   = list(y = 1),
     scale = TRUE
   )

weather_pl_dataset$.getitem(1)
#> $x
#> torch_tensor
#> -1.3449 -1.6086 -0.3617 -0.3091 -0.2641  0.0491
#> -1.2028 -1.2505 -0.3617 -0.3091 -0.2641 -0.0979
#> -0.7637 -0.9046  1.6144  1.4789  1.0892 -0.1111
#> -0.5442 -0.5712 -0.3617 -0.3091 -0.2641 -0.0430
#> -0.5829 -0.7688 -0.3617 -0.3091 -0.2641 -0.0979
#> -0.3246 -0.2747 -0.3617 -0.3091 -0.2641 -0.2055
#> -0.0922  0.0464 -0.1401 -0.3091  0.0742 -0.0211
#> -0.2213 -0.0154  2.8149  0.7516  3.5983 -0.1023
#> -0.6862 -0.4724  1.1527  2.1759 -0.2641 -0.0738
#> -0.8412 -0.8799 -0.2509 -0.3091 -0.0949  0.0623
#> -0.8670 -0.9540 -0.1401  0.0546 -0.2641  0.0865
#> -1.0220 -0.9911 -0.2694 -0.2182 -0.2077  0.1962
#> -1.1641 -1.3122 -0.2879 -0.2485 -0.2077  0.3806
#> -1.6032 -1.7939 -0.3433 -0.2788 -0.2641  0.4618
#> -1.2932 -1.0529 -0.3617 -0.3091 -0.2641  0.4245
#> -1.4095 -1.5963 -0.3617 -0.3091 -0.2641  0.3608
#> -1.7065 -1.6704 -0.3617 -0.3091 -0.2641  0.3038
#> -1.9131 -1.9174 -0.3617 -0.3091 -0.2641  0.2884
#> -2.1198 -2.0780 -0.3617 -0.3091 -0.2641  0.2928
#> -2.1972 -2.1521 -0.3617 -0.3091 -0.2641  0.3060
#> -2.0294 -1.9545 -0.3617 -0.3091 -0.2641  0.3433
#> -1.3320 -1.0652 -0.3617 -0.3091 -0.2641  0.2401
#> -1.2157 -1.0652 -0.3617 -0.3091 -0.2641  0.0974
#> -0.8800 -0.8799 -0.0662 -0.3091  0.1870  0.0338
#> -0.3892 -0.3118  1.1342 -0.2788  1.9913  0.0030
#> -0.3634 -0.4353  0.3401 -0.3091  0.8072 -0.1001
#> -0.4538 -0.4724 -0.3617 -0.3091 -0.2641 -0.2340
#> -0.6733 -0.7688 -0.3617 -0.3091 -0.2641 -0.1813
#> [ CPUFloatType{28,6} ]
#> 
#> $y
#> torch_tensor
#> -0.8771
#> -1.3207
#> -1.4820
#> -1.5223
#> -1.4619
#> -1.7542
#> -1.8046
#> [ CPUFloatType{7,1} ]
#>