# Fitting model AR(1) with R

I've sampled 100 variables from a Gauss distribution with mean 0 and standard deviation 1.

> set.seed(1)
> wn=rnorm(100)


Then I've fitted an AR(1) model with the arima command and sent the results to the wnF variable

> wnF=arima(wn, order=c(1,0,0))


Finally I've requested the estimated coefficients

> wnF$coef ar1 intercept -0.003655755 0.108935363  Now I want to replicate the computation R. I exported the data to an Excel file (https://www.dropbox.com/s/6c8ukcbtxe9gqp1/DataTester.xlsx?dl=0) and computed the following model: $$x{_t}-\mu = \psi_1x_{t-1}+\omega_t$$ I've replaced$\mu$and$\psi_1$with the intercept and ar1 coefficients reported by the wnF$coef command. I've also replaced $\omega_t$ with zero, since I've sampled the data from a zero-mean population.

Finally I've compared the residuals from the R computed model (wnF$residuals) with the residuals I've computed in the Excel file and I've noticed that they differ about$\delta<0.0005$in absolute value. I know that 0.0005 is not much, but when dealing with such small values it may not be negligible. I also find strange that there is no difference up the fourth decimal place. Can you please help me finding the origin of the difference? • You should have$x_t-\mu=\psi_1(x_{t-1}-\mu)+w_t$, that is, you should include the mean not only on the left hand side but also on the right hand side. Also,$w_t$need not be zero, or actually, it should not be zero. – Richard Hardy Dec 1 '15 at 11:11 • Subtracting the mean on the right side greatly improved the model. The difference between R computed residuals and Excel computed residuals is now less than 0.000001. Regarding$\omega_t$, I really don't know how to estimate it. – Eduardo Dec 1 '15 at 11:28 • @Eduardo if you wish to estimate an AR(q) model in R, why not just use the LM command? It is much more (IMO) intuitive than the arima-command. – Repmat Dec 1 '15 at 12:28 • @Repmat the estimates of AR coefficients produced by lm (what I assume you mean by "LM") are not maximum likelihood estimates and will not be the same as those produced by arima. – Glen_b -Reinstate Monica Dec 1 '15 at 12:52 ## 1 Answer After some research I found out that the difference between R computed residuals given by wnF$residuals and the residuals I've computed externaly in Excel file, was originated from the lack of precision of the data passed to Excel.

At first, I had passed data to Excel with only 7 decimal places. After repeating the procedure with 15 decimal places, the difference almost disapeared.

Also, as Richard Hardy commented, the model I was fitting was not correct. The correct model is:

$$x_t-\mu=\psi_1(x_{t-1}-\mu)+\omega_t$$