順位を使った回帰

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

Keyword: 順位, 回帰, regression, rank

概要

本サンプルは順位を使った回帰(Regression using ranks) を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される標本と説明変数行列を分析対象とします。このサンプルでは回帰分析を行い、スコア統計量、スコア統計量の共分散行列、パラメータ推定、パラメータ推定の共分散行列、カイ二乗統計量、パラメータ推定の標準誤差やZ統計量を算出します。

順位を使った回帰のデータ 

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

入力データ

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

このデータをダウンロード
nag_rank_regsn (g08rac) Example Program Data
 1 2 2 0.00001
 20
 1.0 1.0 23.0
 1.0 1.0 32.0
 3.0 1.0 37.0
 4.0 1.0 41.0
 2.0 1.0 41.0
 4.0 1.0 48.0
 1.0 1.0 48.0
 5.0 1.0 55.0
 4.0 1.0 55.0
 4.0 0.0 56.0
 4.0 1.0 57.0
 4.0 1.0 57.0
 4.0 1.0 57.0
 1.0 0.0 58.0
 4.0 1.0 59.0
 5.0 0.0 59.0
 5.0 0.0 60.0
 4.0 1.0 61.0
 4.0 1.0 62.0
 3.0 1.0 62.0

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に標本の数(ns)、フィッティングされるパラメータ数(p)、誤差分布のパラメータ(idist)、そして観測値の同順位(タイ)についての許容基準(tol)を指定しています。
  • 3行目に標本の観測値の数(nv)を指定しています。
  • 4~23行目に縦に標本の観測値(y)と説明変数行列(x)を指定しています。

出力結果

(本関数の詳細はnag_rank_regsn のマニュアルページを参照)
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_rank_regsn (g08rac) Example Program Results

Number of samples = 1
Number of parameters fitted = 2
Distribution = 2
Tolerance for ties = 0.00001

Score statistic
   -1.048    64.333

Covariance matrix of score statistic
    0.673
   -4.159   533.670

Parameter estimates
   -0.852     0.114

Covariance matrix of parameter estimates
     1.560
     0.012     0.002

Chi-squared statistic =    8.221 with 2 d.f.

Standard errors of estimates and
approximate z-statistics
    1.249        -0.682
    0.044         2.567


  • 3行目には標本の数が出力されています。
  • 4行目にはフィッティングされるパラメータ数が出力されています。
  • 5行目には誤差分布の種類(2:ロジスティック分布)が出力されています。
  • 6行目には観測値の同順位の許容基準が出力されています。
  • 9行目にはスコア統計量が出力されています。
  • 12~13行目にはスコア統計量の共分散行列が出力されています。
  • 16行目にはパラメータ推定が出力されています。
  • 19~20行目にはパラメータ推定の共分散行列が出力されています。
  • 22行目には自由度2のカイ二乗統計量が出力されています。
  • 26~27行目には推定値の標準誤差と近似Z統計量が出力されています。

ソースコード

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

※本サンプルソースコードは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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

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

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

int main(void)
{

  /* Scalars */
  double tol;
  Integer exit_status, i, idist, p, j, nmax, ns, nsum;
  Integer pdx, pdparvar;
  NagError fail;
  Nag_OrderType order;

  /* Arrays */
  double *eta = 0, *parest = 0, *parvar = 0, *vapvec = 0, *x = 0;
  double *y = 0, *zin = 0;
  Integer *irank = 0, *nv = 0;

#ifdef nAG_COLUMN_MAJOR
#define X(I, J)      x[(J-1)*pdx + I - 1]
#define PARVAR(I, J) parvar[(J-1)*pdparvar + I - 1]
  order = Nag_ColMajor;
#else
#define X(I, J)      x[(I-1)*pdx + J - 1]
#define PARVAR(I, J) parvar[(I-1)*pdparvar + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_rank_regsn (g08rac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read number of samples, number of parameters to be fitted,
   * error distribution parameter and tolerance criterion for ties.
   */
  scanf("%ld%ld%ld%lf%*[^\n] ", &ns, &p, &idist,
        &tol);

  /* Allocate memory to nv only */
  if (!(nv = nAG_ALLOC(ns, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  printf("\n");
  printf("Number of samples =%2ld\n", ns);
  printf("Number of parameters fitted =%2ld\n", p);
  printf("Distribution =%2ld\n", idist);
  printf("Tolerance for ties =%8.5f\n", tol);

  /* Read the number of observations in each sample. */

  for (i = 1; i <= ns; ++i)
    scanf("%ld", &nv[i - 1]);
  scanf("%*[^\n] ");

  nmax = 0;
  nsum = 0;
  for (i = 1; i <= ns; ++i) {
    nsum += nv[i - 1];
    nmax = MAX(nmax, nv[i - 1]);
  }
  if (nmax > 0 && nmax <= 100 && nsum > 0 && nsum <= 100) {
    /* Allocate memory */
    if (!(eta = nAG_ALLOC(nmax, double)) ||
        !(parest = nAG_ALLOC(4 * p + 1, double)) ||
        !(parvar = nAG_ALLOC((p + 1) * p, double)) ||
        !(vapvec = nAG_ALLOC(nmax * (nmax + 1) / 2, double)) ||
        !(x = nAG_ALLOC(nsum * p, double)) ||
        !(y = nAG_ALLOC(nsum, double)) ||
        !(zin = nAG_ALLOC(nmax, double)) ||
        !(irank = nAG_ALLOC(nmax, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
#ifdef nAG_COLUMN_MAJOR
    pdx = nsum;
    pdparvar = p + 1;
#else
    pdx = p;
    pdparvar = p;
#endif

    /* Read in observations and design matrices for each sample. */
    for (i = 1; i <= nsum; ++i) {
      scanf("%lf", &y[i - 1]);
      for (j = 1; j <= p; ++j)
        scanf("%lf", &X(i, j));
    }
    scanf("%*[^\n] ");

    /* nag_rank_regsn (g08rac).
     * Regression using ranks, uncensored data
     */
    nag_rank_regsn(order, ns, nv, y, p, x, pdx, idist, nmax, tol,
                   parvar, pdparvar, irank, zin, eta, vapvec, parest, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_rank_regsn (g08rac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\n");
    printf("Score statistic\n");
    for (i = 1; i <= p; ++i)
      printf("%9.3f%s", parest[i - 1], i % 2 == 0 || i == p ? "\n" : " ");
    printf("\n");

    printf("Covariance matrix of score statistic\n");
    for (j = 1; j <= p; ++j) {
      for (i = 1; i <= j; ++i)
        printf("%9.3f%s", PARVAR(i, j), i % 2 == 0 || i == j ? "\n" : " ");
    }
    printf("\n");

    printf("Parameter estimates\n");
    for (i = 1; i <= p; ++i)
      printf("%9.3f%s", parest[p + i - 1], i % 2 == 0 || i == p ? "\n" : " ");
    printf("\n");

    printf("Covariance matrix of parameter estimates\n");
    for (i = 1; i <= p; ++i)
    {
      printf(" ");

      for (j = 1; j <= i; ++j)
        printf("%9.3f%s", PARVAR(i + 1, j),
               j % 2 == 0 || j == i ? "\n" : " ");
    }
    printf("\n");

    printf("Chi-squared statistic =%9.3f with%2ld d.f.\n",
           parest[p * 2], p);
    printf("\n");
    printf("Standard errors of estimates and\n");
    printf("approximate z-statistics\n");
    for (i = 1; i <= p; ++i)
      printf("%9.3f%14.3f\n", parest[2 * p + 1 + i - 1],
             parest[p * 3 + 1 + i - 1]);
    printf("\n");
  }
END:
  nAG_FREE(eta);
  nAG_FREE(parest);
  nAG_FREE(parvar);
  nAG_FREE(vapvec);
  nAG_FREE(x);
  nAG_FREE(y);
  nAG_FREE(zin);
  nAG_FREE(irank);
  nAG_FREE(nv);

  return exit_status;
}


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