using tq_mutate to run a rolling window lm model with intraday data. Function works perfect when i want a rolling window of fixed size.
How do i run a window that stays fixed lets say at the first data point, 9:30 AM EST then begins to add to the window size. Example: Window WIDTH is 720 ticks wide. Each tick occurs at 5 seconds so 1 hour is the window. After tick 720 the window grows to 721, 722, etc to the close of the day. Its like there is a burn-in period, then grow the window to close.
Does tq_mutate support a window like this. Fixed on a start date time (opening), starts executing when the width is met and grows until end of data. Again for intraday data only.
This works below for fixed width.
pair.ratio <- as.tibble(runif(4000, min = 1, max = 5) / runif(4000,min=1,max=5))
tq_mutate(data = pair.ratio,mutate_fun = rollapply, width = 720, align='right', FUN = regr_fun,col_rename="coefficient")
regr_fun <- function(data) {
coef(lm(data[,1] ~ 1))
}
You can give width
a vector argument that has the window size for each window:
tq_mutate(data = pair.ratio,
mutate_fun = rollapply,
width = seq(nrow(pair.ratio)),
align='right',
FUN = regr_fun,
col_rename="coefficient")
I didn't make a working example, pair.ratio I believe needs a date index but you can make a function to generate the vector you request and then use the result.