単線形回帰

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

Keyword: 単線形回帰

概要

本サンプルは単線形回帰を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される観測値について単線形回帰分析を行い、回帰定数、回帰係数、回帰の残差の平方和や自由度を出力します。

単線形回帰のデータ 

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

入力データ

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

このデータをダウンロード
nag_simple_linear_regression (g02cac) Example Program Data
Nag_AboutMean Nag_TRUE
8
1.0  20.0  1.0
0.0  15.5  1.0
4.0  28.3  1.0
7.5  45.0  1.0
2.5  24.5  1.0
0.0  10.0  1.0
10.0 99.0  1.0
5.0  31.2  1.0

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に回帰に定数項を含むかどうかを示すパラメータ(mean)と重みづけをするかどうかを示すパラメータ(weight)を指定しています。"Nag_AboutMean" は回帰定数があることを意味し、"Nag_TRUE" は重みづけをすることを意味しています。
  • 3行目に観測値の数(n)を指定しています。
  • 4から11行目には縦1列めに独立変数の値(x)、縦2列めに従属変数の値(y)、縦3列目に重み(wt)を指定しています。

出力結果

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

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

Regression constant a = 7.5982

Standard error of the regression constant a = 6.6858

Regression coefficient b = 7.0905

Standard error of the regression coefficient b = 1.3224

The regression coefficient of determination = 0.8273

The sum of squares of the residuals about the regression = 965.2454

Number of degrees of freedom about the regression = 6.0000


  • 3行目に回帰定数が出力されています。
  • 5行目に回帰定数の誤差が出力されています。
  • 7行目に回帰係数が出力されています。
  • 9行目に回帰係数の誤差が出力されています。
  • 11行目に回帰決定係数が出力されています。
  • 13行目に回帰の残差の平方和が出力されています。
  • 15行目に回帰の自由度が出力されています。

ソースコード

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

※本サンプルソースコードは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

このソースコードをダウンロード
/* nag_simple_linear_regression (g02cac) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagg02.h>

int main(void)
{
  Integer exit_status = 0, i, n;
  Nag_SumSquare mean;
  Nag_Boolean weight;
  char nag_enum_arg[40];
  double a, b, df, err_a, err_b, rsq, rss;
  double *wt = 0, *wtptr, *x = 0, *y = 0;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_simple_linear_regression (g02cac) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf(" %39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts nAG enum member name to value
   */
  mean = (Nag_SumSquare) nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s", nag_enum_arg);
  weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);
  scanf("%ld", &n);
  if (n >= (mean == Nag_AboutMean ? 2 : 1)) {
    if (!(x = nAG_ALLOC(n, double)) ||
        !(y = nAG_ALLOC(n, double)) || !(wt = nAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }

  if (weight) {
    wtptr = wt;
    for (i = 0; i < n; ++i)
      scanf("%lf%lf%lf", &x[i], &y[i], &wt[i]);
  }
  else {
    wtptr = (double *) 0;
    for (i = 0; i < n; ++i)
      scanf("%lf%lf", &x[i], &y[i]);
  }

  /* nag_simple_linear_regression (g02cac).
   * Simple linear regression with or without a constant term,
   * data may be weighted
   */
  nag_simple_linear_regression(mean, n, x, y, wtptr, &a, &b, &err_a, &err_b,
                               &rsq, &rss, &df, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_simple_linear_regression (g02cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  if (mean == Nag_AboutMean) {
    printf("\nRegression constant a = %6.4f\n\n", a);
    printf("Standard error of the regression constant a = %6.4f\n\n", err_a);
  }

  printf("Regression coefficient b = %6.4f\n\n", b);
  printf("Standard error of the regression coefficient b = %6.4f\n\n", err_b);

  printf("The regression coefficient of determination = %6.4f\n\n", rsq);
  printf("The sum of squares of the residuals about the "
         "regression = %6.4f\n\n", rss);
  printf("Number of degrees of freedom about the "
         "regression = %6.4f\n\n", df);

END:
  nAG_FREE(x);
  nAG_FREE(y);
  nAG_FREE(wt);

  return exit_status;
}


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