[datatable-help] speeding up a for loop on a rolling window

cosimo sme.costanzo at gmail.com
Sat Feb 10 13:51:18 CET 2018


I have the following loop that I would like to speed up. 

I have looked at RcppRoll, which seems the best option in my case. However I
am unfamiliar with c++ and unable to write a function in c++ that replaces
QCalc.

Thank you so much in advance for any help on this!!!

minQCalc <- function(x, length.window = 250, ...){
s.index <- length.window:dim(x)[1]
out.vec <- rep(NA, length(s.index))

for(s in s.index){
    x.sub <- x[(s - length.window + 1):s, ]
    out.vec[match(s, s.index)] <- QCalc(x.sub, ...) 
}

out.min <- min(out.vec)
return(out.min)}
where:

QCalc <- function(x, method = "quantile"){

alpha <- 0.99
alpha.tilde <- 0.974234518211730

if(method == "quantile"){

    out <- quantile(x,  alpha)

}else if(method == "ETL"){

  qq.alpha.tilde <- sort(x)[ceiling((1 - alpha.tilde)*length(x))]   
  out <- mean(x[x <= qq.alpha.tilde])
}else{stop("quantile unknown")} return(out)}



--
Sent from: http://r.789695.n4.nabble.com/datatable-help-f2315188.html


More information about the datatable-help mailing list