1 year ago

#329711

test-img

Mangoman

rollapply fails to cover/roll the whole data set

I'm trying to run a function I created using rollapply, on a dataset that has 5706 data points. The function (basically calculates Rescaled Range), uses all the observations in the window (in this case 500 observations) and calculates an output of one single value. So with width = 500 and by = 500, I expect to be left with 11 values (since 5706 is divisible by 500 11 times) after all the NA values were omitted.

But for some reason, rollapply stops calculating the function for the 10th window and only leaves me with only 10 values and ignores the last 706 observations, which doesn't make sense to me.

I tested it with a different function (just made a simple function that calculates the mean) and I still get this problem. I also changed the width and by sizes and whenever they are big they cause the same problem (e.g. if I set width=2853 and by=2853, I only get 1 calculated value and I expect 2.

My code:

test2 <- na.omit(rollapply(returns, 
                           width = 500, 
                           FUN = RS, 
                           fill = NA, 
                           partial = TRUE,
                           align = 'right', 
                           by = 500))

I know I could just create separate subsets, but I plan to run this later on in a for loop where it calculates this function with several different window sizes.

Does anyone have a solution? but I don't want it to ignore the last window or 500 observations in this example.

Thanks!

r

function

zoo

rolling-computation

rollapply

0 Answers

Your Answer

Accepted video resources