[R] Fitted values from AR model

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sat Aug 13 20:46:40 CEST 2022


I have not looked into this. R is open source so you can download the
source code and look at it to try and figure it out.
I do not know if the values are configurable.

If you assume that Equation (1) is applicable for t=1 and t=2, then
you can back out values for y(0) and y(1).
Explicitly, if you enter

> fit

you will get the coefficients of the fit: ar1, ar2 and 'intercept'. In
the notation I used in Equation (1) these map to

a1 = ar1, a2 = ar2 and    a0 = intercept / (1 - ar1 - ar2 )

You then have

fitted(2) = a0 + a1 * y(1) + a2 * y(0)

All the values in this expression are known except for y(0), so you
can back out y(0).
Finally

fitted(1) = a0 + a1 * y(0) + a2 * y(-1)

Again, all the values are known except for y(-1) so you can back out y(-1).

I did this manually and got a0 = 7.502621, y(0) = 7.932861 and y(-1) =
6.475706, but I could easily have made a mistake so you
might want to try this yourself.

Best,
Eric


On Sat, Aug 13, 2022 at 8:18 PM bogus christofer
<bogus.christofer using gmail.com> wrote:
>
> Thanks Eric.
>
> Is it possible to know what R uses as the initial values? Are these configurable?
>
> On Fri, 12 Aug 2022 at 13:44, Eric Berger <ericjberger using gmail.com> wrote:
>>
>> The model that you are fitting to the data is an AR(2) model, which means
>>
>> y(t) = a0 + a1 * y(t-1) + a2 * y(t-2) + eps(t)                 (1)
>>
>> The fitting procedure estimates the coefficients a0, a1, a2 (and the
>> variance of eps).
>>
>> After the coefficients have been estimated, the fitted values can be
>> calculated using equation (1) (setting eps(t) = 0)
>> using
>> fitted(t) = a0 + a1 * y(t-1) + a2 * y(t-2)
>>
>> Assuming the series is given for t=1,2,...,20, there is no problem to
>> apply equation (1) to get the
>> fitted values for t=3,4,...,20. But there is a problem for t=1 and
>> t=2. For example, for t=1, equation (1)
>> says
>>
>> fitted(1) = a0 + a1 * y(0) + a2 * y(-1)
>>
>> But there are no values given for y(0) and y(-1). So, either no fitted
>> values should be given for t=1,2, or some
>> other method is being used. Apparently, the arima functions in R and
>> python use different methodology
>> to generate these two fitted points. (For all the other values, the
>> fits are extremely close.)
>>
>> HTH,
>> Eric
>>
>> On Thu, Aug 11, 2022 at 9:53 PM bogus christofer
>> <bogus.christofer using gmail.com> wrote:
>> >
>> > Hi,
>> >
>> > I have below AR model and fitted values from the forecast package
>> >
>> > library(forecast)
>> > dta = c(5.0, 11, 16, 23, 36, 58, 29, 20, 10, 8, 3, 0, 0, 2, 11, 27, 47, 63,
>> > 60, 39)
>> > fit <- arima(dta, order=c(2,0,0))
>> > fitted(fit)
>> >
>> > This gives fitted values as
>> >
>> > Time Series:
>> > Start = 1
>> > End = 20
>> > Frequency = 1
>> >  [1] 13.461017  9.073427 18.022166 20.689420 26.352282 38.165635 57.502926
>> > 9.812106 15.335303  8.298995 11.543320  6.606999  5.800820  7.502621
>> > 9.930962 19.723966 34.045298 49.252447 57.333846 44.615067
>> >
>> >
>> > However when I compare this result with Python, I see significant
>> > difference particularly in the first few values as below
>> >
>> > from statsmodels.tsa.arima.model import ARIMA
>> > dta = [5.0, 11, 16, 23, 36, 58, 29, 20, 10, 8, 3, 0, 0, 2, 11, 27, 47, 63,
>> > 60, 39]
>> > fit = ARIMA(dta, order=(2, 0, 0)).fit()
>> > fit.predict()
>> >
>> > array([21.24816788, 8.66048306, 18.02197059, 20.68931006,
>> > 26.35225759,38.16574655, 57.503278 , 9.81253693, 15.33539514,
>> > 8.29894655,11.54316056, 6.60679489, 5.80055038, 7.50232004,
>> > 9.93067155,19.72374025, 34.04524337, 49.25265365, 57.3343347 , 44.6157026 ])
>> >
>> > Any idea why there are such difference between R and Python results will be
>> > very helpful.
>> >
>> > Thanks,
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list