I am trying to calculate quantiles for every "slice" of a dataset, in order to get some kind of "confidence intervals" at a 99% level. I manage this with base R, but it is excruciatingly slow. Any idea to speed up, or for a better approach, is welcome.
a <- (1:20000)/100
b <- 20001:40000
speedseq <- data.frame(a, b)
work_quantile <- rep(NA, nrow(speedseq))
myfunc <- function() {
for(i in 1: nrow(speedseq)) {
work_quantile[i] <- quantile(speedseq$b[speedseq$a>=(speedseq$a[i] - 1) &
speedseq$a<=speedseq$a[i]], na.rm = T, probs = 0.99)
if(i%%10000==0) print(round(i/nrow(speedseq),3))
}
mean(is.na(work_quantile))
}
microbenchmark::microbenchmark(myfunc(), times = 1)
Unit: seconds
expr min lq mean median uq max neval
myfunc() 5.185645 5.185645 5.185645 5.185645 5.185645 5.185645 1