[R] gls prediction using the correlation structure in nlme
Dr Carbon
drcarbon at gmail.com
Fri Jan 30 02:28:44 CET 2009
On Wed, Jan 28, 2009 at 9:51 AM, Dr Carbon <drcarbon at gmail.com> wrote:
> How does one coerce predict.gls to incorporate the fitted correlation
> structure from the gls object into predictions? In the example below
> the AR(1) process with phi=0.545 is not used with predict.gls. Is
> there another function that does this? I'm going to want to fit a few
> dozen models varying in order from AR(1) to AR(3) and would like to
> look at the fits with the correlation structure included.
>
> Thanks in advance.
>
> -JC
>
> PS I am including the package maintainers on this post - does this
> constitute a maintainer-specific question in r-help etiquette?
>
> # example
> set.seed(123)
> x <- arima.sim(list(order = c(1,0,0), ar = 0.7), n = 100)
> y <-x + arima.sim(list(order = c(1,0,0), ar = 0.7), n = 100)
> x <- c(x)
> y <- c(y)
> lm1 <- lm(y~x)
> ar(residuals(lm1)) # indicates an ar1 model
> cs1 <- corARMA(p=1)
> fm1 <- gls(y~x,corr=cs1)
> summary(fm1)
> # get fits
> fits <- predict(fm1)
> # use coef to get fits
> fits2 <- coef(fm1)[1] + coef(fm1)[2] * x
> plot(fits,fits2)
>
I think this is the way to do this?
b0 <- coef(fm1)[1]
b1 <- coef(fm1)[2]
p1 <- intervals(fm1)$corStruct[2]
y[i] = b0 + p1*y[i-1] + b1*x[i]
More information about the R-help
mailing list