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
1
vote
2 answers

several questions regarding zoo::rollapply

My understanding of rollapply's width option is that it specifies the window size on which the function will operate and by options specifies the shift size for this window. Here is my dataset: > dataset <- as.vector(t(cbind(5:1, 1:5))) > dataset …
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
1
vote
2 answers

How to speed up/ improve rolling average function?

My data is 988, 785 obs. of 3 variables. A smaller example of my data is below: Names <- c("Jack", "Jill", "John") RawAccelData <- data.frame( Sample = as.numeric(rep(1:60000, each = 3)), Acceleration = rnorm(6000), ID = rep((Names), each =…
user2716568
  • 1,866
  • 3
  • 23
  • 38
1
vote
0 answers

na.rm behavior of roll_median?

Quick question: Would anyone help explain why I got all NA's for the rolling median output. I set na.rm to TRUE but that doesn't seem to help. Thanks! library(RcppRoll) input <- c(1:10, rep(NA, 190)) output <- roll_median(input, n = 120, fill = NA,…
Yuanchu Dang
  • 307
  • 4
  • 14
1
vote
1 answer

Rollapply backwards time series in R

I need to fill backwards the historical prices knowing the returns (in real situation they are simulated). So far I have this code: library(quantmod) getSymbols("AAPL") df = AAPL["2014-01-01/2015-01-01", "AAPL.Close"] df_ret = diff(log(df),1) #…
stkubr
  • 371
  • 1
  • 5
  • 15
1
vote
1 answer

Clueless about this error: wrong sign in 'by' argument

I have the following dataset: >k1[1:10,] id web_name first_name second_name position date team1 team2 game_week points home_away team_scored team_conceded minutes goals assists 1 1 Fabianski Lukasz Fabianski Goalkeeper…
Manasvi Bali
  • 25
  • 1
  • 3
1
vote
2 answers

looping for a time series object

I know this may look like a long post, but it is a simple issue related to looping in zoo. Please read on: # required libraries: library(vars) library(zoo) # create time series model <- zoo(x = cbind(rnorm(10),rnorm(10)*2, rnorm(10)*0.2), order.by…
cune
  • 79
  • 1
  • 5
1
vote
1 answer

R: rollapplyr and lm factor error: Does rollapplyr change variable class?

This question builds upon a previous one which was nicely answered for me here. R: Grouped rolling window linear regression with rollapply and ddply Wouldn't you know that the code doesn't quite work when extended to the real data rather than the…
Nate Miller
  • 386
  • 5
  • 19
1
vote
1 answer

R: Grouped rolling window linear regression with rollapply and ddply

I have a data set with several grouping variables on which I want to run a rolling window linear regression. The ultimate goals is to extract the 10 linear regressions with the lowest slopes and average them together to provide a mean minimum rate…
Nate Miller
  • 386
  • 5
  • 19
1
vote
1 answer

Using rollapply to process time window -- not fixed sample window for irregular time series

I am trying to compute various stats for a time window (think 20 seconds) for various signals which may or may not be recorded at every sample window. Additionally, the sampling interval is not regular -- it may be 2 or 3 or 4 seconds. Consider…
Scott
  • 13
  • 3
1
vote
2 answers

R xts: apply over a rolling window

I wish to execute a function FUN over a rolling window of 1 year. My xts has not the same number of points per year. How can I do that in an efficient way? P.S. usually to execute a FUN over a fixed number of datapoints (for instance 100) I…
RockScience
  • 17,932
  • 26
  • 89
  • 125
1
vote
0 answers

GARCH forecast expanding window: rollapplyr() and apply.fromstart()

My intention is to generate a forecast using a GARCH(1,1) using data from an expanding window. Everyday a new return enters the dataset and I will redo the GARCH fit and forecast. The function myFit does this but now I need to pass as argument a…
opt
  • 477
  • 1
  • 10
  • 25
1
vote
0 answers

Garch prediction on expanding window in R with ugarchfit

I have daily data for S&P500 and store the close-to-close return in my_data$Return. My goal is to refit a GARCH(1,1) each day in the period (so starting from the startDate which is 01/01/2004) and then calculate a 30 days forecast. In other words,…
opt
  • 477
  • 1
  • 10
  • 25
1
vote
1 answer

Apply additional parameters syntax

Let's say I have a data frame with columns of data, and I want to calculate the one-sided moving average of each column. Why does this work my.rollapply <- function(x){ return(rollapply(x,moving.avg,FUN= mean, fill = NA,align = 'right', na.rm =…
jtanman
  • 654
  • 1
  • 4
  • 18
1
vote
1 answer

R - Sliding Door Analysis # of events in time period

I am reposting this question as I thought I needed a cluster type analysis but what is required is more of a 'sliding window' analysis. I have a dataset that has 59k entries recorded over 63 years, I need to identify 'clusters' of events with the…
Methexis
  • 2,739
  • 5
  • 24
  • 34
1
vote
1 answer

Moving average with changing period in R

I have a data frame named abc on which I'm doing moving average using rollapply. The following code works: forecast <- rollapply(abc, width=12, FUN=mean, align = "right", fill=NA) Now, I want to do the same thing with the width being variable, i.e.…