Questions tagged [rollapply]

rollapply is a function in the zoo package used to perform rolling operations on an object.

rollapply is a function in the zoo package used to apply operations sequentially over elements of an object. Examples are returning a rolling mean for a vector or performing a regression on each of several partially-overlapping subsets, or rolling subsets, of a data set.

256 questions
0
votes
2 answers

applying moving averages/ rollapply over multiple variables

I'm working with panel data. I want to add 3 and 5-year moving averages of some of my variables into my dataset. I have the following code which works fine but is very long. Is there a neater and more efficient way of creating moving averaged…
user3227641
  • 157
  • 7
0
votes
0 answers

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…
0
votes
1 answer

How to create in R new column calculating mean (3 previous grouped rows) of numerical variable grouping by variables (factors)?

I have tried using rollapply but I can't get the desired result. These are the columns(sample) of the dataset on which I want to do the calculations. structure(list(LeagueROUND = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L,…
William88
  • 49
  • 6
0
votes
2 answers

Rollapply with expanding window in R

Let's say I have a simple toy vector in R like: x = seq(1:10);x [1] 1 2 3 4 5 6 7 8 9 10 I want to use the rollapply function from zoo package but in a different way.Rollapply calculates a function from a vector x with width argument to…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
0
votes
1 answer

Rollapply with different rolling window on each vector of time series

Let's say that you have 3 time-series vectors with different sizes: xdate = seq(as.Date("2010/1/1"), by = "day", length.out = 2789) ydate = seq(as.Date("2010/1/1"), by = "day", length.out = 1289) zdate = seq(as.Date("2010/1/1"), by = "day",…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
0
votes
1 answer

Roll condition ifelse in R data frame

I have a data frame with two columns in R and I want to create a third column that will roll by 2 in both columns and check if a condition is satisfied or not as described in the table below. The condition is a rolling ifelse and goes like this : IF…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
0
votes
1 answer

Rollapply for backtesting the value at risk

I want to backtest the empirical (unconditional) value at risk of a time series vector: x = rnorm(1000) xt = diff(x) quantile(xt,0.01) So I am thinking that rollapply function in R might be helpful, since I want to roll estimate the quantile a…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
0
votes
2 answers

How can I correspond the values of characters from a dataframe to characters of a list

Since I am really new in R, I am not sure if I will be able to express my problem correctly so sorry in advance. I have some letters that have a given value. I created a dataframe for those and I also have a string with the same set of letters. I…
Greggs
  • 1
  • 4
0
votes
2 answers

adjust "width" argument in rollapply() function in r for discontinuous dates

I have a dataset of daily remotely sensed data. In short, it's reflectance (values between 0 and 1) for the last 20 years. Because it's remotely sensed data, some dates do not have a value because of clouds or some other obstruction. I want to use…
0
votes
2 answers

Making rollApply() skip n steps - R

Below is my attempt at a minimal reproducible example. Briefly explained, I am using rollApply from the rowr package to calculate a function over a rolling window, and using data from two columns simultaneously. If possible, I would like to skip n…
0
votes
3 answers

find the sum after every seventh day but with missing days in R

I have an R df as below: | date_entered | returning | new | | ------------ | --------- | --- | | 2021-06-02 | 0 | 14 | | 2021-06-03 | 12 | 8 | | 2021-06-04 | 8 | 0 | | 2021-06-05 | 7 | 15 | | 2021-06-07 …
doubleD
  • 269
  • 1
  • 12
0
votes
1 answer

correlation coefficients between two time series calculated over windows moved forward in time by n time unit

Is there a package or a simple code to produce plots of (1) correlation coefficients between two time series calculated over windows moved forward in time by n time unit (2) and their respective p-values calculated for each move ? library(zoo) x =…
Alex
  • 158
  • 5
0
votes
1 answer

Roll regression for 4 years of daily data which moves one month ahead for each new regression and for different dependent variables

I have 5 independent variables (Columns B-F in attached data) and some dependent variables (columns G-M in the attached data) and I need to do multiple regressions for each of the dependent variable against all independent ones. The regressions have…
LMLP
  • 11
  • 3
0
votes
2 answers

rollapply for multidimensional arrays (R)

Not a duplicate of rollapply for two-dimensional arrays (R), but rather an elaboration upon it - what I actually want to do is slightly more complicated than described in that question. I hoped that any answer to that question would easily extend to…
Drubbels
  • 327
  • 2
  • 4
  • 11
0
votes
1 answer

How to match third (or whatever) from the bottom in a rolling fashion in R?

Here is my example data frame with the expected…
Eric
  • 101
  • 7