residuals in R using auto.arima and forecast package - r

I am fitting a model using the auto.arima function in package forecast. I get a model that is AR(1), for example. I then extract residuals from this model. How does this generate the same number of residuals as the original vector? If this is an AR(1) model then the number of residuals should be 1 less than the dimensionality of the original time series. What am I missing?
Example:
require(forecast)
arprocess = as.numeric(arima.sim(model = list(ar=.5), n=100))
#auto.arima(arprocess, d=0, D=0, ic="bic", stationary=T)
# Series: arprocess
# ARIMA(1,0,0) with zero mean
# Coefficients:
# ar1
# 0.5198
# s.e. 0.0867
# sigma^2 estimated as 1.403: log likelihood=-158.99
# AIC=321.97 AICc=322.1 BIC=327.18
r = resid(auto.arima(arprocess, d=0, D=0, ic="bic", stationary=T))
> length(r)
[1] 100
Update: Digging into the code of auto.arima, I see that it uses Arima which in turn uses stats:::arima. Therefore the question is really how does stats:::arima compute residuals for the very first observation?

The residuals are the actual values minus the fitted values. For the first observation, the fitted value is the estimated mean of the process. For subsequent observations, the fitted value is $\phi$ times the previous observation, assuming an AR(1) process had been estimated.

Related

In the auto.arima() function in R, how do I find the p,d,q values for that arima

I used an R code with an auto.arima function on a time series data set to forecast. From here, Id like to know how to find the p,d,q values for the arima. Is there a quick way to determine that, thank you.
The forecast::auto.arima() function was written to pick the optimal p, d, and q with respect to some optimization criterion (e.g. AIC). If you want to see which model was picked, use the summary() function.
For example:
fit <- auto.arima(lynx)
summary(fit)
Series: lynx
ARIMA(2,0,2) with non-zero mean
Coefficients:
ar1 ar2 ma1 ma2 mean
1.3421 -0.6738 -0.2027 -0.2564 1544.4039
s.e. 0.0984 0.0801 0.1261 0.1097 131.9242
sigma^2 estimated as 761965: log likelihood=-932.08
AIC=1876.17 AICc=1876.95 BIC=1892.58
Training set error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set -1.608903 853.5488 610.1112 -63.90926 140.7693 0.7343143 -0.01267127
Where you can see the particular specification in the second row of the output. In this example, auto.arima picks an ARIMA(2,0,2).
Note that I did this naively here for demonstration purposes. I didn't check whether this is an accurate representation of the dependency structure in the lynx data set.
Other than summary(), you could also use arimaorder(fit) to get the vector c(p,d,q) or as.character(fit) to get "ARIMA(p,d,q)".

Reproduce ARIMA Forecast (Coefficients from R Arima())

I am quite new to the R and the ARIMA model, and I have a question on the ARIMA model that I obtained in R.
I will use the US unemployment rate as an example, the data range is from Jan, 1948 to Feb, 2015, total of 806 observations. After looking at the AICc, I decided to use ARIMA(2,1,2) model. (BTW I am using Arima() function from "forecast" package in R)
The output is the following:
Series: log.unemp
ARIMA(2,1,2)
ar1 1.6406
ar2 -0.7499
ma1 -1.5943
ma2 0.7893
sigma^2 estimated as 0.001307: log likelihood=1530.14
AIC=‐3050.27 AICc=‐3050.2 BIC=‐3026.82
The code is
fit.best <- Arima(log.unemp, c(2, 1, 2), include.constant=FALSE)
print(fit.best)
Then I want to measure the forecast performance of this model. That is, to calculate things like RMSE, Theil's U, etc. But I do not know how to do that. The reason is that it seems that I do not know how to derive the forecast equation from this output to calculate the fitted values.
So could anyone help me on this? How should I derive the forecast equation from this output? Also, after obtaining the equation, how can I do the forecast in Excel to calculate the fitted values from the first data point (there are some numbers that are not available when you are calculating the fitted value for t=1)?
Thanks!
you can use summmary(fit.best) to view RMSE.
Or if you want to caluate by yourself you can derive residuals and fitted values like this:
fitted=log.unemp-fit.best$residuals
about the equation see this
You can use forecast package
fit.best <- Arima(log.unemp, c(2, 1, 2), include.constant=FALSE)
my_forecast <- forecast(fit.best, h=10)
my_forecast #will show the next 10 periods
# or use some detailed data like
plot(my_forecast$residuals)
Use arima model as below code:
arimafit = arima(log.unemp, order=c(2,1,2))
Forecast using the below code:
arima_future = forecast(arimafit, h=3)
where forecast is the function to forecast for next whatever months you want.
h=3 means it will forecast for next 3 months.
If you want to check RMSE on the test data you can use DMwr package:
metrics = as.data.frame(DMwR::regr.eval(<test_data_vector>, arima_future$point_forecast))
test_data_vector - is the test data vector which you can create while dividing your main dataset into train and test dataset.
arima_future$point_forecast - Is the vector point forecast vector you will get in step2.

arima estimate validation through arima.sim

This is out of my curiosity trying to compare time series input to an ARMA model and reconstructed series after an ARMA estimate is obtained. These are the steps I am thinking:
construct simulation time series
arma.sim <- arima.sim(model=list(ar=c(0.9),ma=c(0.2)),n = 100)
estimate the model from arma.sim, assuming we know it is a (1,0,1) model
arma.est1 <- arima(arma.sim, order=c(1,0,1))
also say we get arma.est1 in this form, which is close to the original (0.9,0,0.2):
Coefficients:
ar1 ma1 intercept
0.9115 0.0104 -0.4486
s.e. 0.0456 0.1270 1.1396
sigma^2 estimated as 1.15: log likelihood = -149.79, aic = 307.57
If I try to reconstruct another time series from arma.est1, how do I incorporate intercept or s.e. in arima.sim? Something like this doesn't seem to work well because arma.sim and arma.rec are far off:
arma.rec <- arima.sim(n=100, list(ar=c(0.9115),ma=c(0.0104)))
Normally we use predict() to check the estimate. But is this a legit way to look at the estimate?

Estimating mean time to failure with survreg/flexsurvreg with standard error

I am trying to estimate the mean time to failure for a Weibull distribution fitted to some survival data with flexsurvreg from the flexsurv package. I need to be able to estimate the standard error for use in a simulation model.
Using flexsurvreg with the lung data as an example;
require(flexsurv)
lungS <- Surv(lung$time,lung$status)
lungfit <- flexsurvreg(lungS~1,dist="weibull")
lungfit
Call:
flexsurvreg(formula = lungS ~ 1, dist = "weibull")
Maximum likelihood estimates:
est L95% U95%
shape 1.32 1.14 1.52
scale 418.00 372.00 469.00
N = 228, Events: 165, Censored: 63
Total time at risk: 69593
Log-likelihood = -1153.851, df = 2
AIC = 2311.702
Now, calculating the mean is just a case of plugging in the estimated parameter values into the standard formula, but is there an easy way of getting out the standard error of this estimate? Can survreg do this?
In flexsurv version 0.2, if x is the fitted model object, then x$cov is the covariance matrix of the parameter estimates, with positive parameters on the log scale. You could then use the asymptotic normal property of maximum likelihood estimators. Simulate a large number of multivariate normal vectors, with the estimates as means, and this covariance matrix (using e.g. rmvnorm from the mvtnorm package). This gives you replicates of the parameter estimates under sampling uncertainty. Calculate the corresponding mean survival for each replicate, then take the SD or quantiles of the resulting sample to get the standard error or a confidence interval.

Explaining the forecasts from an ARIMA model

I am trying to explain to myself the forecasting result from applying an ARIMA model to a time-series dataset. The data is from the M1-Competition, the series is MNB65. I am trying to fit the data to an ARIMA(1,0,0) model and get the forecasts. I am using R. Here are some output snippets:
> arima(x, order = c(1,0,0))
Series: x
ARIMA(1,0,0) with non-zero mean
Call: arima(x = x, order = c(1, 0, 0))
Coefficients:
ar1 intercept
0.9421 12260.298
s.e. 0.0474 202.717
> predict(arima(x, order = c(1,0,0)), n.ahead=12)
$pred
Time Series:
Start = 53
End = 64
Frequency = 1
[1] 11757.39 11786.50 11813.92 11839.75 11864.09 11887.02 11908.62 11928.97 11948.15 11966.21 11983.23 11999.27
I have a few questions:
(1) How do I explain that although the dataset shows a clear downward trend, the forecast from this model trends upward? This also happens for ARIMA(2,0,0), which is the best ARIMA fit for the data using auto.arima (forecast package) and for an ARIMA(1,0,1) model.
(2) The intercept value for the ARIMA(1,0,0) model is 12260.298. Shouldn't the intercept satisfy the equation: C = mean * (1 - sum(AR coeffs)), in which case, the value should be 715.52. I must be missing something basic here.
(3) This is clearly a series with non-stationary mean. Why is an AR(2) model still selected as the best model by auto.arima? Could there be an intuitive explanation?
Thanks.
No ARIMA(p,0,q) model will allow for a trend because the model is stationary. If you really want to include a trend, use ARIMA(p,1,q) with a drift term, or ARIMA(p,2,q). The fact that auto.arima() is suggesting 0 differences would usually indicate there is no clear trend.
The help file for arima() shows that the intercept is actually the mean. That is, the AR(1) model is (Y_t-c) = ϕ(Y_{t-1} - c) + e_t rather than Y_t = c + ϕY_{t-1} + e_t as you might expect.
auto.arima() uses a unit root test to determine the number of differences required. So check the results from the unit root test to see what's going on. You can always specify the required number of differences in auto.arima() if you think the unit root tests are not leading to a sensible model.
Here are the results from two tests for your data:
R> adf.test(x)
Augmented Dickey-Fuller Test
data: x
Dickey-Fuller = -1.031, Lag order = 3, p-value = 0.9249
alternative hypothesis: stationary
R> kpss.test(x)
KPSS Test for Level Stationarity
data: x
KPSS Level = 0.3491, Truncation lag parameter = 1, p-value = 0.09909
So the ADF says strongly non-stationary (the null hypothesis in that case) while the KPSS doesn't quite reject stationarity (the null hypothesis for that test). auto.arima() uses the latter by default. You could use auto.arima(x,test="adf") if you wanted the first test. In that case, it suggests the model ARIMA(0,2,1) which does have a trend.

Resources