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

rollapply: fix the starting observation in each sliding window?

Simple zoo object is z <- zoo(11:15, as.Date(31:45)) as.data.frame(z) z 1970-02-01 11 1970-02-02 12 1970-02-03 13 1970-02-04 14 1970-02-05 15 1970-02-06 11 1970-02-07 12 1970-02-08 13 1970-02-09 14 1970-02-10 15 1970-02-11 11 1970-02-12…
Lodyk Vovchak
  • 133
  • 2
  • 12
2
votes
2 answers

rollapply : Is it possible to add end date for each sliding window?

A dummy zoo object is created as z <- zoo(11:15, as.Date(31:45)) as.data.frame(z) z 1970-02-01 11 1970-02-02 12 1970-02-03 13 1970-02-04 14 1970-02-05 15 1970-02-06 11 1970-02-07 12 1970-02-08 13 1970-02-09 14 1970-02-10 15 1970-02-11 11 1970-02-12…
bioinformatician
  • 364
  • 1
  • 12
  • 27
2
votes
2 answers

Unexpected output from rollapply due to zero length output from the function

I'm encountering a problem that I'm failling to understand. Here is the commented code : library(zoo) #Pattern p used as row feeding matrix to apply() for function f > p [,1] [,2] [,3] [1,] -1 1 1 [2,] 1 1 1 #Function f…
Wicelo
  • 2,358
  • 2
  • 28
  • 44
2
votes
2 answers

VaR calculation with complete missing column

I need to calculate rolling VaR of stock returns. From this post: Using rollapply function for VaR calculation using R , I understand that columns having complete missing cases will give error. But since the starting date and end date of stock…
Jairaj Gupta
  • 347
  • 4
  • 16
2
votes
1 answer

Using rollapply function for VaR calculation using R

I did the following for calculating Value at Risk (VaR) over 20 period rolling window: require(PerformanceAnalytics); require(zoo) data(edhec) class(edhec) # [1] "xts" "zoo" class(edhec$CTAGlobal) #…
Jairaj Gupta
  • 347
  • 4
  • 16
2
votes
1 answer

R rollapply bottom to top?

I'm trying to run rollapply from the bottom of my data.frame up to the top of my data.frame. Basically the last row in the data.frame (RBH) is the final measurement for a given subject in 2012. I then need to subtract each annual measurement in the…
KKL234
  • 367
  • 1
  • 5
  • 23
2
votes
3 answers

rollapply function on specific column of dataframes within list

I must admit to complete lunacy when trying to understand how functions within functions are defined and passed in R. The examples always presume you understand every nuance and don't provide descriptions of the process. I have yet to come across a…
Look Left
  • 1,305
  • 3
  • 15
  • 20
2
votes
2 answers

Defining functions (in rollapply) using lines of a dataframe

First of all, I have a dataframe (lets call it "years") with 5 rows and 10 columns. I need to build a new one doing (x1-x2)/x1, being x1 the first element and x2 the second element of a column in "years", then (x2-x3)/x2 and so forth. I thought…
user6894
  • 21
  • 1
2
votes
1 answer

Return Index value or Count with Rollapply Function

Thank you for all your help! I am working with time-series data and trying to identify the count at which an observation occurred, while working with the rollapply function in R. To clarify, here is some code: # Sample Data dates <-…
geoQuant
  • 95
  • 1
  • 1
  • 7
1
vote
1 answer

Error '! “x” : attempt to define invalid zoo object' using rollapply

I am trying to calculate rolling correlation for two variables in a data frame (the variables position in the data set are 29 and 35 respectively). I run these lines of code - df <- df %>% mutate (rrcorr_prccq = zoo::rollapply(width = 20, function…
Sharif
  • 163
  • 1
  • 9
1
vote
1 answer

Rollapply (zoo) gives multiple columns instead of a single column result

I am trying to apply a simple function in a rolling manner to a vector in R using the rollapply function from the zoo package. However, I am encountering an unexpected behavior where the result has multiple columns instead of the expected single…
Borexino
  • 802
  • 8
  • 26
1
vote
1 answer

Applying a function per individual behaviour event R

I have accelerometer data with each row labelled for the behaviour an animal is displaying per second, simple…
1
vote
2 answers

Calculate rolling correlation for all permutations of columns in a DF

I have a table that contains 4 numeric vectors, I would like to calculate and populate a new table that shows the rolling correlation of each possible column pair. I want to transform this simple table: Column A Column B Column c Column…
tpscp
  • 49
  • 6
1
vote
1 answer

Produce dataframe from rollapply that combines function names and those of original dataset

Is it possible to output a dataframe (or similar) following rollapply with merged column headers - that is combining my function object names with the original dataset (sensor_data)? The dataframe output I want would look like…
PharmR
  • 260
  • 1
  • 3
  • 12
1
vote
2 answers

Conduct rolling regression in windows of 5 years for each group in R

I have data with variables industry_sales, year, and industry. I want to conduct time series regressions for each industry in rolling windows of 5 years, with the dependent variable being industry_sales and the independent variable being year. For…
fsure
  • 127
  • 6