Skip to content

Instantly share code, notes, and snippets.

@atyre2
Created May 12, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atyre2/5f1d240496d57528558f289fb6144666 to your computer and use it in GitHub Desktop.
Save atyre2/5f1d240496d57528558f289fb6144666 to your computer and use it in GitHub Desktop.
Trying out gamm() with auto-correlated errors and Poisson error distribution
n <- 200;sig <- 2
x <- 0:(n-1)/(n-1)
f <- 0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10
e <- rnorm(n,0,sig)
for (i in 2:n) e[i] <- 0.6*e[i-1] + e[i]
y <- f + e
op <- par(mfrow=c(2,2))
## Fit model with AR1 residuals
b <- gamm(y~s(x,k=20),correlation=corAR1())
plot(b$gam);lines(x,f-mean(f),col=2)
## Raw residuals still show correlation, of course...
acf(residuals(b$gam),main="raw residual ACF")
## But standardized are now fine...
acf(residuals(b$lme,type="normalized"),main="standardized residual ACF")
## compare with model without AR component...
b <- gam(y~s(x,k=20))
plot(b);lines(x,f-mean(f),col=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment