因子得点の計算

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

Keyword: 因子得点, 計算, 多変量解析

概要

本サンプルは因子得点の計算を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される相関行列について因子得点の計算を行います。

因子得点計算のデータ 

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

入力データ

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

このデータをダウンロード
nag_mv_fac_score (g03ccc) Example Program Data
Nag_MatCorr_Covar Nag_FALSE 220 6  6 2
1.000 0.439 0.410 0.288 0.329 0.248
0.439 1.000 0.351 0.354 0.320 0.329
0.410 0.351 1.000 0.164 0.190 0.181
0.288 0.354 0.164 1.000 0.595 0.470
0.329 0.320 0.190 0.595 1.000 0.464
0.248 0.329 0.181 0.470 0.464 1.000
  1    1    1   1    1   1
Nag_FacScoreRegsn

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目には因子得点の計算が実行される行列の種類を示すパラメータ(matrix)、重みづけをするかどうかを示すパラメータ(weright)、相関行列/分散共分散行列の計算に使用される観測値の数(n)、相関/分散共分散行列の変数の数(m)、因子得点の計算の変数の数(nvar)、因子数(nfac)を指定しています。
    この場合、"Nag_MatCorr_Covar"は相関行列/分散共分散行列に対して因子得点の計算が実行されることを意味しています。"Nag_FALSE"は重みづけがされないことを意味しています。
  • 3~8行目に相関行列のデータ(x)を指定しています。
  • 9行目に変数が因子得点の計算に含まれるかどうかを示すフラグ(isx)を指定しています。
  • 10行目に因子得点係数の計算に使用される手法(method)を指定しています。"Nag_FacScoreRegsn" は回帰の手法が使用されることを意味しています。

出力結果

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

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


Parameters to e04lbc
--------------------

Number of variables...........   6

optim_tol...............  1.00e-03    linesearch_tol..........  9.00e-01
step_max................  1.47e+01    max_iter................       500
print_level......... Nag_Soln_Iter    machine precision.......  1.11e-16
deriv_check.............     Nag_FALSE
outfile.................    stdout

Memory allocation:
state...................      User
hesl....................      User    hesd...................       User

Iterations performed = 0,   function evaluations = 1
Criterion =     2.999971e-02

             Variable    Standardized
                         Communalities
                1           0.4168
                2           0.4138
                3           0.3384
                4           0.5164
                5           0.5148
                6           0.4127

Iterations performed = 1,   function evaluations = 2
Criterion =     1.579256e-02

             Variable    Standardized
                         Communalities
                1           0.4929
                2           0.4050
                3           0.3664
                4           0.6586
                5           0.6077
                6           0.3580

Iterations performed = 2,   function evaluations = 3
Criterion =     1.099067e-02

             Variable    Standardized
                         Communalities
                1           0.4896
                2           0.4059
                3           0.3566
                4           0.6277
                5           0.5760
                6           0.3700

Iterations performed = 3,   function evaluations = 4
Criterion =     1.086731e-02

             Variable    Standardized
                         Communalities
                1           0.4898
                2           0.4059
                3           0.3563
                4           0.6228
                5           0.5688
                6           0.3717

Iterations performed = 4,   function evaluations = 5
Criterion =     1.086720e-02

             Variable    Standardized
                         Communalities
                1           0.4898
                2           0.4059
                3           0.3563
                4           0.6226
                5           0.5686
                6           0.3718

Loadings, Communalities and PSI

     0.553    -0.429   0.490   0.510
     0.568    -0.288   0.406   0.594
     0.392    -0.450   0.356   0.644
     0.740     0.273   0.623   0.377
     0.724     0.211   0.569   0.431
     0.595     0.132   0.372   0.628

Factor score coefficients

     0.193    -0.392
     0.170    -0.226
     0.109    -0.326
     0.349     0.337
     0.299     0.229
     0.169     0.098

  • 7行目に変数の数が出力されています。
  • 9~13行目にはオプションパラメータの値が出力されています。
    optim_tol 解を必要とする x の精度。
    linesearch_tol 線形最小化の精度。
    step_max 解と開始点の間のユークリッド距離の推定値。
    max_iter 最大反復数。
    print_level 結果出力のレベル。"Nag_Soln_Iter"は最終解と各反復の結果を出力することを意味します。
    machine precision マシンの精度。
    deriv_check 開始点 x で導関数のチェックがされるかどうかを示しています。"Nag_FALSE"はチェックがされないことを意味します。
    outfile 結果が出力されるファイル名。"stdout"は標準出力を意味します。
  • 15~17行目にはオプションパラメータstate、hesl、hesd がユーザによってメモリの割り当てがされていることを表しています。
  • 19~20行目には実行された反復数、関数評価、基準値が出力されています。
  • 22~29行目には変数と標準化された共通性が出力されています。
  • 31~41行目には1回目の反復後の計算結果が出力されています。
  • 43~53行目には2回目の反復後の計算結果が出力されています。
  • 55~65行目には3回目の反復後の計算結果が出力されています。
  • 67~77行目には4回目の反復後の計算結果が出力されています。
  • 79~86行目には各因子の負荷量、共通性とΨの推定値が出力されています。
  • 88~95行目には因子得点係数が出力されています。

ソースコード

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

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

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

#include <nag.h>
#include <stdio.h>
#include <string.h>
#include <nag_stdlib.h>
#include <nage04.h>
#include <nagg03.h>
#include <math.h>

#define FL(I, J) fl[(I) *tdfl + J]
#define FS(I, J) fs[(I) *tdfs + J]
#define X(I, J)  x[(I) *tdx + J]
int main(void)
{

  Integer exit_status = 0, i, *isx = 0, j, m, n, nfac, nvar, tdfl, tdfs, tdr;
  Integer tdx;
  NagError fail;
  Nag_E04_Opt options;
  Nag_FacMat matrix;
  Nag_FacScoreMethod method;
  Nag_Boolean weight;
  char nag_enum_arg[40];
  double *com = 0, *e = 0, eps, *fl = 0, *fs = 0, *psi = 0, *r = 0;
  double *stat = 0, *wt = 0, *wtptr = 0, *x = 0;

  INIT_FAIL(fail);

  printf("nag_mv_fac_score (g03ccc) Example Program Results\n\n");

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

  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts nAG enum member name to value
   */
  matrix = (Nag_FacMat) 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);
  scanf("%ld", &m);
  scanf("%ld", &nvar);
  scanf("%ld", &nfac);

  if (nvar >= 2 && m >= nvar && n > nvar && nvar >= nfac) {
    if (!(com = nAG_ALLOC(nvar, double)) ||
        !(e = nAG_ALLOC(nvar, double)) ||
        !(fl = nAG_ALLOC(nvar * nfac, double)) ||
        !(fs = nAG_ALLOC(nvar * nfac, double)) ||
        !(psi = nAG_ALLOC(nvar, double)) ||
        !(r = nAG_ALLOC(m * m, double)) ||
        !(stat = nAG_ALLOC(4, double)) ||
        !(wt = nAG_ALLOC(n, double)) ||
        !(x = nAG_ALLOC((matrix == Nag_MatCorr_Covar ? m : n) * m, double)) ||
        !(isx = nAG_ALLOC(m, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    tdfl = nfac;
    tdfs = nfac;
    tdr = m;
    tdx = m;
  }
  else {
    printf("Invalid nvar or m or n.\n");
    exit_status = 1;
    return exit_status;
  }
  if (matrix == Nag_MatCorr_Covar) {
    for (i = 0; i < m; ++i) {
      for (j = 0; j < m; ++j)
        scanf("%lf", &X(i, j));
    }
  }
  else {
    if (weight) {
      for (i = 0; i < n; ++i) {
        for (j = 0; j < m; ++j)
          scanf("%lf", &X(i, j));
        scanf("%lf", &wt[i]);
      }
      wtptr = wt;
    }
    else {
      for (i = 0; i < n; ++i) {
        for (j = 0; j < m; ++j)
          scanf("%lf", &X(i, j));
      }
    }
  }
  for (j = 0; j < m; ++j)
    scanf("%ld", &isx[j]);

  /* nag_opt_init (e04xxc).
   * Initialization function for option setting
   */
  nag_opt_init(&options);
  options.max_iter = 500;
  options.optim_tol = 1e-3;
  eps = 1e-5;
  /* nag_mv_factor (g03cac).
   * Maximum likelihood estimates of parameters
   */
  fflush(stdout);
  nag_mv_factor(matrix, n, m, x, tdx, nvar, isx, nfac, wtptr, e,
                stat, com, psi, r, fl, tdfl, &options, eps, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_mv_factor (g03cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\nLoadings, Communalities and PSI\n\n");
  for (i = 0; i < nvar; ++i) {
    for (j = 0; j < nfac; ++j)
      printf("  %8.3f", FL(i, j));
    printf("%8.3f%8.3f\n", com[i], psi[i]);
  }
  scanf("%39s", nag_enum_arg);
  method = (Nag_FacScoreMethod) nag_enum_name_to_value(nag_enum_arg);

  /* nag_mv_fac_score (g03ccc).
   * Factor score coefficients, following nag_mv_factor
   * (g03cac)
   */
  nag_mv_fac_score(method, Nag_FacNoRotate, nvar, nfac, fl, tdfl, psi, e,
                   r, tdr, fs, tdfs, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_mv_fac_score (g03ccc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\nFactor score coefficients\n\n");
  for (i = 0; i < nvar; ++i) {
    for (j = 0; j < nfac; ++j)
      printf("  %8.3f", FS(i, j));
    printf("\n");
  }

END:
  nAG_FREE(com);
  nAG_FREE(e);
  nAG_FREE(fl);
  nAG_FREE(fs);
  nAG_FREE(psi);
  nAG_FREE(r);
  nAG_FREE(stat);
  nAG_FREE(wt);
  nAG_FREE(x);
  nAG_FREE(isx);
  return exit_status;
}


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