# [R-sig-ME] Retrieve autocorrelation-corrected errors from gls (nlme) or gamm (mgcv)

Daniel Malter daniel at umd.edu
Tue Aug 21 22:23:43 CEST 2012

```Hi,

In the example below, I am modeling the dependent variable Y as a function of X when the errors are autoregressive at the first lag.  There is a number of ways/functions with which to model this. arima (tseries), gls (nlme), and gamm should produce similar results in the simulated example below, and they do.

However, I need the residuals from this analysis and both gls and gamm seem to return the errors before correction for autocorrelation, whereas arima returns the corrected errors (i.e., the estimate of the innovation). My question is whether there is an easy way to retrieve the corrected errors from gamm or gls. My real data are panel data with AR3 errors for which I model nonlinear effects of the independent variables on the dependent variable. Hand-computing the appropriate errors would be painful.

#simulate data with AR1 errors

set.seed(394857395)
e<-rnorm(101)
e<-e[2:101]+0.5*e[1:100]
x<-rnorm(100)
y<-x+e

#OLS for comparison
reg<-lm(y~x)
summary(reg)
pacf(residuals(reg))

#arima
require(tseries)
reg1<-arima(y,order=c(1,0,0),xreg=x)
reg1

#gls
require(nlme)
reg2<-gls(y~x,correlation=corAR1())
summary(reg2)

#gamm
require(mgcv)
reg3<-gamm(y~s(x),correlation=corAR1())
summary(reg3\$lme)

par(mfcol=c(1,3))
pacf(residuals(reg1),main="ARIMA",lag.max=10)
pacf(residuals(reg2),main="GLS",lag.max=10)
pacf(residuals(reg3\$lme),main="GAMM",lag.max=10)

Thanks,
Daniel

```