Keyword: 分散分析, ANOVA, 完全要因計画
概要
本サンプルは完全要因計画の分散分析(ANOVA: Analysis of Variance) を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される観測値を完全要因計画による分散分析を行い、分散分析表、処理平均や処理平均の差の標準誤差を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_anova_factorial() のExampleコードです。本サンプル及び関数の詳細情報は nag_anova_factorial のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_anova_factorial のマニュアルページを参照)1 2 3 4 5 6 7
このデータをダウンロード |
nag_anova_factorial (g04cac) Example Program Data 54 3 2 2 : n nblock nfac inter 6 3 : lfac 274 361 253 325 317 339 326 402 336 379 345 361 352 334 318 339 393 358 350 340 203 397 356 298 382 376 355 418 387 379 432 339 293 322 417 342 82 297 133 306 352 361 220 333 270 388 379 274 336 307 266 389 333 353
- 1行目はタイトル行で読み飛ばされます。
- 2行目に観測値の数(n)、ブロック数(nblock)、要因数(nfac)と交互作用項の最大要因数(inter)を指定しています。
- 3行目に要因の水準の数(lfac)を指定しています。
- 5~7行目に観測値のデータ(y)を指定しています。
出力結果
(本関数の詳細はnag_anova_factorial のマニュアルページを参照)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
この出力例をダウンロード |
nag_anova_factorial (g04cac) Example Program Results ANOVA table Source df SS MS F Prob Blocks 2 30118.78 15059.39 7.68 0.00 Effect 1 5 73008.17 14601.63 7.45 0.00 Effect 2 2 21596.33 10798.17 5.51 0.01 Effect 3 10 31191.67 3119.17 1.59 0.15 Residual 34 66627.89 1959.64 Total 53 222542.83 Treatment Means and Standard Errors Effect 1 254.78 339.00 333.33 367.78 330.78 360.67 SE of difference in means = 20.87 Effect 2 334.28 353.78 305.11 SE of difference in means = 14.76 Effect 3 235.33 332.67 196.33 342.67 341.67 332.67 309.33 370.33 320.33 395.00 370.33 338.00 373.33 326.67 292.33 350.00 381.00 351.00 SE of difference in means = 36.14
- 4~13行目に分散分析表が出力されています。
- 8行目にブロックの自由度、平方和、平均平方、F統計量と有意水準が出力されています。
- 9行目に1つめの要因の主効果の処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
- 10行目に2つめの要因の主効果の処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
- 11行目に2つの要因の交互作用効果の処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
- 12行目に残差の自由度、平方和、平均平方が出力されています。
- 13行目に自由度と平方和の合計が出力されています。
- 19行目には1つめの要因の主効果の処理平均が出力されています。
- 21行目には処理平均の間の差の標準誤差が出力されています。
- 25行目には2つめの要因の主効果の処理平均が出力されています。
- 27行目には処理平均の間の差の標準誤差が出力されています。
- 31~33行目には2つの要因の交互作用効果の処理平均が出力されています。
- 35行目には処理平均の差の標準誤差が出力されています。
ソースコード
(本関数の詳細はnag_anova_factorial のマニュアルページを参照)
※本サンプルソースコードは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
このソースコードをダウンロード |
/* nag_anova_factorial (g04cac) 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 MTERM 6 #define TABLE(I, J) table[((I) -1) * (5) + ((J) -1)] int main(void) { Integer c__27 = 27, exit_status = 0, i, *imean = 0, inter, irdf, j, k, l; Integer *lfac = 0; Integer mterm = MTERM, n, nblock, nfac, ntreat, num; NagError fail; double *bmean = 0, *e = 0, *r = 0, *semean = 0, *table = 0, *tmean = 0; double *y = 0; INIT_FAIL(fail); printf("nag_anova_factorial (g04cac) Example Program Results\n\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%ld%ld%ld%ld%*[^\n]", &n, &nblock, &nfac, &inter); if (n >= 4 && nfac >= 1 && nblock >= 1 && !(n % nblock)) { if (!(r = nAG_ALLOC(n, double)) || !(y = nAG_ALLOC(n, double)) || !(lfac = nAG_ALLOC(nfac, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } } else { printf("Invalid n or nfac or nblock.\n"); exit_status = 1; return exit_status; } for (j = 0; j < nfac; ++j) scanf("%ld", &lfac[j]); scanf("%*[^\n]"); for (i = 0; i < n; ++i) scanf("%lf", &y[i]); scanf("%*[^\n]"); irdf = 0; /* nag_anova_factorial (g04cac). * Complete factorial design */ nag_anova_factorial(n, y, nfac, lfac, nblock, inter, irdf, &mterm, &table, &tmean, &c__27, &e, &imean, &semean, &bmean, r, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_anova_factorial (g04cac).\n%s\n", fail.message); exit_status = 1; goto END; } printf("\n ANOVA table\n\n"); printf(" Source df SS MS F" " Prob\n\n"); k = 0; if (nblock > 1) { ++k; printf("%s ", " Blocks "); printf("%4ld ", (Integer) TABLE(1, 1)); for (j = 2; j <= 5; ++j) printf("%12.2f", TABLE(1, j)); printf("\n"); } ntreat = mterm - 2 - k; for (i = 1; i <= ntreat; ++i) { printf("%s%2ld ", " Effect ", i); printf("%4ld ", (Integer) TABLE(k + i, 1)); for (j = 2; j <= 5; ++j) printf("%12.2f", TABLE(k + i, j)); printf("\n"); } printf("%s ", " Residual"); printf("%4ld ", (Integer) TABLE(mterm - 1, 1)); for (j = 2; j <= 3; ++j) printf("%12.2f", TABLE(mterm - 1, j)); printf("\n"); printf("%s ", " Total "); printf("%4ld ", (Integer) TABLE(mterm, 1)); printf("%12.2f\n\n", TABLE(mterm, 2)); printf(" Treatment Means and Standard Errors \n\n"); k = 0; for (i = 0; i < ntreat; ++i) { l = imean[i]; printf("%s%2ld\n\n", " Effect ", i + 1); num = 1; for (j = k; j < l; ++j) { printf("%10.2f%s", tmean[j], num % 8 ? "" : "\n"); num++; } printf("\n\n%s%10.2f\n\n", " SE of difference in means = ", semean[i]); k = l; } /* nag_anova_factorial_free (g04czc). * Memory freeing function for nag_anova_factorial (g04cac) */ nag_anova_factorial_free(&table, &tmean, &e, &imean, &semean, &bmean); END: nAG_FREE(r); nAG_FREE(y); nAG_FREE(lfac); return exit_status; }