マン・ホイットニー(Mann Whitney)のU検定

C言語によるサンプルソースコード : 使用関数名:nag_mann_whitney (g08amc)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > マン・ホイットニー(Mann Whitney)のU検定 (C言語/C++)

Keyword: ノンパラメトリック, 検定

概要

本サンプルはマン・ホイットニー(Mann Whitney)のU検定を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される2つの独立した標本を分析対象とし、検定統計量U、P値等を算出します。

U検定のデータ 

※本サンプルはnAG Cライブラリに含まれる関数 nag_mann_whitney() のExampleコードです。本サンプル及び関数の詳細情報は nag_mann_whitney のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はnag_mann_whitney のマニュアルページを参照)
1
2
3
4
5
6

このデータをダウンロード
nag_mann_whitney (g08amc) Example Program Data
 16 23
 13.0  6.0 12.0  7.0 12.0  7.0 10.0  7.0
 10.0  7.0 16.0  7.0 10.0  8.0  9.0  8.0
 17.0  6.0 10.0  8.0 15.0  8.0 15.0 10.0 15.0 10.0 14.0 10.0
 14.0 11.0 14.0 11.0 13.0 12.0 13.0 12.0 13.0 12.0 12.0

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に一つ目の標本のサンプル数 (n1=16)、二つ目の標本のサンプル数 (n2=23) を指定しています。
  • 3行目から4行目には全部で16個の数値が指定されていますが、これらは一つ目の標本の値(x)です。
  • 5行目から6行目には全部で23個の数値が指定されていますが、これらは二つ目の標本の値(y)です。

出力結果

(本関数の詳細はnag_mann_whitney のマニュアルページを参照)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

この出力例をダウンロード
nag_mann_whitney (g08amc) Example Program Results

Sample size of group 1 =    16
Sample size of group 2 =    23

Mann-Whitney U test

Data values

    Group 1   13.0  6.0 12.0  7.0 12.0  7.0 10.0  7.0
              10.0  7.0 16.0  7.0 10.0  8.0  9.0  8.0
             
    Group 2   17.0  6.0 10.0  8.0 15.0  8.0 15.0 10.0
              15.0 10.0 14.0 10.0 14.0 11.0 14.0 11.0
              13.0 12.0 13.0 12.0 13.0 12.0 12.0

Test statistic                 =  86.0000
Normal Statistic               =  -2.8039
Approximate tail probability   =   0.0025
Exact tail probability         =   0.0020

  • 1行目はタイトルです
  • 3行目は一つ目の標本のサイズが出力されています。 (入力データより読んだ値)
  • 4行目は二つ目の標本のサイズが出力されています。 (入力データより読んだ値)
  • 6行目~15行目には読み込まれたデータが出力されています。
  • 17行目は マン・ホイットニーの検定統計量であるUの値が出力されています。
  • 18行目は正規検定統計量が出力されています。
  • 19行目には裾確率 (tail probability) である P値 の近似値が出力されています。(ある程度のサンプルサイズの場合はこちらの近似値が利用可能です。)
  • 20行目には正確な裾確率 (tail probability) である P値 が出力されています。 (正確な裾確率の計算には時間がかかりますが、小さいサンプルサイズの場合はこちらの値が推奨されます)

ソースコード

(本関数の詳細はnag_mann_whitney のマニュアルページを参照)

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

このソースコードをダウンロード
/* nag_mann_whitney (g08amc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg08.h>

int main(void)
{

  Integer exit_status = 0, i, n1, n2;
  NagError fail;
  double p, u, *x = 0, *y = 0, z;

  INIT_FAIL(fail);

  printf("nag_mann_whitney (g08amc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");

  scanf("%ld %ld ", &n1, &n2);
  printf("%s%5ld\n", "Sample size of group 1 = ", n1);
  printf("%s%5ld\n", "Sample size of group 2 = ", n2);
  if (!(x = nAG_ALLOC(n1, double))
      || !(y = nAG_ALLOC(n2, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  printf("\n");
  for (i = 1; i <= n1; ++i)
    scanf("%lf", &x[i - 1]);
  printf("%s\n", "Mann-Whitney U test");
  printf("\n");
  printf("%s\n", "Data values");
  printf("\n");
  printf("%s", "    Group 1  ");
  for (i = 1; i <= n1; ++i)
    printf("%5.1f%s", x[i - 1], i % 8 ? "" : "\n             ");
  for (i = 1; i <= n2; ++i)
    scanf("%lf", &y[i - 1]);
  printf("\n");
  printf("%s", "    Group 2  ");
  for (i = 1; i <= n2; ++i)
    printf("%5.1f%s", y[i - 1], i % 8 ? "" : "\n             ");

  /* nag_mann_whitney (g08amc).
   * Performs the Mann-Whitney U test on two independent
   * samples
   */
  nag_mann_whitney(n1, x, n2, y, Nag_LowerTail, Nag_CompProbApprox,
                   &u, &z, &p, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_mann_whitney (g08amc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n\n");
  printf("%s%8.4f\n", "Test statistic                 = ", u);
  printf("%s%8.4f\n", "Normal Statistic               = ", z);
  printf("%s%8.4f\n", "Approximate tail probability   = ", p);
  /* nag_mann_whitney (g08amc), see above. */
  nag_mann_whitney(n1, x, n2, y, Nag_LowerTail, Nag_CompProbExact,
                   &u, &z, &p, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_mann_whitney (g08amc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("%s%8.4f\n", "Exact tail probability         = ", p);
END:
  nAG_FREE(x);
  nAG_FREE(y);
  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks