0
$\begingroup$

Following the post here, I came with another issue that is related to another topic. I am using PCA to use K number of principal components as exogenous regressors to use in an auto.arima model in R. These principal components are input in the parameter "xreg." The main problem I am having now is that the number of variables is larger than the number of observations of the data I am trying to fit. So when I choose, say 5 PCA, the number of rows is bigger than the number of observations in auto.arima and an error appears as:

Error in model.frame.default(formula = x ~ xreg, drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'xreg')
In addition: Warning message:
In !is.na(x) & !is.na(rowSums(xreg)) :
  longer object length is not a multiple of shorter object length

One way of solving this problem is to choose the number of rows with higher absolute values, so that they match the number of observations in the auto.arima. What do you think?

$\endgroup$
3
  • $\begingroup$ Looking at it now, I think this will not make sense. Is the best way just to cut the resulting PCA matrix according to the size of the data to predict? For example, if we have 38 observations, should we cut off the PCA matrix to 38 rows? $\endgroup$
    – ruthy_gg
    Commented Jul 31, 2016 at 21:51
  • $\begingroup$ Another approach could be to use the PCs to predict all the regressors and then use these predictions in the xreg parameter. In this case I could split this data in training and testing xregs based on those predictions. I took the idea from here : tgmstat.wordpress.com/2013/11/28/… (the last PC values will have the same number of rows as the number of observations) $\endgroup$
    – ruthy_gg
    Commented Aug 1, 2016 at 11:11
  • $\begingroup$ I was going through my old answers and noticed this one was not accepted. Do you perhaps need further clarification? $\endgroup$ Commented Feb 20, 2017 at 15:25

1 Answer 1

2
$\begingroup$

This error message seems to indicate not that the number of variables exceeds the number of observations but rather that the exogenous variables have a different number of observations than the dependent variable. You should make sure that the variable lengths coincide, i.e. dim(xreg)[1]==length(y).

For example, if we have 38 observations, should we cut off the PCA matrix to 38 rows?

You would use the same (sub-)sample for running the PCA and fitting the regression with ARIMA errors. Therefore, there should not be a discrepancy in variable lengths at this stage. Next, when you do $h$-step ahead forecasting, you need to make sure to supply $h$-long predicted vectors of the exogenous variables.

Another approach could be to use the PCs to predict all the regressors and then use these predictions in the xreg parameter.

Obtaining PCs from regressors first and reconstructing regressors back from PCs does not seem to add value in this exercise. But probably I do not get your idea.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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