Keyword: パラメータ推定, PLS, 予測
概要
本サンプルはパラメータ推定に基づくPLS予測を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについてPLS予測を行います。
※本サンプルはnAG Cライブラリに含まれる関数 nag_pls_orth_scores_pred() のExampleコードです。本サンプル及び関数の詳細情報は nag_pls_orth_scores_pred のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_pls_orth_scores_pred のマニュアルページを参照)このデータをダウンロード |
nag_pls_orth_scores_pred (g02ldc) Example Program Data 15 1 Nag_EstimatesStand Nag_PredStdScale 15 15 : ip, my, orig, iscale, n, mz -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 1.9607 -1.6324 0.5746 1.9607 -1.6324 0.5740 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 1.9607 -1.6324 0.5746 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 0.0744 -1.7333 0.0902 1.9607 -1.6324 0.5746 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 2.8369 1.4092 -3.1398 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 -4.7548 3.6521 0.8524 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 -1.2201 0.8829 2.2253 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 2.4064 1.7438 1.1057 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 2.2261 -5.3648 0.3049 3.0777 0.3891 -0.0701 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -4.1921 -1.0285 -0.9801 3.0777 0.3891 -0.0701 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -4.9217 1.2977 0.4473 3.0777 0.3891 -0.0701 0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 2.2261 -5.3648 0.3049 2.2261 -5.3648 0.3049 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 -4.9217 1.2977 0.4473 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701 -4.1921 -1.0285 -0.9801 0.0744 -1.7333 0.0902 2.8369 1.4092 -3.1398 : z 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 : Elements of isz -0.1383 0.0572 -0.1906 0.1238 0.0591 0.0936 -0.2842 0.4713 0.2661 -0.0914 0.1226 -0.0488 0.0332 0.0332 -0.0332 : b -2.6137 -2.3614 -1.0449 2.8614 0.3156 -0.2641 -0.3146 -1.1221 0.2401 0.4694 -1.9619 0.1691 2.5664 1.3741 -2.7821 : xbar 0.4520 : ybar 1.4956 1.3233 0.5829 0.7735 0.6247 0.7966 2.4113 2.0421 0.4678 0.8197 0.9420 0.1735 1.0475 0.1359 1.3853 : xstd 0.9062 : ystd
- 1行目はタイトル行で読み飛ばされます。
- 2行目は予測変数の数(ip)、応答変数の数(my)、どのようにパラメータ推定が行われるか(orig)、どのように予測変数がスケーリングされるか(iscale)、テストデータの観測値の数(n)、テストデータで有効な予測変数の数(mz)を指定しています。 "Nag_EstimatesStand"は中心的スケーリング可能(centred and possibly scaled)データに対しパラメータ推定が行われることを意味します。 "Nag_PredStdScale"はデータが変数の標準分散によってスケーリングされることを意味します。
- 3〜47行目は予測変数の観測値(z)を指定しています。
- 48行目はどの予測変数がモデルに含まれるか(isz)を指定しています。
- 49〜51行目はパラメータ推定値(b)を指定しています。
- 52〜54行目は予測変数の平均値(xbar)を指定しています。
- 55行目は応答変数の平均値(ybar)を指定しています。
- 56〜58行目は予測変数のスケーリング(xstd)を指定しています。
- 59行目は応答変数のスケーリング(ystd)を指定しています。
出力結果
(本関数の詳細はnag_pls_orth_scores_pred のマニュアルページを参照)この出力例をダウンロード |
nag_pls_orth_scores_pred (g02ldc) Example Program Results YHAT 1 1 0.2132 2 0.5152 3 0.1437 4 0.4459 5 0.1716 6 2.4809 7 0.0964 8 1.4475 9 -0.1546 10 -0.5492 11 0.5393 12 0.2686 13 -1.1332 14 1.7975 15 0.4973
- 4〜19行目に応答変数の予測値が出力されています。
ソースコード
(本関数の詳細はnag_pls_orth_scores_pred のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
このソースコードをダウンロード |
/* nag_pls_orth_scores_pred (g02ldc) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ /* Pre-processor includes */ #include <stdio.h> #include <math.h> #include <nag.h> #include <nag_stdlib.h> #include <nagg02.h> #include <nagx04.h> int main(void) { /*Integer scalar and array declarations */ Integer exit_status = 0; Integer i, ip, j, l, my, mz, n; Integer pdb, pdyhat, pdz; Integer *isz = 0; /*Double scalar and array declarations */ double *b = 0, *xbar = 0, *xstd = 0, *ybar = 0, *yhat = 0; double *ystd = 0, *z = 0; /*Character scalar and array declarations */ char siscale[40], sorig[40]; /*nAG Types */ Nag_OrderType order; Nag_ScalePredictor iscale; Nag_EstimatesOption orig; NagError fail; INIT_FAIL(fail); printf("nag_pls_orth_scores_pred (g02ldc) Example Program Results\n"); /* Skip header in data file. */ scanf("%*[^\n] "); /* Read data values. */ scanf("%ld%ld%39s %39s %ld%ld" "%*[^\n] ", &ip, &my, sorig, siscale, &n, &mz); orig = (Nag_EstimatesOption) nag_enum_name_to_value(sorig); iscale = (Nag_ScalePredictor) nag_enum_name_to_value(siscale); #ifdef nAG_COLUMN_MAJOR pdb = ((orig == Nag_EstimatesStand) ? ip : 1 + ip); #define B(I, J) b[(J-1)*pdb + I-1] pdyhat = n; pdz = n; #define Z(I, J) z[(J-1)*pdz + I-1] order = Nag_ColMajor; #else pdb = my; #define B(I, J) b[(I-1)*pdb + J-1] pdyhat = my; pdz = mz; #define Z(I, J) z[(I-1)*pdz + J-1] order = Nag_RowMajor; #endif if (!(b = nAG_ALLOC(pdb * (order == Nag_RowMajor ? (1 + ip) : my), double)) || !(xbar = nAG_ALLOC(ip, double)) || !(xstd = nAG_ALLOC(ip, double)) || !(ybar = nAG_ALLOC(my, double)) || !(yhat = nAG_ALLOC(pdyhat * (order == Nag_RowMajor ? n : my), double)) || !(ystd = nAG_ALLOC(my, double)) || !(z = nAG_ALLOC(pdz * (order == Nag_RowMajor ? n : mz), double)) || !(isz = nAG_ALLOC(mz, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read prediction x-data */ for (i = 1; i <= n; i++) { for (j = 1; j <= mz; j++) scanf("%lf ", &Z(i, j)); } scanf("%*[^\n] "); /* Read elements of isz */ for (j = 0; j < mz; j++) scanf("%ld ", &isz[j]); scanf("%*[^\n] "); /* Read parameter estimates */ l = ip; if (orig != Nag_EstimatesStand) { l = l + 1; } for (j = 1; j <= l; j++) { for (i = 1; i <= my; i++) scanf("%lf ", &B(j, i)); } scanf("%*[^\n] "); /* Read means */ if (orig == Nag_EstimatesStand) { for (j = 0; j < ip; j++) scanf("%lf ", &xbar[j]); scanf("%*[^\n] "); for (l = 0; l < my; l++) scanf("%lf ", &ybar[l]); scanf("%*[^\n] "); } /* Read scalings */ if ((orig == Nag_EstimatesStand) && (iscale != Nag_PredNoScale)) { for (j = 0; j < ip; j++) scanf("%lf ", &xstd[j]); scanf("%*[^\n] "); for (l = 0; l < my; l++) scanf("%lf ", &ystd[l]); scanf("%*[^\n] "); } /* Calculate predictions */ /* * nag_pls_orth_scores_pred (g02ldc) * Partial least squares */ nag_pls_orth_scores_pred(order, ip, my, orig, xbar, ybar, iscale, xstd, ystd, b, pdb, n, mz, isz, z, pdz, yhat, pdyhat, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_pls_orth_scores_pred (g02ldc).\n%s\n", fail.message); exit_status = 1; goto END; } /* * nag_gen_real_mat_print (x04cac) * Print real general matrix (easy-to-use) */ fflush(stdout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, my, yhat, pdyhat, "YHAT", 0, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } END: nAG_FREE(b); nAG_FREE(xbar); nAG_FREE(xstd); nAG_FREE(ybar); nAG_FREE(yhat); nAG_FREE(ystd); nAG_FREE(z); nAG_FREE(isz); return exit_status; }