Skip to content

Creating variable based on lags

2 messages · Am Gut, Joshua Ulrich

#
Good Afternoon Everyone,

I have a series of returns are was hoping that someone could help me create
a variable based on a pre-specified set of lagged observations. For
example, I would want to sum up the last 25 returns to make a new return
column. I would also like to weight the returns by a factor of 0.25^i with
i being the lag number. I am intuitively thinking a for loop but can not
rationalize internally how to write this. I am trying to make a variable
like below:

newreturn = return(-1)*0.25^1 + return(-2)*0.25^2 + .... return(-25)*0.25^25

And obviously I would like to apply this to all new observations past the
25th observatios to the end of my dataset. I hope this is clear as I know
the solution must be relatively simple. I hope to hear from you guys.

Thanks,

Am Gut
6 days later
#
On Wed, Feb 15, 2017 at 2:11 PM, Am Gut <agquantr at gmail.com> wrote:
If you have a set of weights you would like to apply, you can use
TTR::WMA.  It calculates a weighted moving average, and the
denominator in the average is the sum of the weights.  So if you just
want the sum, you can multiply the result by the sum of the weights.
Or you can use a for loop, like you thought.

# random data
set.seed(21)
x <- rnorm(10, 0, 0.01)
n <- 5
# pre-allocate result
y <- x * NA
# weight vector
w <- 0.25^(n:1)

# loop over input
for(i in n:length(x)) {
  y[i] <- sum(x[(i-n+1):i] * w)
}

# Use TTR's WMA function
z <- TTR::WMA(x, n, w) * sum(w)

# verify the results are the same
all.equal(y, z)