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
2
votes
2 answers

Rolling sum from a certain position in a data frame in R

Say I have the following data, dat1; width from by 2 1 A 3 1 A 2 2 A 3 2 A 2 1 B 3 1 B 2 2 B 3 2 B And additionally have that, dat2; x pos by 4 1 A 5 2 …
user7715029
  • 83
  • 1
  • 7
2
votes
1 answer

For each column, sum scores by group over prior window of time

I have a large panel dataset (10,000,000 x 53) with about 50 columns of scores. I have aggregated each score by group (there are about 15,000) and date. Now I want to calculate a rolling sum of three values including the prior two dates' and the…
em1031
  • 35
  • 5
2
votes
2 answers

Rolling Regression Data Frame

Appreciate this may have been asked before but I have not found a clear solution to work over a data frame. I want to run a rolling linear regression over a look back of 5 days. (small so can illustrate here) So far I am trying: rollingbeta <-…
Andrew Bannerman
  • 1,235
  • 2
  • 16
  • 36
2
votes
0 answers

Calculate running sum/decay value in time series

I'm looking to calculate a specific type of 'diminishing' sum or decay value within a time series. For instance, say I have some simple data: thedata <- data.frame(magicseeds = c(30,20,10,40,20) ,week = seq(from = 1, to = 5,…
SlyGrogger
  • 317
  • 5
  • 16
2
votes
1 answer

sum over past window-size dates per group

The problem is similar to How do I do a conditional sum which only looks between certain date criteria but slightly different and the answer from that does not fit into current problem. The main difference is that the date column based on each group…
HappyCoding
  • 5,029
  • 7
  • 31
  • 51
2
votes
1 answer

Calculating firm specific Fama-French beta coefficients from a rolling 1 year regression

I was wondering if someone could guide me through the rollapply function in R. I want to conduct a 1 year rolling regression with fama-french factors. The dataset is prepared in Excel and contains weekly data from 2011-2017. Therefore the time…
Neri Kim
  • 135
  • 1
  • 2
  • 9
2
votes
1 answer

Rolling window regressions within multiple groups

I am trying to apply a rolling window regression model to multiple groups in my data. Part of my data is as below: gvkey year LC YTO 1 001004 1972 0.1919713 2.021182 2 001004 1973 0.2275895 2.029056 3 001004 1974 …
Tuan Le
  • 97
  • 1
  • 7
2
votes
2 answers

rollapply na.rm = TRUE giving 0 values instead of NA's

I have a simple question, that I can't seem to find the answer to on google, stackoverflow or stackexchange. I am currently working with the example of rollapply to find the sum of some values that contain NA's. For example: z <- zoo(c(NA, NA,…
lurodrig
  • 99
  • 3
  • 8
2
votes
1 answer

Alternative to For-Loop: How to use rolling window for boosting in R?

I'm looking for help optimizing my code to get rid of loops and increase computational speed. I am pretty new to the field and to R. I run component wise gradient boosting regressions on a linear time series model with a rolling window. I use the…
SimonCW
  • 356
  • 3
  • 11
2
votes
2 answers

Use rollapply() to get mean of specific/custom rows

I am using rollapply (from the zoo package) in R to get rolling mean values for a series of rows in a data frame. For each row, where the focal row is x I am trying to get a number of means. I can do this with a loop but it's slow, and I try to…
rg255
  • 4,119
  • 3
  • 22
  • 40
2
votes
0 answers

t.test for two variables using rollapply in a data.table

I'm trying to run a two sample t-test t.test with a 7 days rollying window with samples coming from two seperate columns (sample1 and sample2). I would like to obtain p-values for each test and add them to my current data, starting from day 1. I…
user3498523
  • 370
  • 4
  • 10
2
votes
1 answer

applying rollmean to multiple time series, with multiple windows

I need to produce rolling averages of different lengths for several time series objects. Ideally, I would be left with a dataframe for each time series object with the rolling averages stored in adjacent columns. I was able to produce the output I…
Fanderson
  • 90
  • 9
2
votes
3 answers

Using rollapply and lm over multiple columns of data

I have a data frame similar to the following with a total of 500 columns: Probes <- data.frame(Days=seq(0.01, 4.91, 0.01), B1=5:495,B2=-100:390, B3=10:500,B4=-200:290) I would like to calculate a rolling window linear regression where my window…
user507
  • 223
  • 1
  • 6
  • 14
2
votes
2 answers

How to rollapply over a multi column data table

I would like to use the rollapply function over a multi column datatable, namely I would like to be able to use each column independantly for instance let's consider the following datable : > DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6),…
Wicelo
  • 2,358
  • 2
  • 28
  • 44
2
votes
1 answer

rollapply with function that returns more than one value

Say I want to use rollapply with a function that returns more than on value. Like this: library(quantmod) getSymbols("YHOO") openYHOO <- YHOO[1:10,1] rollapply(openYHOO, width = 2, range) I get an error. I also tried merging the results inside…
brandco
  • 310
  • 2
  • 6