Keyword: 単線形回帰
概要
本サンプルは単線形回帰を行うFortranによるサンプルプログラムです。 本サンプルは以下に示される観測値について単線形回帰分析を行い、回帰定数、回帰係数、回帰の残差の平方和や自由度を出力します。
※本サンプルはnAG Fortranライブラリに含まれるルーチン g02caf() のExampleコードです。本サンプル及びルーチンの詳細情報は g02caf のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本ルーチンの詳細はg02caf のマニュアルページを参照)| このデータをダウンロード |
G02CAF Example Program Data 8 :: N 1.0 20.0 0.0 15.5 4.0 28.3 7.5 45.0 2.5 24.5 0.0 10.0 10.0 99.0 5.0 31.2 :: End of X,Y
- 1行目はタイトル行で読み飛ばされます。
- 2行目に観測値のペアの数(n)を指定しています。
- 3から10行目には縦1列めに独立変数の値(x)、縦2列めに従属変数の値(y)を指定しています。
出力結果
(本ルーチンの詳細はg02caf のマニュアルページを参照)| この出力例をダウンロード |
G02CAF Example Program Results
Case Independent Dependent
number variable variable
1 1.0000 20.0000
2 0.0000 15.5000
3 4.0000 28.3000
4 7.5000 45.0000
5 2.5000 24.5000
6 0.0000 10.0000
7 10.0000 99.0000
8 5.0000 31.2000
Mean of independent variable = 3.7500
Mean of dependent variable = 34.1875
Standard deviation of independent variable = 3.6253
Standard deviation of dependent variable = 28.2604
Correlation coefficient = 0.9096
Regression coefficient = 7.0905
Standard error of coefficient = 1.3224
t-value for coefficient = 5.3620
Regression constant = 7.5982
Standard error of constant = 6.6858
t-value for constant = 1.1365
Analysis of regression table :-
Source Sum of squares D.F. Mean square F-value
Due to regression 4625.303 1. 4625.303 28.751
About regression 965.245 6. 160.874
Total 5590.549 7.
- 6〜13行目にケース番号と独立変数、従属変数が出力されています。
- 15行目に独立変数の平均が出力されています。
- 16行目に従属変数の平均が出力されています。
- 17行目に独立変数の標準偏差が出力されています。
- 18行目に従属変数の標準偏差が出力されています。
- 19行目に相関係数が出力されています。
- 21行目に回帰係数が出力されています。
- 22行目に回帰係数の標準誤差が出力されています。
- 23行目に回帰係数のt値が出力されています。
- 25行目に回帰定数が出力されています。
- 26行目に回帰定数の標準誤差が出力されています。
- 27行目に回帰定数のt値が出力されています。
- 29〜36行目に回帰の分散分析表が出力されています。
- 33行目に回帰に起因する平方和、自由度、平均平方、F値が出力されています。
- 34行目に回帰の偏差の平方和、自由度、平均平方が出力されています。
- 35行目に平方和と自由度の合計が出力されています。
ソースコード
(本ルーチンの詳細はg02caf のマニュアルページを参照)
※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
PROGRAM g02cafe
! G02CAF Example Program Text
! Mark 23 Release. nAG Copyright 2011.
! .. Use Statements ..
USE nag_library, ONLY : g02caf, nag_wp
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Parameters ..
INTEGER, PARAMETER :: nin = 5, nout = 6
! .. Local Scalars ..
INTEGER :: i, ifail, n
! .. Local Arrays ..
REAL (KIND=nag_wp) :: reslt(20)
REAL (KIND=nag_wp), ALLOCATABLE :: x(:), y(:)
! .. Executable Statements ..
WRITE (nout,*) 'G02CAF Example Program Results'
WRITE (nout,*)
! Skip heading in data file
READ (nin,*)
! Read in the problem size
READ (nin,*) n
ALLOCATE (x(n),y(n))
! Read in data
READ (nin,*) (x(i),y(i),i=1,n)
! Display data
WRITE (nout,*) ' Case Independent Dependent'
WRITE (nout,*) 'number variable variable'
WRITE (nout,*)
WRITE (nout,99999) (i,x(i),y(i),i=1,n)
WRITE (nout,*)
! Fit linear regression model
ifail = 0
CALL g02caf(n,x,y,reslt,ifail)
! Display results
WRITE (nout,99998) 'Mean of independent variable = ', &
reslt(1)
WRITE (nout,99998) 'Mean of dependent variable = ', &
reslt(2)
WRITE (nout,99998) 'Standard deviation of independent variable = ', &
reslt(3)
WRITE (nout,99998) 'Standard deviation of dependent variable = ', &
reslt(4)
WRITE (nout,99998) 'Correlation coefficient = ', &
reslt(5)
WRITE (nout,*)
WRITE (nout,99998) 'Regression coefficient = ', &
reslt(6)
WRITE (nout,99998) 'Standard error of coefficient = ', &
reslt(8)
WRITE (nout,99998) 't-value for coefficient = ', &
reslt(10)
WRITE (nout,*)
WRITE (nout,99998) 'Regression constant = ', &
reslt(7)
WRITE (nout,99998) 'Standard error of constant = ', &
reslt(9)
WRITE (nout,99998) 't-value for constant = ', &
reslt(11)
WRITE (nout,*)
WRITE (nout,*) 'Analysis of regression table :-'
WRITE (nout,*)
WRITE (nout,*) ' Source Sum of squares D.F. &
& Mean square F-value'
WRITE (nout,*)
WRITE (nout,99997) 'Due to regression', reslt(12:15)
WRITE (nout,99997) 'About regression', reslt(16:18)
WRITE (nout,99997) 'Total ', reslt(19:20)
99999 FORMAT (1X,I4,2F15.4)
99998 FORMAT (1X,A,F8.4)
99997 FORMAT (1X,A,F14.3,F8.0,2F14.3)
END PROGRAM g02cafe
