R code for power [🇷 for BE/BA]

posted by ElMaestro  – Denmark, 2009-10-09 13:32 (5731 d 04:31 ago) – Posting: # 4332
Views: 33,995

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,424 posts in 4,927 threads, 1,671 registered users;
26 visitors (0 registered, 26 guests [including 11 identified bots]).
Forum time: 18:03 CEST (Europe/Vienna)

Truth and clarity are complementary.    Niels Bohr

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