R code for power [🇷 for BE/BA]

posted by ElMaestro  – Denmark, 2009-10-09 13:32 (6091 d 01:13 ago) – Posting: # 4332
Views: 45,681

What comes out of lying sleepless at night:


EatMyShorts <- function (x, V, T, W)
{
 
  l1 = pnorm((T*x/sqrt(V)) - W);
 
  l2 = (V-1.0)*log(x) -lgamma(0.5*V) - (0.5*(V-2.0))* log (2.0);
  l3 = exp(l2);
  l4 =dnorm(x);
  l1*l3*l4;
 
}

Apfelstrudel<-function(t, df, ta, tb, ncp)
{
  L1 = sqrt(2*pi);
  P=0;
  step = (tb-ta)/500;
  foo=ta;
  for (i in 1:500)
    {
      y1 = EatMyShorts(foo, df, t, ncp);
      y2 = EatMyShorts(foo+step, df,t, ncp);
      P=P+step*0.5*(y1+y2); ## the trapez meffud!
      foo=foo+step;
    }
  L1*P
}


Pwr222 <- function (N, alpha, AccLimitLo, AccLimitHi, TRRatio, CV)
{
  df = N-2;
  tc = qt( 1-alpha, df);
  k  = sqrt(2/N);
  d1 = (log(TRRatio)-log(AccLimitLo))/(k * sqrt(log(1+CV*CV)));
  d2 = (log(TRRatio)-log(AccLimitHi))/(k * sqrt(log(1+CV*CV)));
  R  = sqrt(df) * (d1-d2) / (2*tc);
 
  P2 = Apfelstrudel(-tc, df, 0,R,d2);
  P1 = Apfelstrudel(tc, df, 0,R,d1);
 
  P2-P1
}


Increasing the integration steps (to some number higher than 500) may ímprove accuracy, as will e.g. a method like Simpson's formula. Can be played around with.

Best regards
EM.

Complete thread:

UA Flag
Activity
 Admin contact
23,653 posts in 4,991 threads, 1,570 registered users;
114 visitors (0 registered, 114 guests [including 22 identified bots]).
Forum time: 14:45 CEST (Europe/Vienna)

To propose that poor design can be corrected by subtle analysis techniques
is contrary to good scientific thinking.    Stuart J. Pocock

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