I'm new to R Coding and Fable, and is currently working on creating some basic moving average forecast models, and my intention is to create several models taking the mean of different period lengths.
I've tried to specify ~window for MEAN()
, however it returns a NULL
model.
Here is some Test Data:
Units <- sample(1:30)
Date <- ymd(paste0("2019-01-",1:30))
key[1:30] <- "test1"
trial <- bind_cols(Date=Date, Units=Units, key = key) %>%
as_tsibble(index=Date, key=key)
fit <- trial %>%
model(Avg = MEAN(Units~window(size = 12)))
Running the code i get the following result:
> Units <- sample(1:30)
> Date <- ymd(paste0("2019-01-",1:30))
> key[1:30] <- "test1"
> trial <- bind_cols(Date=Date, Units=Units, key = key) %>%
+ as_tsibble(index=Date, key=key)
>
> fit <- trial %>%
+ model(Avg = MEAN(Units~window(size = 12)))
Advarselsbesked:
1 error encountered for Avg
[1] `slide()` was deprecated in tsibble 0.9.0 and is now defunct.
Please use `slider::slide()` instead.
>
> fit
# A mable: 1 x 2
# Key: key [1]
key Avg
<chr> <model>
1 test1 <NULL model>
Any pointers on how I can get MEAN()
to just take the mean of the last 12 observations that would be very much appreciated.
This works using the latest versions of the packages:
library(tsibble)
library(fable)
library(lubridate)
library(dplyr)
Units <- sample(1:30)
Date <- ymd(paste0("2019-01-",1:30))
key <- "test1"
trial <- bind_cols(Date=Date, Units=Units, key = key) %>%
as_tsibble(index=Date, key=key)
fit <- trial %>%
model(Avg = MEAN(Units ~ window(size = 12)))
# .fitted shows mean of previous 12 observations
augment(fit) %>% tail()
#> # A tsibble: 6 x 7 [1D]
#> # Key: key, .model [1]
#> key .model Date Units .fitted .resid .innov
#> <chr> <chr> <date> <int> <dbl> <dbl> <dbl>
#> 1 test1 Avg 2019-01-25 24 16.4 7.58 7.58
#> 2 test1 Avg 2019-01-26 27 18.1 8.92 8.92
#> 3 test1 Avg 2019-01-27 26 18.9 7.08 7.08
#> 4 test1 Avg 2019-01-28 18 20.5 -2.5 -2.5
#> 5 test1 Avg 2019-01-29 11 19.6 -8.58 -8.58
#> 6 test1 Avg 2019-01-30 3 20.1 -17.1 -17.1
# Check
mean(trial$Units[18:29])
#> [1] 20.08333
Created on 2021-08-04 by the reprex package (v2.0.0)