0
$\begingroup$

I'm trying to do a multivariate time series forecasting using dynamic regression. The data was collected for 148 weeks with two main variables- Shipment Qty(dependent variable) and Net.Production.Qty(independent variable).

The training data is first 118 weeks and the test data is remaining 30 weeks.

  1. First, I'm going to forecast the Net Production Qty- test set 30 weeks information using Arima with Fourier term
  2. Second, I'm going to generate the Fourier values and best fit model for Shipment test data set
  3. Third, I'm going to forecast the Shipment Qty using Net.Production.Qty forecasted values(test set 30 weeks) and Fourier values of Shipment data set as co-variates.

P.S. I have edited my input data here to contain only 26 weeks with test data as 13 weeks.

And this is the error I get -

Error in forecast.forecast_ARIMA(bestfit.Shipment, xreg = cbind(final.prod,c: Number of regressors does not match fitted model

Could anyone please help me resolve this error?

library(tidyverse)
library(dplyr)
library(lubridate)
library(ISOweek)
library(feasts)
library(fable)

Shipment.df<-> structure(list(YearWeek = c("201901", "201902", "201903", "201904", "201905", "201906", "201907", "201908", "201909", "201910", "201911", "201912", "201913", "201914", "201915", "201916", "201917", "201918", "201919", "201920", "201921", "201922", "201923", "201924", "201925", "201926"), Shipment = c(418, 1442, 1115, 1203, 1192, 1353, 1191, 
1411, 933, 1384, 1362, 1353, 1739, 1751, 1595, 1380, 1711, 2058, 1843, 1602, 2195, 2159, 2009, 1812, 2195, 1763), Production = c(0, 198, 1436, 1055, 1396, 1330, 1460, 1628, 1513, 1673, 1737, 1274, 1726, 1591, 2094, 1411, 2009, 1909, 1759, 1693, 1748, 1455, 2078, 1717, 1737, 1886), Net.Production.Qty = c(22, 188, 1428, 1031, 1382, 1368, 1456, 1578, 1463, 1583, 1699, 1318, 1582, 1537, 2118, 1567, 1961, 1897, 1767, 1603, 1666, 1419, 2186, 1621, 1677, 1840)), row.names = c(NA, 26L), class = "data.frame")

Shipment.df<-Shipment.df <- Shipment.df %>%
  mutate(isoweek = str_replace(YearWeek,
                               "^(\\d{4})(\\d{2})$",
                           "\\1-W\\2-1"),
     date = ISOweek::ISOweek2date(isoweek))
Shipment2.df<-Shipment.df[,c("Shipment","Production","Net.Production.Qty")]
Shipment.ts<-ts(Shipment2.df,frequency = 365.25/7,start = c(2019,1))
Shipment.df$date<-as.Date(Shipment.df$date)
Shipment.train.df<-with(Shipment.df,Shipment.df[(Shipment.df$date >= "2018-12-31" & Shipment.df$date <= "2019-03-25"),])
Net.Production.df<-Shipment.train.df[,c("Net.Production.Qty")]
Net.Production.train.ts<-ts(Net.Production.df,frequency = 365.25/7,start = c(2019,1))
bestfit.Net.Prod <- list(aicc=Inf)
for(K in seq(25))
{
  fit.Net.Prod <- auto.arima(Net.Production.train.ts, xreg=fourier(Net.Production.train.ts, K=K), seasonal=FALSE)
  if(fit.Net.Prod$aicc < bestfit.Net.Prod$aicc)
  {
bestfit.Net.Prod <- fit.Net.Prod
bestK.Net.Prod <- K
  }
}
forecast.net.prod<- forecast(bestfit.Net.Prod,xreg = fourier(Net.Production.train.ts,K=bestK.Net.Prod,h=13))
final.prod<-forecast.net.prod$mean

Shipment4.df<-Shipment.train.df[,c("Shipment")]
Shipment.train.ts<-ts(Shipment4.df,frequency = 365.25/7,start = c(2019,1))
bestfit.Shipment <- list(aicc=Inf)
for(K in seq(25))
{
  fit.Shipment <- auto.arima(Shipment.train.ts, xreg=fourier(Shipment.train.ts, K=K), seasonal=FALSE)
  if(fit.Shipment$aicc < bestfit.Shipment$aicc)
  {
    bestfit.Shipment <- fit.Shipment
    bestK.Shipment <- K
  }
}
fourier.shipment<-fourier(Shipment.train.ts,K=bestK.Shipment,h=13)
forecast.shipment<-forecast(bestfit.Shipment,xreg =cbind(final.prod,fourier.shipment))

$\endgroup$
6
  • 1
    $\begingroup$ I strongly, strongly suspect that if you start paring down your code to get a Minimal Working Example, you will find the problem yourself. And if it still persists with an MWE, it will be much easier for us to find it and help you if you post that MWE. $\endgroup$ Nov 23 at 13:41
  • $\begingroup$ @StephanKolassa Thank you. I have edited my input data to contain only 26 weeks as a reference dataset. And I still couldn't figure out the error that I have mentioned.. $\endgroup$ Nov 23 at 14:48
  • 1
    $\begingroup$ It's not a question of using less input data, rather one of reducing your code. How much of what you post is truly minimally necessary to replicate the error? Look at every single line and think about whether you can't remove it. For instance, I see a for loop. Is that necessary? Yes, cutting your code down is an effort. But it will definitely be easier for you to do than for us, since you know what your code is about. $\endgroup$ Nov 23 at 14:52
  • $\begingroup$ Given the error obtained and my understanding of your code, you must add the Net.Production.train.ts series as an external regressor (xreg) when fitting the fit.Shipment model. $\endgroup$ Nov 23 at 14:53
  • 1
    $\begingroup$ Why are you using the ts function when you are using the fable and feasts packages. You are better of using tsibbles. You can compare the models a lot easier than how you are doing it now. Go through the fpp3 book to see some examples. $\endgroup$
    – phiver
    Nov 24 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.