一般的な行と列配置の分散分析

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

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 一般的な行と列配置の分散分析 (C言語/C++)

Keyword: 分散分析, ANOVA, 行と列配置, ラテン方格法

概要

本サンプルは一般的な行と列配置の分散分析(ANOVA: Analysis of Variance) を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される観測値をラテン方格法を用いて分散分析し、分散分析表、処理平均と処理平均の差の標準誤差を出力します。

分散分析のデータ 

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

入力データ

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

このデータをダウンロード
nag_anova_row_col (g04bcc) Example Program Data

1 5 5 5

6.67  7.15  8.29  8.95  9.62
5.40  4.77  5.40  7.54  6.93
7.32  8.53  8.50  9.99  9.68
4.92  5.00  7.29  7.85  7.08
4.88  6.16  7.83  5.38  8.51

5  4  1  3  2
2  5  4  1  3
3  2  5  4  1
1  3  2  5  4
4  1  3  2  5

  • 1行目はタイトル行で読み飛ばされます。
  • 3行目に反復数(nrep)、反復ごとの行数(nrow)、反復ごとの列数(ncol)と処理数(nt)を指定しています。
  • 5~9行目に観測値のデータ(y)を指定しています。
  • 11~15行目に各観測値が何回目の処理で処理されるか処理回数(it)を指定しています。

出力結果

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

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

 ANOVA TABLE


 Rows          4     29.4231      7.3558      9.0266    0.0013
 Columns       4     22.9950      5.7487      7.0545    0.0037

 Treatments    4      0.5423      0.1356      0.1664    0.9514
 Residual     12      9.7788      0.8149  
 Total        24     62.7392  
 Treatment means

    7.3180    7.2440    7.2060    6.9000    7.2600

 S.E. of difference (orthogonal design) =     0.5709

  • 3~11行目に分散分析表が出力されています。
  • 6行目に行の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 7行目に列の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 9行目に処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 10行目に残差の自由度、平方和、平均平方が出力されています。
  • 11行目に自由度と平方和の合計が出力されています。
  • 14行目には各処理の処理平均が出力されています。
  • 16行目には処理平均の差の標準誤差が出力されています。

ソースコード

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

※本サンプルソースコードは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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

このソースコードをダウンロード
/* nag_anova_row_col (g04bcc) 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 <nagg04.h>

int main(void)
{
  Integer c__0 = 0, exit_status = 0, i, *irep = 0, *it = 0, j, n, ncol,
         nrep, nrow, nt;
  NagError fail;
  const char *fmt_99999[] = { "%3.0f  ", "%10.4f  ", "%10.4f  ", "%10.4f  ",
    "%8.4f"
  };
  double *c = 0, c_b20 = 1e-5, *cmean = 0, *ef = 0, gmean, *r = 0;
  double *rmean = 0, *rpmean = 0, *table = 0, *tmean = 0, *y = 0;

#define TABLE(I, J) table[((I) -1)*5 + (J) -1]
#define C(I, J)     c[((I) -1)*nt + (J) -1]

  INIT_FAIL(fail);

  printf("nag_anova_row_col (g04bcc) Example Program Results\n");

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

  scanf("%ld %ld %ld %ld", &nrep,
        &nrow, &ncol, &nt);
  if (!(c = nAG_ALLOC(nt * nt, double))
      || !(cmean = nAG_ALLOC(nrep * ncol, double))
      || !(ef = nAG_ALLOC(nt, double))
      || !(r = nAG_ALLOC(nrep * nrow * ncol, double))
      || !(y = nAG_ALLOC(nrep * nrow * ncol, double))
      || !(rmean = nAG_ALLOC(nrep * nrow, double))
      || !(rpmean = nAG_ALLOC(nrep, double))
      || !(tmean = nAG_ALLOC(nt, double))
      || !(table = nAG_ALLOC(30, double))
      || !(irep = nAG_ALLOC(nt, Integer))
      || !(it = nAG_ALLOC(nrep * nrow * ncol, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  n = nrep * nrow * ncol;
  for (i = 1; i <= n; ++i)
    scanf("%lf", &y[i - 1]);
  for (i = 1; i <= n; ++i)
    scanf("%ld", &it[i - 1]);
  /* nag_anova_row_col (g04bcc).
   * Analysis of variance, general row and column design,
   * treatment means and standard errors
   */
  nag_anova_row_col(nrep, nrow, ncol, y, nt, it, &gmean, tmean, table,
                    c, nt, irep, rpmean, rmean, cmean, r, ef, c_b20, c__0,
                    &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_anova_row_col (g04bcc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n ANOVA TABLE\n\n");
  if (nrep > 1) {
    printf("\n Reps        ");
    for (j = 1; j <= 5; ++j)
      printf(fmt_99999[j - 1], TABLE(1, j));
  }

  printf("\n Rows        ");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99999[j - 1], TABLE(2, j));

  printf("\n Columns     ");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99999[j - 1], TABLE(3, j));

  printf("\n\n Treatments  ");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99999[j - 1], TABLE(4, j));

  printf("\n Residual    ");
  for (j = 1; j <= 3; ++j)
    printf(fmt_99999[j - 1], TABLE(5, j));

  printf("\n Total       ");
  for (j = 1; j <= 2; ++j)
    printf(fmt_99999[j - 1], TABLE(6, j));

  printf("\n Treatment means\n\n");
  for (i = 1; i <= nt; ++i)
    printf("%10.4f%s", tmean[i - 1], i % 6 ? "" : "\n");
  printf("\n\n S.E. of difference (orthogonal design) = %10.4f\n", C(2, 1));
END:
  nAG_FREE(c);
  nAG_FREE(cmean);
  nAG_FREE(ef);
  nAG_FREE(r);
  nAG_FREE(y);
  nAG_FREE(rmean);
  nAG_FREE(rpmean);
  nAG_FREE(tmean);
  nAG_FREE(table);
  nAG_FREE(irep);
  nAG_FREE(it);
  return exit_status;
}


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