I have a set of data that is in the form of the following:
library(tidyverse)
library(readxl)
library(tsibble)
library(tsibbledata)
library(purrr)
library(lubridate)
library(fable)
library(fabletools)
library(tidymodels)
library(feasts)
data <- tibble(Date=yearweek(mdy("5/6/2022","5/14/2022","5/21/2022")),
value=c(3,5,7))
ts_data <- tsibble(data)
new_test_data <- tibble(`Formula ID` = c(1),
`Formula Name` = c("Flubber"),
data=list(data),data_ts=list(ts_data))
This gives me tibble that looks like the following:
Formula ID | Formula Name | data | data_ts |
---|---|---|---|
1 | Flubber | 2 variables | 2 variables |
Where data is a tibble and data_ts is a tsibble. I need to create a model for time series data_ts and forecast the time series. I have been trying to use purrr to map the model as below:
new_test_data < new_test_data %>%
mutate(model = map(data_ts,ARIMA(data_ts,value)))
This does not work, unfortunately. Can someone offer me some pointers on getting this mapping to work to create a ARIMA model and the forecast from the model? I have about 70 data sets within the new_test_data, making it perfect for this type of modelling.
Thank you kindly,
Shawn Way
Maybe this helps to get you started. To estimate an ARIMA or any other model you have to use model()
which as its first argument takes a dataset and one or more model specifications, e.g. ARIMA
. Moreover, when using map
to loop over a list column of datasets you have to pass a single element or dataset to model()
not the list column itself.
library(tidyverse)
library(lubridate)
library(fabletools)
library(fable)
library(tsibble)
new_test_data <- new_test_data %>%
mutate(model = map(data_ts, ~ model(.x, ARIMA(value))))
new_test_data$model |> map(report)
#> Series: value
#> Model: ARIMA(1,0,0) w/ mean
#>
#> Coefficients:
#> ar1 constant
#> 0 5.0000
#> s.e. 1 0.9428
#>
#> sigma^2 estimated as 8: log likelihood=-5.73
#> AIC=17.46 AICc=-6.54 BIC=14.75
#> [[1]]
#> # A mable: 1 x 1
#> `ARIMA(value)`
#> <model>
#> 1 <ARIMA(1,0,0) w/ mean>