# [R] Fitted values from AR model

Eric Berger er|cjberger @end|ng |rom gm@||@com
Fri Aug 12 10:13:40 CEST 2022

```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
>
> 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
> and provide commented, minimal, self-contained, reproducible code.

```