Antti
Antti

Reputation: 1293

How to calculate product of lags for an arbitrary number of lags in R (dplyr)

This is probably a very elementary problem, but I don't seem to get it working right. I need to calculate simple product of an element and certain number of its lags in R data.frame of time series data. I am trying to achieve this in dplyr pipe. E.g.:

require(dplyr)

df <- data.frame(YEAR = c(2010, 2011, 2012, 2013, 2014),
                 x = c(1, 2, 3, 4, 5))

dfFinal <- df %>% mutate(prodLag1 = prod(x, lag(x, 1), na.rm = T),
                         prodLag2 = prod(x, lag(x, 1), lag(x, 2), na.rm = T),
                         prodLag3 = prod(x, lag(x, 1), lag(x, 2), lag(x, 3), na.rm = T))

The result is not what I thought. E.g. with prodLag1 the resulting dataframe should look like this:

dfFinal <- data.frame(YEAR = c(2010, 2011, 2012, 2013, 2014),
                      x = c(1, 2, 3, 4, 5),
                      prodLag1 = c(NA, 2, 6, 12, 20))

Additionally, I am aiming at lag = 10 and I would like to find more feasible way than just typing each individual lag in. Would Reduce work for this?

Upvotes: 0

Views: 455

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

One way of doing prodLag10...

dfFinal <- df %>% mutate(cumlog = cumsum(log(x)),
                         prodLag10 = exp(cumlog-lag(cumlog,11)))

Upvotes: 2

Related Questions