0
$\begingroup$

I'm using the tsCV function from the forecast package in R. I'm comparing arima with and without xreg. Without xreg I have no problems. But when I add xreg to the tsCV function I get a matrix of NAs.

I thought I might need length(xreg) = length(y) + horizon. But if I do that I get an error: xreg must be of the same size as y.

Here is a reproducible example:

library(forecast)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Registered S3 methods overwritten by 'forecast':
#>   method             from    
#>   fitted.fracdiff    fracdiff
#>   residuals.fracdiff fracdiff

stack_data <- ts(rnorm(100), start = 2000, frequency = 12)
stack_data_xreg <- ts(rnorm(100), start = 2000, frequency = 12)


farma <- function(x, h){forecast(auto.arima(x), h = h)}
tsCV(stack_data, farma)
#>              Jan         Feb         Mar         Apr         May
#> 2000  0.40563034  0.17463799 -0.77045150 -1.26653788 -0.54549741
#> 2001 -0.82662134 -0.12712224 -0.16689046 -0.28039922 -0.14633838
#> 2002  1.00869308 -0.10646429 -0.11733494 -0.98269977  2.02888513
#> 2003  0.08992425 -0.04478466  1.33947694  0.61512603 -1.47779195
#> 2004  0.62197916 -0.31285023 -1.87748218  0.05545157  0.79404086
#> 2005  1.34079217  1.16067205 -0.31473379  1.28361957  0.26702644
#> 2006 -0.20387530  0.14089200  0.09655947 -1.68903183 -1.74576484
#> 2007 -1.47340231  2.07289246 -1.88595924 -0.08185593  0.66826102
#> 2008  1.46971617  0.56345483 -1.39370468          NA            
#>              Jun         Jul         Aug         Sep         Oct
#> 2000  0.03088558 -1.56988024  0.36739705  0.19510088  0.35736462
#> 2001  0.61791686 -0.34181673 -0.38493804  1.65816962 -0.09582700
#> 2002 -1.40810233 -1.49081572  0.45128015 -0.22893364  0.14057981
#> 2003 -1.18764843  0.05096855  1.36152907 -0.11422859  0.27291010
#> 2004  1.85680313  1.88050368 -1.03857395 -1.92289436  1.04696123
#> 2005  1.03188951  0.89751963  1.92407988  1.35389806  0.89851252
#> 2006 -0.01003884  0.70429687 -0.71814159 -0.42640676  1.37978749
#> 2007 -1.19165002  1.02750038 -0.23383838 -0.76811251  1.02074594
#> 2008                                                            
#>              Nov         Dec
#> 2000  1.19798923  1.59771830
#> 2001  0.68061128  0.59334251
#> 2002  0.41844484 -0.90827529
#> 2003  0.05572988 -1.41338097
#> 2004  1.21183965  0.82382650
#> 2005  0.63131654  1.40116039
#> 2006  0.13046189 -1.71495153
#> 2007 -1.62466898  0.63538448
#> 2008
tsCV(stack_data, farma, xreg = stack_data_xreg)
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 2000  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2001  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2002  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2003  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2004  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2005  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2006  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2007  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
#> 2008  NA  NA  NA  NA

Created on 2019-10-10 by the reprex package (v0.3.0)

I tried to change my farma function as follows:
farma_x <- function(x, h, xreg){forecast(auto.arima(x, xreg = xreg), h = h)}

but that did not work.

I have no problem when I run auto.arima with xreg, only when I try to use tsCV

$\endgroup$
0
$\begingroup$

You need to split the xreg matrix into two pieces, one used for training and one for forecasting. The following code should fix the problem.

library(forecast)
set.seed(1)
stack_data <- ts(rnorm(100), start = 2000, frequency = 12)
stack_data_xreg <- ts(rnorm(100), start = 2000, frequency = 12)

farma <- function(y, h, xreg) {
  ncol <- NCOL(xreg)
  X <- matrix(xreg[seq_along(y), ], ncol = ncol)
  if (NROW(xreg) < length(y) + h) {
    stop("Not enough xreg data for forecasting")
  }
  newX <- matrix(xreg[length(y) + seq(h), ], ncol = ncol)
  fit <- auto.arima(y, xreg = X)
  forecast(fit, xreg = newX, h = h)
}

tsCV(stack_data, farma, xreg = stack_data_xreg)
#>               Jan          Feb          Mar          Apr          May
#> 2000  0.810097135           NA  1.304257208  0.682981005 -5.610649793
#> 2001 -2.300753372  1.122943604 -0.056166686 -0.024771800  0.936527913
#> 2002 -0.083400817 -0.153262209 -1.469429055 -0.451752087  0.436745959
#> 2003 -0.021221494  1.147379540  0.765396693 -0.093538812 -0.305109643
#> 2004  0.951795652  0.395926226 -0.611855910  0.344129361 -1.123042184
#> 2005 -0.051414670  0.743490403  0.059981832 -0.765007510  0.247113268
#> 2006 -0.935160311 -1.365429702  0.291221704 -0.438080942  0.025365086
#> 2007  0.336816520  1.028414077 -0.271960523  0.389200605  0.306516552
#> 2008 -0.713072219 -1.223829836 -0.476239935           NA             
#>               Jun          Jul          Aug          Sep          Oct
#> 2000  0.525713374  0.722321499  0.527858032 -0.569519802  1.544766085
#> 2001  0.844427791  0.591343448  0.907552931  0.859488240  0.078810418
#> 2002  1.355720366 -0.070836700  0.357486229  0.049680596 -1.396737647
#> 2003  0.748322301  0.551776497 -0.709934151 -0.702899128  0.317519688
#> 2004  1.480106472  1.960889994 -0.302600323 -1.088335143  0.491245564
#> 2005 -1.809366181  1.451960439  0.147030897  2.181626321  0.558826680
#> 2006  0.085603679 -0.577727812 -0.592174595 -0.125793946  1.180536801
#> 2007 -0.535583516  1.191666401  1.194077284  0.670606300  1.637822904
#> 2008                                                                 
#>               Nov          Dec
#> 2000  0.366156757 -0.522869056
#> 2001 -1.986002110  0.624077286
#> 2002 -0.350771686 -0.374158943
#> 2003  0.767824690 -0.059505618
#> 2004 -0.002975468  2.431699557
#> 2005 -0.708162439  0.618942712
#> 2006 -1.534986325  0.583242325
#> 2007  0.437497859 -1.456253804
#> 2008

Created on 2019-10-11 by the reprex package (v0.3.0)

$\endgroup$
  • $\begingroup$ Thank yo so much. This worked. $\endgroup$ – Viðar Ingason Oct 11 at 9:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.