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

posted by Helmut Homepage – Vienna, Austria, 2017-08-20 14:40 (1079 d 00:02 ago) – Posting: # 17719
Views: 24,569

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,963 posts in 4,373 threads, 1,459 registered users;
online 15 (1 registered, 14 guests [including 4 identified bots]).
Forum time: Monday 14:42 CEST (Europe/Vienna)

Garbage in, garbage out.
It’s very very simple.    Anders Fuglsang

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