loop ↔ vectorized ↔ direct [Two-Stage / GS Designs]

posted by Helmut Homepage – Vienna, Austria, 2017-08-20 12:40  – Posting: # 17719
Views: 24,426

Hi ElMaestro,

» vecQT1=c(1:5000)
» vecQT2=c(1:5000)
» for (i in 1:5000)
»    {
»     vecQT1[i]=qt(p=1-alpha1, df=i)
»     vecQT2[i]=qt(p=1-alpha2, df=i)
»    }

Later down you makes calls to vecQT1[df] and vecQT2[df]. That’s awfully slooow. Why not simply call qt(p=foo, df=bar) directly?

library(microbenchmark)
loop <- function(alpha1, alpha2, df) {
  vecQT2 <- vecQT1 <- numeric()
  for (j in 1:5000) {
    vecQT1[j]=qt(p=1-alpha1, df=j)
    vecQT2[j]=qt(p=1-alpha2, df=j)
  }
  return(QT=c(vecQT1[df], vecQT2[df]))
}
vectorized <- function(alpha1, alpha2, df) {
  vecQT1 <- qt(p=1-alpha1, df=1:5000)
  vecQT2 <- qt(p=1-alpha2, df=1:5000)
  return(QT=c(vecQT1[df], vecQT2[df]))
}
direct <- function(alpha1, alpha2, df) {
  return(QT=c(qt(p=1-alpha1, df=df,
              qt(p=1-alpha2, df=df))))
}
res <- microbenchmark(loop(0.0294, 0.05, 10),
                      vectorized(0.0294, 0.05, 10),
                      direct(0.0294, 0.05, 10), times=200L,
                      control=list("random", warmup=10))
print(res)


Dif-tor heh smusma 🖖
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

Activity
 Admin contact
20,801 posts in 4,354 threads, 1,446 registered users;
online 9 (0 registered, 9 guests [including 8 identified bots]).
Forum time: 01:22 UTC (Europe/Vienna)

Medical statistician: One who will not accept that Columbus discovered America…
because he said he was looking for India in the trial plan.    Stephen Senn

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5