メイン sample-size2.html   Last modified: Sep 01, 2009
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;CHARSET=EUC-JP">
<link rel="shortcut icon" href="../favicon.ico">
<title>JavaScript</title>

<script language="JavaScript">
<!--
var cof = new Array(76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5)

function gammln(x)
{
  var y, tmp, ser, j
  y = x
  tmp = x+5.5
  tmp -= (x+0.5)*Math.log(tmp)
  ser = 1.000000000190015
  for (j = 0; j <= 5; j++) {
    ser += cof[j]/++y
  }
  return Math.log(2.5066282746310005*ser/x)-tmp
}

function error(i)
{
  alert("Error! "+i)
  return
}

function gcf(a, x)
{
  var i, an, b, c, d, del, h
  var ITMAX = 100
  var EPS = 3e-7
  var FPMIN = 1e-30

  b = x+1-a
  c = 1/FPMIN
  d = 1/b
  h = d
  for (i = 1; i <= ITMAX; i++) {
    an = -i*(i-a)
    b += 2
    d = an*d+b
    if (Math.abs(d) < FPMIN) d = FPMIN
    c = b+an/c
    if (Math.abs(c) < FPMIN) c = FPMIN
    d = 1/d
    del = d*c
    h *= del
    if (Math.abs(del-1) < EPS) {
      return Math.exp(-x+a*Math.log(x)-gammln(a))*h
    }
  }
  error(3)
  return -99999
}

function gser(a, x)
{
  var n, sum, del, ap
  var ITMAX = 100
  var EPS = 3e-7

  if (x == 0) {
    return 0
  }
  else if (x > 0) {
    ap = a
    del = sum = 1/a
    for (n = 1; n <= ITMAX; n++) {
      ++ap
      del *= x/ap
      sum += del
      if (Math.abs(del) < Math.abs(sum)*EPS) {
        return sum*Math.exp(-x+a*Math.log(x)-gammln(a))
      }
    }
  }
  error(4)
  return -99999
}

function gammp(a, x)
{
  return (x < a+1) ? gser(a, x) : 1-gcf(a,x)
}

function erff(x)
{
  return x < 0 ? -gammp(0.5,x*x) : gammp(0.5,x*x)
}

function betacf(a, b, x)
{
  var ITMAX = 100
  var EPS = 3e-7
  var FPMIN = 1e-30
  var m, m2, aa, c, d, del, h, qab, qam, qap

  qab = a+b
  qap = a+1
  qam = a-1
  c = 1
  d = 1-qab*x/qap
  if (Math.abs(d) < FPMIN) d = FPMIN
  d = 1/d
  h = d
  for (m = 1; m <= ITMAX; m++) {
    m2 = 2*m
    aa = m*(b-m)*x/((qam+m2)*(a+m2))
    d = 1+aa*d
    if (Math.abs(d) < FPMIN) d = FPMIN
    c = 1+aa/c
    if (Math.abs(c) < FPMIN) c = FPMIN
    d = 1/d
    h *= d*c
    aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2))
    d = 1+aa*d
    if (Math.abs(d) < FPMIN) d = FPMIN
    c = 1+aa/c
    if (Math.abs(c) < FPMIN) c = FPMIN
    d = 1/d
    del = d*c
    h *= del
    if (Math.abs(del-1) < EPS) break
  }
  return (m > ITMAX) ? error(1) : h
}
  
function betai(a, b, x)
{
  var bt
  if (x < 0 || x > 1) error(2)
  bt = (x == 0 || x == 1) ? 0 : Math.exp(gammln(a+b)-gammln(a)-gammln(b)+a*Math.log(x)+b*Math.log(1-x))
  return (x < (a+1)/(a+b+2)) ? bt*betacf(a,b,x)/a : 1-bt*betacf(b,a,1-x)/b
}

function pxt(p0, df)
{
  var p9, xl, xm, xr
  var EPS = 0.000001
  var ok = 0

  if (p0 == 1) {
    return 0
  }

  xl = 0
  xr = 10
  while (xr < 1e30) {
    p9 = betai(0.5*df, 0.5, df/(df+xr*xr))
    if (p9 == p0) {
      return xr
    }
    else if (p9 < p0) {
      ok = 1
      break
    }
    xl = xr
    xr *= 10
  }

  if (!ok) error(5)

  for (;;) {
    xm = (xl+xr)*0.5
    p9 = betai(0.5*df, 0.5, df/(df+xm*xm))
    if (p9 > p0) {
      xl = xm
    }
    else {
      xr = xm
    }
    if (Math.abs(xr-xl)/xm < EPS) {
      break
    }
  }
  return xm
}

function sub1(n, esize, alpha)
{
  var t, df, dd
  df = n-2
  t = pxt(alpha, df)
  dd = 1-0.25/df+1/(32*df*df)
  return 0.5*(1+erff((esize*Math.sqrt(n)-1.41421356237309504880*t*dd) / (2*Math.sqrt(1+t*t*(1-dd*dd)))))
}

function sample_size(alpha, powd, esize)
{
  var n = 0
  var powa = 0
  var INTV = 200
  var EPS = 0.001
  var dir = -1

  while (powa <= powd) {
    n += 100
    powa = sub1(n, esize, alpha)
  }

  while (Math.abs((powa-powd)/powd) >= EPS) {
    INTV *= 0.5
    n += dir*INTV*0.5
    powa = sub1(n, esize, alpha)
    dir = (powa < powd) ? 1 : -1
  }
  return n
}


function calc(form)
{
// ***** 二群の平均値の差の検定に必要な標本サイズ *****
  var alpha, beta, d
  if (isNaN(alpha = parseFloat(form.alpha.value)) || alpha <= 0 || alpha >= 1 ||
      isNaN(beta = parseFloat(form.beta.value)) || beta <= 0 || beta >= 1 ||
      isNaN(d = parseFloat(form.d.value)) || d == 0) {
      form.n.value = "入力値が変です"
    return
  }
  form.n.value = Math.ceil(sample_size(alpha, beta, d))
}
//-->
</script>
</head>

<body bgcolor="#ffffff">
<font size="+2"><b>二群の平均値の差の検定に必要な標本サイズの決定</b></font><br> <a  href="src/sample-size2.html"><img src="png/src.png" width=35 height=11 alt="src" align=top></a>     Last modified: Jun 01, 2006<hr noshade><p>
<font color="#ff0000" size="+2">以下のプログラムのサポートは終了しました。自己責任でお使い下さい。</font>
<script language="JavaScript">
<!--
//-->JavaScript がサポートされていないブラウザですか?
</script>
<hr noshade>
<form name="PowerAnalysys">
<table>
<tr><th align=right>Effect size</th><td><input name="d" value=0.5 size=10></td></tr>
<tr><th align=right>有意水準</th><td><input name="alpha" value=0.05 size=10></td></tr>
<tr><th align=right>検出力</th><td><input name="beta" value=0.8 size=10></td></tr>
<tr><td align=right><input type="button" name="clear" value="初期化" onClick="this.form.d.value=this.form.alpha.value=this.form.beta.value=this.form.n.value=''"></td><td><input type="button" name="calcurate" value="計算開始" onClick="calc(this.form)"></td></tr>
<tr><th align=right>両側検定標本サイズ</th><td><input name="n" value="" size=10></td></tr>
<tr><td align=right colspan=2>各群あたりの標本サイズです</td></tr>
</table>
</form>
<p><hr noshade>
<img src="../gra/button3.png" width=9 height=9 alt="・"> <A HREF="../lecture/SampleSize/index.html"> 手法の解説</a><br>
<img src="../gra/button3.png" width=9 height=9 alt="・"> <A HREF="javascript:history.go(-1)">直前のページへ戻る</A>  <img src="../gra/button3.png" width=9 height=9 alt="・"> <a href="../mail.html">E-mail to Shigenobu AOKI</a>
<p><center><IMG SRC="../gra/ume5.png" width=121 height=37 ALT="Made with Macintosh"></center>
</body>
</html>


・ 直前のページへ戻る  ・ E-mail to Shigenobu AOKI

Made with Macintosh