# Arima residual calculation and comparison with R

I have simulated an ARIMA(1,0,1) process using R. Below is the code.

library(stats)
library(forecast)
set.seed(101)
y <- arima.sim(n=1000,list(ar=0.2,ma=0.6))
plot(y)
qa <- arima(y,order=c(1,0,1))


Below is the output:

ARIMA(1,0,1) with non-zero mean

Coefficients:
ar1     ma1  intercept
0.2022  0.5895    -0.0713
s.e.  0.0430  0.0349     0.0605

sigma^2 estimated as 0.9236:  log likelihood=-1379.57
AIC=2767.14   AICc=2767.18   BIC=2786.77


I'm trying to see if the estimated residuals matches the calculated residuals for example, an ARMA (1,0,1) residual can be written as: $$e_{t} = y_{t} - \mu -\phi y_{t-1}+\theta e_{t-1}$$ $$\mu = intercept$$ $$\phi=ar1 - coefficient$$ $$\theta = ma1 - coefficient$$

Code for calculating residuals

      n = length(y)
e = rep(1,n)
e[1] = 0 ##since there is no residual at 1, e1 = 0
for (t in (2 : n)){
e[t] = y[t] +0.0713 - 0.2022 *y[t-1] + 0.5895*e[t-1]
}


Inputting all the numbers into equation say for residual 10

$$e_{10} = y_{10} - (-0.0713) -0.2022y_{9} +0.5895e_{9}$$

calculated residual is:

> e[10]
[1] -0.8278073


residual from running arima in R is

> qa\$residuals[10]
[1] -0.1424167


The estimated and calculated numbers don't match.

My questions are

1. Am I doing something incorrect here ?
2. Why is there a difference between calculated and estimated numbers?