完全要因計画の分散分析

Fortranによるサンプルソースコード : 使用ルーチン名:g04caf

Keyword: 分散分析, ANOVA, 完全要因計画

概要

本サンプルは完全要因計画の分散分析(ANOVA: Analysis of Variance) を行うFortranによるサンプルプログラムです。 本サンプルは以下に示される観測値を完全要因計画による分散分析を行い、分散分析表、処理平均や処理平均の差の標準誤差を出力します。

分散分析のデータ 

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

入力データ

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

このデータをダウンロード
G04CAF 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=54)、ブロック数(nblock=3)、要因数(nfac=2)と交互作用項の最大要因数(inter=2)を指定しています。
  • 3行目に要因の水準の数(lfac)を指定しています。
  • 4~6行目に観測値のデータ(y)を指定しています。

出力結果

(本ルーチンの詳細はg04caf のマニュアルページを参照)
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

この出力例をダウンロード
 G04CAF Example Program Results

  ANOVA table

   Source      df         SS          MS          F        Prob

 Blocks        2.      30119.      15059.       7.685     0.0018
 Effect   1    5.      73008.      14602.       7.451     0.0001
 Effect   2    2.      21596.      10798.       5.510     0.0085
 Effect   3   10.      31192.       3119.       1.592     0.1513
 Residual     34.      66628.       1960.
 Total        53.     222543.

  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


  • 3~12行目に分散分析表が出力されています。
  • 7行目にブロックの自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 8行目に1つめの要因の主効果の処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 9行目に2つめの要因の主効果の処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 10行目に2つの要因の交互作用効果の処理の自由度、平方和、平均平方、F統計量と有意水準が出力されています。
  • 11行目に残差の自由度、平方和、平均平方が出力されています。
  • 12行目に自由度と平方和の合計が出力されています。
  • 18行目には1つめの要因の主効果の処理平均が出力されています。
  • 20行目には処理平均の間の差の標準誤差が出力されています。
  • 24行目には2つめの要因の主効果の処理平均が出力されています。
  • 26行目には処理平均の間の差の標準誤差が出力されています。
  • 30~32行目には2つの要因の交互作用効果の処理平均が出力されています。
  • 35行目には処理平均の差の標準誤差が出力されています。

ソースコード

(本ルーチンの詳細はg04caf のマニュアルページを参照)

※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法

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

このソースコードをダウンロード
    PROGRAM g04cafe

!      G04CAF Example Program Text

!      Mark 23 Release. nAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g04caf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, inter, irdf, itotal, k, l, &
                                          maxt, mterm, n, nblock, nfac, ntreat
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: bmean(:), e(:), r(:), semean(:),     &
                                          table(:,:), tmean(:), y(:)
       INTEGER, ALLOCATABLE            :: imean(:), iwk(:), lfac(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G04CAF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)

!      Read in problem size
       READ (nin,*) n, nblock, nfac, inter

       ALLOCATE (lfac(nfac),iwk(n+3*nfac),y(n),bmean(nblock+1),r(n))

!      Read in the number of levels for each factor
       READ (nin,*) lfac(1:nfac)

!      Read in the observations
       READ (nin,*) y(1:n)

!      Use standard degrees of freedom
       irdf = 0

!      Using call to G04CAF to calculate required values for MAXT and MTERM ...

!      Setting MAXT to zero ensures it is too small and hence the routine
!      will calculate its correct size, IMEAN needs to be at least 1 element
!      long so set MTERM to 1.
       maxt = 0
       mterm = 1

!      Dummy allocation
       ALLOCATE (tmean(maxt),e(maxt),table(mterm,5),semean(mterm), &
          imean(mterm))

!      Call the routine initially to get MTERM and MAXT
       ifail = 1
       CALL g04caf(n,y,nfac,lfac,nblock,inter,irdf,mterm,table,itotal,tmean, &
          maxt,e,imean,semean,bmean,r,iwk,ifail)
       IF (ifail/=0 .AND. ifail/=2) THEN
          WRITE (nout,99996) ' ** G04CAF exited with IFAIL = ', ifail
          GO TO 20
       END IF

!      Allocate remaining output arrays
       mterm = itotal
       maxt = imean(1)
       DEALLOCATE (tmean,e,table,semean,imean)
       ALLOCATE (tmean(maxt),e(maxt),table(mterm,5),semean(mterm), &
          imean(mterm))

!      Calculate the ANOVA table
       ifail = 0
       CALL g04caf(n,y,nfac,lfac,nblock,inter,irdf,mterm,table,itotal,tmean, &
          maxt,e,imean,semean,bmean,r,iwk,ifail)

!      Display results
       WRITE (nout,*) ' ANOVA table'
       WRITE (nout,*)
       WRITE (nout,*) '  Source      df         SS          MS          F', &
          '        Prob'
       WRITE (nout,*)
       k = 0
       IF (nblock>1) THEN
          k = k + 1
          WRITE (nout,99998) ' Blocks    ', table(1,1:5)
       END IF
       ntreat = itotal - 2 - k
       DO i = 1, ntreat
          WRITE (nout,99997) ' Effect  ', i, table(k+i,1:5)
       END DO
       WRITE (nout,99998) ' Residual  ', table(itotal-1,1:3)
       WRITE (nout,99998) ' Total     ', table(itotal,1:2)
       WRITE (nout,*)
       WRITE (nout,*) ' Treatment Means and Standard Errors'
       WRITE (nout,*)
       k = 1
       DO i = 1, ntreat
          l = imean(i)
          WRITE (nout,99996) ' Effect ', i
          WRITE (nout,*)
          WRITE (nout,99999) tmean(k:l)
          WRITE (nout,*)
          WRITE (nout,99995) ' SE of difference in means  = ', semean(i)
          WRITE (nout,*)
          k = l + 1
       END DO

20     CONTINUE

99999  FORMAT (8F10.2)
99998  FORMAT (A,3X,F3.0,2X,2(F10.0,2X),F10.3,2X,F9.4)
99997  FORMAT (A,I2,3X,F3.0,2X,2(F10.0,2X),F10.3,2X,F9.4)
99996  FORMAT (A,I5)
99995  FORMAT (A,F10.2)
    END PROGRAM g04cafe


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