I want to build many auto.arima models based on purrr and forecast packages. I can't finish below code, some errors appear.
We can start without reproducible code, if necessary i can provide.
My data:
head(df)
nam period sv
APA 2016-07-03 1895619
APA 2016-07-10 2100690
APA 2016-07-17 2059273
APA 2016-07-24 2073187
APA 2016-07-31 1951968
and my code in R to be finished...
df %>%
nest(-nam) %>%
mutate(ts_data = map(data, tk_ts, select = sv, start = c(2016,26), frequency = 52)) %>%
mutate(harmonics = map(ts_data, fourier, K=24)) %>%
mutate(fitted = map2(.x = ts_data, .y =harmonics, .f= auto.arima, xreg , seasonal = F))
I want to achieved equivalent to this code:
harmonics <- fourier(db, K = 24)
# Fit regression model with ARIMA errors
fit <- auto.arima(db, xreg = harmonics, seasonal = F)
# Forecasts next 46 periods
newharmonics <- fourier(db, K = 24, h = 46)
fc <- forecast(fit, xreg = newharmonics )
Can somebody help me to finish it? Thx with advance
I solved it. It was enough just to put it into formula
`auto_arima = function(df) {
harmonics <- fourier(df, K = 24)
# Fit regression model with ARIMA errors
fit <- auto.arima(df, xreg = harmonics, seasonal = FALSE)
# Forecasts next 46 period
newharmonics <- fourier(df, K = 24, h = 46)
fc <- forecast(fit, xreg = newharmonics)
fc_db = fc %>% as_data_frame() %>% select(`Point Forecast`) %>%
mutate(period = seq.Date(as.Date("2017-10-15"), as.Date("2018-08-27"),by = "week"))
return(fc_db)
}
` and then:
mutate(fitted = map(ts_data, auto_arima))
Sewe