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
1 answer

How to estimate rolling correlations and then take the mean of it?

Based on the following data-frame, I would like to compute the rolling correlations (with a window size of 12): library(rugarch) library(rmgarch) data(dji30retw) Dat = dji30retw[, 1:8, drop = FALSE] > dput(head(Dat)) structure(list(AA =…
0
votes
1 answer

Stepwise regression: lagged date range generated for a zoo series

I'm trying to generate a rolling stepwise regression using rollapply() and step(), but noticed that the date range of the resulting zoo series is lagged. Below you'll find an illustration using two years of "data" (2016-01-01:2017-12-31) and a…
Lauri
  • 1
  • 1
0
votes
1 answer

Evaluate rolling mean by group when groups have insufficient observations

I am trying to calculate a bunch of rolling means for a dataset - date name px_last 2012-12-04 A 6.81 2012-12-05 A 4.28 2012-12-06 A 4.32 2012-12-04 A 6.89 2012-12-05 A 7.24 2012-12-04 B 6.81 2012-12-05 B …
phil_t
  • 851
  • 2
  • 7
  • 17
0
votes
1 answer

rollapply for select number of rows excluding top 6 rows

I have been trying to implement rollapply and calculate mean of window length 2 excluding the top 6 rows. My data is as follows Volume <- c(10406513,14252364,7235783,7235783,5593794,5825159,3887574,2959390, …
0
votes
1 answer

Calculate maximum of last year observations

I am struggeling to get maximum value of variable from last year of observations (Not each year!) and implement it to each row (observation). I think the best way to do so is using the rollapply function but I cannot figure out how the width should…
Yoni W
  • 37
  • 5
0
votes
1 answer

Rollapply for rolling regression with different window sizes, save coefficients in matrixes

I'm trying to perform a rolling linear regression, whit overlapping windows and for different window sizes. I want to save the result in two tables, one with window size and slopes and one wiht window size a and intercepts. My data set is now saved…
Nicolle
  • 61
  • 7
0
votes
1 answer

Count observations using rollapply from zoo package with dynamic criteria

I am looking for a way to count observations like here but with the ability to change criteria according to the specific observation (moving count). For example - count the number of observations of mag (from the last 50) which are greater than the…
Yoni W
  • 37
  • 5
0
votes
1 answer

Calculate coefficients of rolling regressions with dependent variables in the columns

I'm hoping to calculate the coefficients (the intercepts in particular) of rolling regressions. There are many dependent variables. Some of them (Y1 and Y2) are shown below. Each of them is regressed with independent variables X1 and X2.…
Warrior
  • 223
  • 1
  • 10
0
votes
0 answers

R rollapplyr to calculate 10-day sum in xts zoo class

I am new to R, I would like to calculate the below formula in the zoo xts class object. formula: 10-day sum of [(close- open)/(high-low) *volume] / 10-day sum of volume Input df: df <- getSymbols("0005.HK", auto.assign = FALSE, src = "yahoo")…
Peter Chung
  • 1,010
  • 1
  • 13
  • 31
0
votes
3 answers

Using ccf() with rollapply across variables in R

I have 3 columns of data with 10 rows in each column as below set.seed(101) inputx <- rnorm(1000,mean = 3,sd=2) inputy <- rnorm(1000,mean = 2,sd=1) inputz <- rnorm(1000,mean = 1,sd=3) example <- cbind(inputx,inputy,inputz) > head(example,10) …
TheGoat
  • 2,587
  • 3
  • 25
  • 58
0
votes
3 answers

How can I roll up lagged time data given conditions in a data.table in R?

I'm fairly new to R and have gone through some tutorials. What I'd like to do is find a good method of joining data onto itself based on some conditions. In this case what I want to do is pick an arbitrary length of lag and create a rolling window.…
shiggity
  • 531
  • 4
  • 12
0
votes
1 answer

How can I find Covariance for every n row in R

I have 2 large set of data, each have 2000+ data and trying to find the covariance for every 5 row. x=c(1,2,3,4,5) y=c(6,7,8,9,10) df=data.frame(x,y) group=rep(1:length(df),each=2,length=length(df)) What is my next step so I can find the…
Ian
  • 3
  • 2
0
votes
1 answer

Standard deviation is zero warning message when using rollapply on a time series object

I have an xts object with 12 variables over time in 15 minute intervals. > summary(wideRawXTSscaled) Index DO0182U09A3 DO0182U09B3 DO0182U09C3 DO0182U21A1 DO0182U21A2 Min. :2017-01-20…
TheGoat
  • 2,587
  • 3
  • 25
  • 58
0
votes
1 answer

Nested function, matrix R

I am currently trying to develop a new function that calculates rolling statistics by groups within a matrix. My dataset looks as follows: ID year ROA CAR [1,] 1 2009 0.006954926 0.3933436 [2,] 1 2010 0.013286958 0.2892719 [3,] 1…
user7375046
0
votes
2 answers

How to use a custom function within the rollapply

I have a custom function which runs a regression and returns the result in a desired way. the function is named "reg.fun". For example, I can run such a code: results <- dt[ ,reg.fun(.SD, depvar="Y", indepvars=c("X1", "X2")), …