[Rd] Feature request: about lag(), which.min() and cat().

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 31 22:12:59 CET 2008


On Jan 31, 2008 8:24 AM, Matthieu Stigler <stigler3 at etu.unige.ch> wrote:
> lag()
> If one wants to construct a time series model, it is often useful to
> have the lags matrix. This is available with embed(ts,k) but the time
> series class disappears. So it would be nice that the argument k of
> lag() also admit values of length >1, which would give the same result
> as embed() but keep the class. In his wish list of 1 January 2008 (point
> 8), Gabor Grothendieck spoke about a function Lag. Maybe also a function
> lags otherwise if the idea of length(k)>1 is not good?

It would be nice.  Note that lag.zoo in the zoo package does supports this
for zoo objects.

> library(zoo)
> z <- zoo((11:20)^2)
> lag(z, 1:2)
  lag1 lag2
1  144  169
2  169  196
3  196  225
4  225  256
5  256  289
6  289  324
7  324  361
8  361  400
9  400   NA

> # regress z on lag(z, 1:2) using dyn package
> library(dyn)
> dyn$lm(z ~ lag(z, 1:2))

Call:
lm(formula = dyn(z ~ lag(z, 1:2)))

Coefficients:
 (Intercept)  lag(z, 1:2)1  lag(z, 1:2)2
           2             2            -1

# as a workaround one could convert to zoo and back

> x <- ts((11:20)^2)
> as.ts(lag(as.zoo(x), 1:2))
Time Series:
Start = 1
End = 9
Frequency = 1
  lag1 lag2
1  144  169
2  169  196
3  196  225
4  225  256
5  256  289
6  289  324
7  324  361
8  361  400
9  400   NA
>



More information about the R-devel mailing list