Lagging Variables in R?

You can achieve this using the built-in embed() function, where its second 'dimension' argument is equivalent to what you've called 'lag': x.

More generally: lagmatrix Lag+1)} Then use lagmatrix(1:4,2) – Rob Hyndman Aug 23 '09 at 5:58 Thanks for the pointer to the embed function. This saved a massive amount of computing time for me. – James Aug 27 '09 at 15:17.

Use a proper class for your objects; base R has ts which has a lag() function to operate on. Note that these ts objects came from a time when 'delta' or 'frequency' where constant: monthly or quarterly data as in macroeconomic series. For irregular data such as (business-)daily, use the zoo or xts packages which can also deal (very well!) with lags.

To go further from there, you can use packages like dynlm or dlm allow for dynamic regression models with lags. The Task Views on Time Series, Econometrics, Finance all have further pointers.

The running function in the gtools package does more or less what you want: > require("gtools") > running(1:4, fun=I, width=3, allow. Fewer=TRUE) $`1:1` 1 1 $`1:2` 1 1 2 $`1:3` 1 1 2 3 $`2:4` 1 2 3 4.

Glad to see you're finally on here! – Christopher DuBois Aug 21 '09 at 16:55 But James wanted a matrix not a list. You could package the result using matrix(unlist(...)) but the embed() function does it in one step.

– Rob Hyndman Aug 23 '09 at 5:56 Totally right, which is why I upvoted the embed() solution when it came out =). But 'running' is still a useful function I think --- most of the time when I wanted to create the matrix James asked for, what I really wanted to do was run apply on it. – Jonathan Chang Aug 23 '09 at 16:46.

Embed() was discussed in a previous answer by Joshua Reich. (Note that I prepended x with NAs to replicate your desired output). It's not particularly well-named but it is quite useful and powerful for operations involving sliding windows, such as rolling sums and moving averages.

> running(1:4, fun=I, width=3, allow. Use a proper class for your objects; base R has ts which has a lag() function to operate on. Note that these ts objects came from a time when 'delta' or 'frequency' where constant: monthly or quarterly data as in macroeconomic series.

For irregular data such as (business-)daily, use the zoo or xts packages which can also deal (very well!) with lags. To go further from there, you can use packages like dynlm or dlm allow for dynamic regression models with lags. The Task Views on Time Series, Econometrics, Finance all have further pointers.

Terms of service. Not the answer you're looking for? Or ask your own question.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions