so I have a dataset Y which I have split into a training and test data set. I have used the tsoutliers function tso() to fit a model to my training data which consists of an AR(3) process plus 15 other variables (mostly additive outliers). Now I want to make a one step ahead forecast (for comparison purposes against the test data). However I cannot seem to get the function predict() to work properly because I get the error
Error in predict.Arima(model$fit, n.ahead = 15, newxreg = newxreg) :
'xreg' and 'newxreg' have different numbers of columns
I cannot seem to pass the correct newxreg values. If anyone can help it would be much appreciated. My code so far is below:
train_date <- floor(length(Y)*0.7)
test_date <- train_date + 1
train <- Y[1:train_date, ]
test <- Y[test_date:nrow(Y), ]
test <- ts(test)
train <- ts(train)
model <- tso(train, types = c("AO", "LS", "TC"),tsmethod = c("arima"),cval = 3.5 ,args.tsmethod = list(order =c(3,0,0)))
print(model)
Output:
Coefficients:
ar1 ar2 ar3 intercept TC7 TC87 AO99 AO106 AO121
0.6449 0.1481 0.1380 927.3582 -5872.039 5778.603 7721.161 4723.200 5410.631
s.e. 0.0322 0.0379 0.0316 708.8306 1513.531 1515.876 1319.987 1322.358 1314.757
AO373 AO449 AO468 AO470 AO485 AO491 AO499 AO832
-4794.655 -4887.083 6674.346 -5842.650 -6917.341 4742.169 -5860.072 -5676.016
s.e. 1314.112 1315.353 1321.769 1317.862 1322.005 1318.003 1317.519 1319.347
TC856 TC871
8508.164 -6165.617
s.e. 1519.721 1510.382
sigma^2 estimated as 2515630: log likelihood = -8885.52, aic = 17811.03
Outliers:
type ind time coefhat tstat
1 TC 7 7 -5872 -3.880
2 TC 87 87 5779 3.812
3 AO 99 99 7721 5.849
4 AO 106 106 4723 3.572
5 AO 121 121 5411 4.115
6 AO 373 373 -4795 -3.649
7 AO 449 449 -4887 -3.715
8 AO 468 468 6674 5.050
9 AO 470 470 -5843 -4.433
10 AO 485 485 -6917 -5.232
11 AO 491 491 4742 3.598
12 AO 499 499 -5860 -4.448
13 AO 832 832 -5676 -4.302
14 TC 856 856 8508 5.599
15 TC 871 871 -6166 -4.082
Trying to predict 1 step ahead.
npred <- 1
newxreg <- outliers.effects(model$outliers, length(train) + npred)
newxreg <- ts(newxreg[-seq_along(train),])
print(newxreg)
p <- predict(model$fit, n.ahead=npred, newxreg=newxreg)
When I run this last line of code, I get the error.
My newxreg output:
Time Series:
Start = 1
End = 15
Frequency = 1
TC7 TC87 AO99 AO106 AO121 AO373
2.106348e-156 5.196214e-144 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
AO449 AO468 AO470 AO485 AO491 AO499
0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
AO832 TC856 TC871
0.000000e+00 6.843752e-25 1.441530e-22
I've only been using R for a couple of days, so I apologise if the code is poorly written.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…