From ggrothendieck at gmail.com  Thu Jan 31 22:12:59 2008
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Thu, 31 Jan 2008 16:12:59 -0500
Subject: [Rd] Feature request: about lag(), which.min() and cat().
In-Reply-To: <47A1CBF7.4090803@etu.unige.ch>
References: <47A1CBF7.4090803@etu.unige.ch>
Message-ID: <971536df0801311312w6c00cca2i4d7849b406abaf6d@mail.gmail.com>

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
>



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