Keyword: 分散分析, ANOVA, 乱塊法, 完全無作為化法
概要
本サンプルは乱塊法/完全無作為化法の分散分析(ANOVA: Analysis of Variance) を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される観測値を10個のブロックに分け6回の処理で処理する釣合型不完備ブロック計画(Balanced Incomplete Block Design)により分散分析し、分散分析表、処理平均や処理平均の差の標準誤差を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_anova_random() のExampleコードです。本サンプル及び関数の詳細情報は nag_anova_random のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_anova_random のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
このデータをダウンロード |
nag_anova_random (g04bbc) Example Program Data 30 6 10 : n, nt, iblock 1 5 4 5 10 6 2 9 3 4 8 6 2 4 7 6 7 5 5 7 2 7 2 4 8 4 2 10 8 7 : y 1 2 3 1 2 4 1 3 5 1 4 6 1 5 6 2 3 6 2 4 5 2 5 6 3 4 5 3 4 6 : it
- 1行目はタイトル行で読み飛ばされます。
- 2行目に観測値の数(n)、処理数(nt)とブロック数(iblock)を指定しています。
- 3~12行目に観測値のデータ(y)を指定しています。
- 13~22行目に各観測値が何回目の処理で処理されるか処理回数(it)を指定しています。
出力結果
(本関数の詳細はnag_anova_random のマニュアルページを参照)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
この出力例をダウンロード |
nag_anova_random (g04bbc) Example Program Results ANOVA table Source df SS MS F Prob Blocks 9.0000 60.0000 6.6667 4.7872 0.0039 Treatments 5.0000 101.7778 20.3556 14.6170 0.0000 Residual 15.0000 20.8889 1.3926 Total 29.0000 182.6667 Efficiency Factors 0.00000 0.80000 0.80000 0.80000 0.80000 0.80000 Grand Mean 5.33333 Treatment Means 2.50000 7.25000 8.08333 5.91667 2.91667 5.33333 Standard errors of differences between means 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444 0.83444
- 3~10行目に分散分析表が出力されています。
- 7行目にブロックの自由度、平方和、平均平方、F統計量と有意水準が出力されています。
- 8行目に処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
- 9行目に残差の自由度、平方和と平均平方が出力されています。
- 10行目に自由度と平方和の合計が出力されています。
- 14行目には各処理の効率因子が出力されています。
- 16行目には総平均が出力されています。
- 20行目には各処理の処理平均が出力されています。
- 24~28行目には処理平均の差の標準誤差が出力されています。
ソースコード
(本関数の詳細はnag_anova_random のマニュアルページを参照)
※本サンプルソースコードは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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
このソースコードをダウンロード |
/* nag_anova_random}(g04bbc) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. * */ #include <nag.h> #include <nag_stdlib.h> #include <stdio.h> #include <nagg04.h> #define C(I, J) c[(I) *tdc + J] #define TABLE(I, J) table[(I) *tdtable + J] int main(void) { Integer exit_status = 0, i, irdf, j, n, nblock, nt, tdc, tdtable; Integer *irep = 0, *it = 0; NagError fail; Nag_Blocks blocks; double gmean, tol; double *bmean = 0, *c = 0, *ef = 0, *r = 0, *table = 0, *tmean = 0; double *y = 0; INIT_FAIL(fail); printf("nag_anova_random (g04bbc) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n] "); scanf("%ld%ld%ld%*[^\n] ", &n, &nt, &nblock); if ((nblock >= 2 && !(n % nblock)) || (nblock == 0)) { if (nblock != 0) { if (!(bmean = nAG_ALLOC(nblock, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } blocks = Nag_SerialBlocks; } else { blocks = Nag_NoBlocks; } if (!(c = nAG_ALLOC((nt) * (nt), double)) || !(ef = nAG_ALLOC(nt, double)) || !(r = nAG_ALLOC(n, double)) || !(table = nAG_ALLOC((4) * (5), double)) || !(tmean = nAG_ALLOC(nt, double)) || !(y = nAG_ALLOC(n, double)) || !(irep = nAG_ALLOC(nt, Integer)) || !(it = nAG_ALLOC(n, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } tdc = nt; tdtable = 5; } else { printf("Invalid nblock or n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; ++i) scanf("%lf", &y[i]); scanf("%*[^\n]"); for (i = 0; i < n; ++i) scanf("%ld", &it[i]); scanf("%*[^\n]"); tol = 5e-6; irdf = 0; /* nag_anova_random (g04bbc). * General block design or completely randomized design */ nag_anova_random(n, y, blocks, nblock, nt, it, &gmean, bmean, tmean, table, c, tdc, irep, r, ef, tol, irdf, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_anova_random (g04bbc).\n%s\n", fail.message); exit_status = 1; goto END; } printf("\nANOVA table\n\n"); printf(" Source df SS MS F" " Prob\n\n"); printf(" Blocks "); for (j = 0; j < 5; ++j) printf("%10.4f ", TABLE(0, j)); printf("\n"); printf(" Treatments "); for (j = 0; j < 5; ++j) printf("%10.4f ", TABLE(1, j)); printf("\n"); printf(" Residual "); for (j = 0; j < 3; ++j) printf("%10.4f ", TABLE(2, j)); printf("\n"); printf(" Total "); for (j = 1; j <= 2; ++j) printf("%10.4f ", TABLE(3, j - 1)); printf("\n"); printf("\nEfficiency Factors\n\n"); for (i = 0; i < nt; ++i) printf("%10.5f", ef[i]); printf("\n"); printf("\n%s%10.5f\n", " Grand Mean", gmean); printf("\nTreatment Means\n\n"); for (i = 1; i <= nt; ++i) printf("%10.5f", tmean[i - 1]); printf("\n"); printf("\nStandard errors of differences between means\n\n"); for (i = 1; i < nt; ++i) { for (j = 0; j < i; ++j) printf("%10.5f", C(i, j)); printf("\n"); } END: nAG_FREE(bmean); nAG_FREE(c); nAG_FREE(ef); nAG_FREE(r); nAG_FREE(table); nAG_FREE(tmean); nAG_FREE(y); nAG_FREE(irep); nAG_FREE(it); return exit_status; }