Created
January 17, 2022 10:29
-
-
Save silversillu/41aa195e6d7a7451dac92b442c25fe58 to your computer and use it in GitHub Desktop.
MDE Calculator Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ppnd(p) { | |
a0 = 2.50662823884, a1 = -18.61500062529; a2 = 41.39119773534; a3 = -25.44106049637; b1=-8.47351093090; b2 = 23.08336743743; b3 = -21.06224101826; b4 = 3.13082909833; c0 = -2.78718931138; c1 = -2.29796479134; c2 = 4.85014127135; c3 = 2.32121276858; d1 = 3.54388924762; d2 = 1.63706781897; | |
var r; | |
var split = 0.42; | |
var value; | |
/* 0.08 < P < 0.92 */ | |
if (Math.abs(p-0.5)<=split){ | |
r=(p-0.5)*(p-0.5); | |
value=(p-0.5)*(((a3*r+a2)*r+a1)*r+a0)/((((b4*r+b3)*r+b2)*r+b1)*r+1.0); | |
} | |
/* P < 0.08 or P > 0.92, | |
R = min ( P, 1-P ) */ | |
else if(0.0<p && p<1.0){ | |
if(0.5<p) r=Math.sqrt(-Math.log(1.0-p)); | |
else r = Math.sqrt(-Math.log(p)); | |
value = (((c3*r+c2)*r+c1)*r+c0)/((d2*r+d1)*r+1.0); | |
if(p<0.5) value = - value; | |
} | |
/* P <= 0.0 or 1.0 <= P */ | |
else value = NaN; | |
return value; | |
} | |
const dunnett_z = (p,tails,Variants)=>{ | |
var coefs=[]; | |
poly_rank=9; | |
if(tails==1){ | |
coefs[1]=[-3.130385,55.34448,-715.9325,5235.129,-22224.02,57314.93,-91012.3,86844.54,-45632.87,10141.43]; | |
coefs[2]=[-2.434125,49.83425,-647.1348,4752.59,-20255.26,52422.9,-83508.44,79911.14,-42097.12,9377.248]; | |
coefs[3]=[-2.121302,47.82393,-621.974,4574.116,-19517.85,50567.02,-80625.85,77215.23,-40706.22,9073.262]; | |
coefs[4]=[-1.930303,46.77481,-608.8468,4480.195,-19125.93,49570.7,-79063.26,75740.3,-39938.61,8904.111]; | |
coefs[5]=[-1.796614,46.13057,-600.7998,4422.195,-18881.77,48944.54,-78072.93,74798.12,-39444.63,8794.507]; | |
coefs[6]=[-1.695463,45.69606,-595.3876,4382.92,-18715.08,48513.52,-77385.92,74139.77,-39097.16,8716.941]; | |
coefs[7]=[-1.61498,45.3845,-591.5205,4354.675,-18594.23,48198.57,-76880.19,73651.85,-38838.05,8658.774]; | |
coefs[8]=[-1.548647,45.15123,-588.6371,4333.479,-18502.82,47958.47,-76491.86,73274.75,-38636.62,8613.318]; | |
coefs[9]=[-1.492536,44.97085,-586.4174,4317.055,-18431.43,47769.48,-76184.04,72973.96,-38475.05,8576.677]; | |
coefs[10]=[-1.444115,44.82781,-584.6658,4304.008,-18374.25,47616.96,-75933.88,72728.01,-38342.24,8546.416]; | |
} | |
if(tails==2){ | |
coefs[1]=[-0.01144596,4.352578,-90.54801,1004.088,-5580.947,17458.51,-32099.94,34391.93,-19857.28,4773.163]; | |
coefs[2]=[0.02111707,9.844298,-146.6649,1339.796,-6777.035,20060.73,-35576.61,37161.7,-21057.17,4988.887]; | |
coefs[3]=[0.09486066,13.24264,-189.4325,1619.879,-7834.534,22479.59,-38976.24,40026.06,-22382.01,5246.966]; | |
coefs[4]=[0.17702,14.85503,-210.8122,1761.529,-8369.46,23696.9,-40672.46,41438.68,-23025.83,5370.105]; | |
coefs[5]=[0.2539563,15.61456,-221.2075,1829.994,-8623.877,24262.83,-41438.35,42053.49,-23293.27,5418.251]; | |
coefs[6]=[0.3226849,15.96869,-226.2315,1862.437,-8740.18,24508.85,-41749.24,42280.26,-23378.88,5430.458]; | |
coefs[7]=[0.3834631,16.11954,-228.5149,1876.51,-8786.418,24593.83,-41833.31,42315.84,-23376.11,5425.477]; | |
coefs[8]=[0.4373194,16.16322,-229.3324,1880.803,-8795.706,24594.94,-41801.08,42255.2,-23329.85,5412.266]; | |
coefs[9]=[0.4853314,16.14836,-229.3371,1879.718,-8784.968,24551.47,-41708.48,42146.08,-23262.51,5395.257]; | |
coefs[10]=[0.5284387,16.10098,-228.8816,1875.627,-8763.279,24484.33,-41585.07,42013.49,-23185.67,5376.714]; | |
} | |
var result=0; | |
result=coefs[Variants-1][0]; | |
for(i=1;i<=poly_rank;i++){ | |
result+=coefs[Variants-1][i]*Math.pow(p,i); | |
} | |
if (Variants==2) return ppnd(1-(1-p)/tails); // regular ppnd for 2 variants case | |
return result; | |
} | |
const fn=(p0,p1,za,zb)=>{ | |
return ((za+zb)**2*(p0+p1))/((p1-p0)**2); | |
} | |
function calcmde (n,p0,tails,variants,confidence,power,relative){ | |
var z_dunnett=dunnett_z(confidence,tails,variants); | |
var z_beta=ppnd(power); | |
var p1=p0; | |
while(fn(p0,p1,z_dunnett,z_beta)>=n/variants) p1+=0.00001; | |
var mde = 0; | |
if(relative) { | |
mde = (p1-p0)/p0; | |
} | |
else { | |
mde = p1-p0; | |
} | |
var formatted_mde = (mde*100).toFixed(2); | |
var result = {}; | |
result = { | |
mde_raw: mde, | |
mde: formatted_mde, | |
status: 'ok' | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment