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

How to count the number of negative values for each observation of a certain variable

I want to calculate total number of negative values for each observation, using previous 10 observations. I used the following code, but it does not work - funda_addit <- funda_addit %>% group_by(TICKER) %>% arrange(year) %>% mutate(NEG_EARN…
Sharif
  • 163
  • 1
  • 9
0
votes
2 answers

R - Execute Zoo rollapply/rollmean on multiple columns

I want to calculate a rolling mean (backward and forward) over 15 days each. Here is a testframe: date_list = seq(ymd('2000-01-15'),ymd('2010-09-18'),by='day') testframe = data.frame(Date = date_list) testframe$Day = substr(testframe$Date, start =…
Mr.Spock
  • 511
  • 2
  • 13
0
votes
1 answer

Rollapply by function,by group can't work

I want to calculate the roll correlation by group,get some problems in the group by with rollapply usmergetemp=structure(list(fundid = c(105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L,…
0
votes
2 answers

Use value from latest non missing row and apply rolling function

i have data like in DT <- data.frame(id=rep("A",times=10),B=1:10, C=c(NA,2:5,NA,NA,NA,NA,NA)) CT <- DT[,c(2,3)]*3 CT$id <- rep("B",times=10) DT <- rbind(DT,CT) I would like to fill in NAs in column C with its previous value plus function of value…
nov
  • 157
  • 1
  • 3
  • 15
0
votes
1 answer

Using rollapply width parameter within FUN with unexpected result

I was using rollapply for a calculation that takes the last value in the roll and divides it by the mean minus one, which works fine as you can try for yourself: set.seed(123) v <- xts(rnorm(5, 5, 1), Sys.Date()-5:1) rollapplyr(v, width = 3,…
sdittmar
  • 365
  • 2
  • 14
0
votes
1 answer

rolling logistic model function

I have some time series data called dat and what I am trying to do is to split it into training and test on a rolling basis. Say we have 100 days in total, I want to train the model on the first 20 days and test on the next 10 days (so using 30 days…
user8959427
  • 2,027
  • 9
  • 20
0
votes
1 answer

Rollapply in for loop

So, what I am doing is creating a heatmap for x and y coordinates. But I would like to do this for every 30 minute interval. For example the first heatmap will be created using the data from "00:00:00" to "00:30:00", then the next from "00:01:00" to…
0
votes
0 answers

Cumulative Sums of different Time Windows with several Conditions in R

I wish to compute cumulative sums of time windows of 3 and 7 days with conditions on a very large data frame. This is an example of my data frame: Date X 1.1.18 0 2.1.18 0 3.1.18 0 4.1.18 NA 5.1.18 0.3 6.1.18 NA…
user0405
  • 15
  • 6
0
votes
3 answers

How to sum every nth (200) observation in a data frame using R

I am new to R so any help is greatly appreciated! I have a data frame of 278800 observations for each of my 10 variables, I am trying to create an 11th variable that sums every 200 observations (or rows) of a specific variable/column (sum(1:200,…
esg16
  • 11
  • 1
  • 1
0
votes
1 answer

Create a function to normalize columns to earliest value using tidyverse?

The dataframes I work with have an arbitrary number of variables, and are of arbitrary length. I'd like to create a function to normalize each column by its earliest value. I'm new to R/tidyverse, and thought a for loop would work: Col.norm <-…
Tom.K
  • 1
  • 2
0
votes
2 answers

CAPM.beta rollapply

I have already successfully calculated my rolling correlations in my xts object with x <- cbind(market_return,stock_returns) rollcor_3year <- rollapplyr( x, width=width_cor,function(x) cor(x[,1],x[,-1], …
0
votes
1 answer

R make a more efficient rolling variance

I have a data table named features with the columns nightNo, HR, motion and angle. I'd like to get the rolling variance of the previous 600 points of the HR, motion and angle per nightNo. I've come up with the following function to do this: features…
Henk
  • 73
  • 8
0
votes
1 answer

R: Rollapply, preserve Date column

I have a dataset that includes a date column and the other columns are daily index returns. I would like to receive the rolling standard deviation for all indices but preserve the date in order to plot the results. Data looks as…
JJP
  • 3
  • 2
0
votes
2 answers

How to calculate rolling correlations between several columns and one column in rollapply?

Problem If I want to calculate the rolling correlation between each of my 39 stocks in their columns in stock_returns (xts object) and the market_return (separate xts object, only one column with market returns) with rollapply: rolling_3yearcor <-…
jackbauer
  • 155
  • 2
  • 14
0
votes
0 answers

Tidyquant rolling sharpe ratio using dynamic risk free rate

I'm looking to pass the risk free rate dynamically into the PortfolioAnalytics function SharpeRatio.annualized through the mutated PA function rollapply by using tq_mutate. Edit* At the moment I am unable to roll the time period window of risk free…