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
11
votes
4 answers

How to compute rolling covariance more efficiently

I am trying to compute a rolling covariance between a set of data (each column of my x variable) and one other (y variable) in R. I thought I could use one of the apply functions, but can not find how to roll two set of inputs at the same time. Here…
Djiggy
  • 235
  • 2
  • 13
9
votes
2 answers

rollapply for large data using sparklyr

I want to estimate rolling value-at-risk for a dataset of about 22.5 million observations, thus I want to use sparklyr for fast computation. Here is what I did (using a sample…
Jairaj Gupta
  • 347
  • 4
  • 16
8
votes
4 answers

Efficiently perform row-wise distribution test

I have a matrix in which each row is a sample from a distribution. I want to do a rolling comparison of the distributions using ks.test and save the test statistic in each case. The simplest way to implement this conceptually is with a…
Ajar
  • 1,786
  • 2
  • 15
  • 23
8
votes
2 answers

the rolling regression in R using roll apply

My imported data contains 7 variables: Y and X1, X2, X3, X4, X5, X6. I tried applying the rollapply function in zoo in order to run a rolling regression within an in-sample with a window of 262 obs. (work days in a year). date …
user3546838
  • 89
  • 1
  • 1
  • 3
6
votes
4 answers

Count numbers that are lower by constant from current number

Imagine that I have a list of numbers (i.e. numbers column in data.table/data.frame). 1 5 5 10 11 12 for each number in a list a want to count how many unique numbers are there which are lower than that particular number + 5. The explanation for…
Makaroni
  • 880
  • 3
  • 15
  • 34
6
votes
1 answer

use rollapply and zoo to calculate rolling average of a column of variables

I want to calculate the rolling mean for all variables in column "sp". This is a sample of my data: the_date sp wins 01-06--2012 1 305 02-06--2012 1 276 03-06--2012 1 184 04-06--2012 1 248 05-06--2012 1 243 06-06--2012 1 …
asiehh
  • 553
  • 12
  • 22
6
votes
2 answers

Estimation of rolling Value at Risk (VaR) using R

I need to perform rolling VaR estimation of daily stock returns. At first I did the following: library(PerformanceAnalytics) data(edhec) sample<-edhec[,1:5] var605<-rollapply(as.zoo(sample),width=60,FUN=function(x)…
6
votes
1 answer

Faster alternative to function 'rollapply'

I need to run rolling window function on a xts data which contains about 7,000 rows and 11,000 columns. I did the following: require(PerformanceAnalytics) ssd60<-rollapply(wddxts,width=60,FUN=function(x) SemiDeviation(x),by.column=TRUE) I waited…
Jairaj Gupta
  • 347
  • 4
  • 16
6
votes
2 answers

Rolling Standard Deviation in a Matrix in R

Bellow is a stock daily returns matrix example (ret_matriz) IBOV PETR4 VALE5 ITUB4 BBDC4 PETR3 [1,] -0.040630825 -0.027795652 -0.052643733 -0.053488685 -0.048455772 -0.061668282 [2,] -0.030463489…
RiskTech
  • 1,135
  • 3
  • 13
  • 19
6
votes
2 answers

R - ave rollapply error: k <= n is not TRUE

I am trying to calculate a rolling mean of value grouped by multiple dimensions in R. Something I would do in SQL in the following way: AVG(value) OVER (PARTITION BY dim1, dim2 ORDER BY date RANGE BETWEEN 5 PRECEDING AND CURRENT…
Adam
  • 756
  • 4
  • 10
  • 23
5
votes
2 answers

diff on data.table column

Really frustrated by this. I just want to take difference between rows in a data.table. where dif(n) = value(n) - value(n-1). so, compared to what I have, the results should be shifted by 1 position, and the first position for each "variable"…
user3385769
  • 161
  • 6
  • 16
5
votes
2 answers

alignment and offsets in rollapply

I am trying to calculate some statistics for a moving window and am using rollapply in the zoo package. My question is how do I get rollapply to apply that function to the previous n observations instead of the current observation and the previous…
TBP
  • 697
  • 6
  • 16
4
votes
2 answers

use rollaply without current row

I have a simple data.frame where I want to compute some summary statistics on a rolling basis. For example, a rolling median over a window of five observations (2 lags, current one and 2 ahead) is achieved by library(dplyr) x <- data.frame("vals" =…
Akkariz
  • 139
  • 8
4
votes
2 answers

Rolling Count of Events Over Time Series

I'm trying to calculate a rolling count/sum of occurrences by group over the series of a time frame. I have a data frame with some sample data like this: dates = as.Date(c("2011-10-09", "2011-10-15", "2011-10-16", …
wetcoaster
  • 367
  • 3
  • 15
4
votes
3 answers

Find similar elements of a vector and modify everything inbetween

I have a large data frame (df) with binomial values ranging from 1 to 2. NAs are also included in the data. As a practical example, I will create a short vector containing a subset of a user's data: df <-…
IlBardo
  • 43
  • 5
1
2 3
17 18