メイン disc.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 src="inv.js">document.write("inv.js ファイルが見つかりません??<br>")</script>
<script src="io.js">document.write("io.js ファイルが見つかりません??<br>")</script>

<script language="JavaScript">
<!--
var epsinv = 1e-8

function zfi(w, i)
{
  var ww = (""+i).length
  ww = ww > w ? 0 : w-ww
  return "000000000000000".substring(0, ww)+i
}

function vname(i)
{
  return "Var"+zfi(2, i+1)
}

function calc(data_string)
{
  var data, nc, nv, n, g, i, j, k, temp2, constant, signum, pred, res, mark
  var mean, s, v, a, result, diff
  if ((data = getdata(data_string, 0)) == false) return
  nc = data.length
  nv = data[0].length-1

  if (nc < 2) {
    printf("ケース数が1以下です\n")
    return
  }

  mean = makeMatrix(2,nv)
  diff = new Array(nv)
  a = new Array(nv)
  s = makeMatrix(nv, nv)
  result = makeMatrix(2,2)
  n = new Array(2)

// Netscape communicator のバグ?配列ベクトルは初期化されていない
  n[0] = n[1] = 0
  for (i = 0; i < nv; i++) {
    mean[0][i] = mean[1][i] = 0
    for (j = 0; j < nv; j++) {
      s[i][j] = 0
    }
  }
  result[0][0] = result[0][1] = result[1][0] = result[1][1] = 0

  for (i = 0; i < nc; i++) {
    g = data[i][nv] == 1 ? 0 : 1
    n[g]++
    for (j = 0; j < nv; j++) {
      mean[g][j] += data[i][j]
    }
  }
  for (j = 0; j < nv; j++) {
    for (i = 0; i < 2; i++) {
      mean[i][j] /= n[i]
    }
    diff[j] = mean[0][j]-mean[1][j]
  }

  for (i = 0; i < nc; i++) {
    g = data[i][nv] == 1 ? 0 : 1
    for (j = 0; j < nv; j++) {
      for (k = 0; k < nv; k++) {
        s[j][k] += (mean[g][j]-data[i][j])*(mean[g][k]-data[i][k])
      }
    }
  }
  g = n[0]+n[1]-2
  for (i = 0; i < nv; i++) {
    for (j = 0; j < nv; j++) {
      s[i][j] /= g
    }
  }

  printf("★ 判別分析 ★\n\n")
  printf("有効データ組数 = %i\n\n", nc)
  printf("第 1 群データ組数 = %i\n", n[0])
  printf("第 2 群データ組数 = %i\n", n[1])
  
  printf("\n各群の平均値\n\n%5s%15s%15s\n", "変数", "第 1 群", "第 2 群")
  for (j = 0; j < nv; j++) {
    printf("%5s%15.7g%15.7g\n", vname(j), mean[0][j], mean[1][j])
  }

  if (inv(s, nv, epsinv) == 0) {
    for (i = 0; i < nv; i++) {
      temp2 = 0
      for (j = 0; j < nv; j++) {
        temp2 += s[i][j]*diff[j]
      }
      a[i] = temp2
    }
    constant = signum = 0
    for (j = 0; j < nv; j++) {
      constant -= (mean[0][j]+mean[1][j])*a[j]*0.5
      signum += mean[0][j]*a[j]
    }
    signum += constant
    printf("\n判別関数\n\n%6s%15s\n", "変数", "判別係数")
    for (i = 0; i < nv; i++) {
      printf("%6s%15.7g\n", vname(i), a[i])
    }
    printf("%6s%15.7g\n", "定数項", constant)

    printf("\n判別結果\n\n%5s %10s %12s %12s   %s\n", "番号", "実際の群", "予測された群", "判別係数", "判別結果")
    for (i = 0; i < nc; i++) {
      g = data[i][nv] == 1 ? 0 : 1
      pred = constant
      for (j = 0; j < nv; j++) {
        pred += a[j]*data[i][j]
      }
      if (signum > 0) {
        res = pred >= 0 ? 0 : 1
      }
      else {
        res = pred >= 0 ? 1 : 0
      }
      result[g][res]++
      mark = g == res ? "   " : "誤判別"
      printf("%5i %10i %12i %12.6f   %6s\n", i+1, data[i][nv], res+1, pred, mark)
    }
    printf("\n判別結果総括表\n\n            予測された群\n")
    printf("       %13s %13s %8s\n", "第 1 群", "第 2 群", "合計")
    printf("実際は第 1 群 %5i(%5.1f%%) %5i(%5.1f%%) %8i\n", result[0][0], result[0][0]/n[0]*100, result[0][1], result[0][1]/n[0]*100, n[0])
    printf("実際は第 2 群 %5i(%5.1f%%) %5i(%5.1f%%) %8i\n", result[1][0], result[1][0]/n[1]*100, result[1][1], result[1][1]/n[1]*100, n[1])
    printf("\n正判別率 = (%i+%i) / %i = %.1f%%\n", result[0][0], result[1][1], nc, (result[0][0]+result[1][1])/nc*100)
  }
}
//-->
</script>
</head>

<body bgcolor="#ffffff">
<font size="+2"><b>二群の判別分析</b></font> <a  href="src/disc.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>

<form name=Result>
<script language="JavaScript">
<!--
//-->JavaScript がサポートされていないブラウザですか?
</script>
二群の判別分析を行う(3 群以上の判別をする場合は,<a href="../BlackBox/BlackBox.html">CGI(C)</a>,<a href="../lecture/stats-by-excel/vba/html/sdis.html">Excel VBA</a> によるプログラムを使用すること)。<br>
<img src="../gra/button3.png" width=9 height=9 alt="・"> <a href="../lecture/Discriminant/index.html">手法の解説</a> <img src="../gra/button3.png" width=9 height=9 alt="・"> <a href="exa/disc.html">使用法</a>
<hr>

<table><tr>
<td><input type="button" name="calcurate" value="計算開始" onClick="calc(this.form.data.value)">  </td>
<td><input type="button" name="clear" value="入力欄クリア" onClick="this.form.data.value=''">  </td>
<td><input type="button" name="clear" value="出力欄クリア" onClick="this.form.result.value=''"></td>
</tr></table>

<p>
入力欄<br><textarea name="data" ROWS=20 COLS=80></textarea><p>
出力欄<br><textarea name="result" ROWS=30 COLS=80></textarea>
</form>

<p><hr noshade>
<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><IMG SRC="../gra/ume5.png" width=121 height=37 ALT="Made with Macintosh">
</body>
</html>

サブ inv.js   Last modified: Mar 25, 2004
サブ io.js   Last modified: Mar 25, 2004

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

Made with Macintosh