0

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!

  • 1
    Please read the information on asking questions at the top of the [tag:r] tag home page. In particular a complete minimal reproducible example including code and inputs is needed when posting questions so that anyone else can copy and paste it into their session to reproduce the result. Note that the code shown uses `na.omit` on the result so you may simply be deleting points that you want because they are `NA` . – G. Grothendieck Mar 24 '22 at 11:55
  • Also maybe don't want `partial=TRUE`. – G. Grothendieck Mar 24 '22 at 12:24

0 Answers0