[R] Introducing lags in my GAM

Simon Wood s.wood at bath.ac.uk
Tue Oct 5 16:16:16 CEST 2010


I think that the problem is probably that y and lag(y,k=1) just get coerced 
back to numeric (i.e. their time-series attributes ar lost). So your model 
formula is equivalent to y~.... + y, which is probably not what you wanted. 

If y is in time order, I would do something like

  bellver$y.1 <- c(NA,bellver$y[1:(nrow(bellver)-1)])

and then use y.1 as the covariate in place of lag(y,k=1). (do check this is 
lagging in the right direction for you)


On Tuesday 05 October 2010 14:09, Oscar Saenz de Miera wrote:
> To whom it may concern,
>
> This is Oscar Saenz. I am working on my thesis and wanted to ask for your
> help.
>
> I am using "R" to estimate a Generalized Additive Model where the
> concentration of a specific pollutant is explained by a series of
> variables. I need to introduce a lag of the regressand in my equation.
>
> I tried to do so by estimating:
>
> eq1 <- gam(y~s(time,bs="cr")+s(x1,bs="cr")+s(x2,bs="cr")+s(x3,bs="cr")+
>     D1+D2+D3+D4+D5+D6+lag(y, k = 1),data=bellver,family=gaussian)
>
> where (D1,...,D7 are day-of-the-week dummies; x1, x2 and x3 are
> weather related variables; time is a trend; and lag(y, k = 1) was supposed
> to be the lag of the dependent variable)
>
> but this did not work.
>
> So, HOW CAN I INTRODUCE THE LAG IN MY GAM ESTIMATIONS?
>
> Thank you very much. Your help will be very valuable.
>
> Receive my best regards,
>
> Oscar
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented, minimal,
> self-contained, reproducible code.

-- 
> Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK
> +44 1225 386603  www.maths.bath.ac.uk/~sw283 



More information about the R-help mailing list