順位を使った回帰(右打ち切りデータ)

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

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 順位を使った回帰(右打ち切りデータ) (C言語/C++)

Keyword: 順位, 回帰, regression, rank, right-censored

概要

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

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

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

入力データ

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

このデータをダウンロード
nag_rank_regsn_censored (g08rbc) Example Program Data
 1 1 0.00001 0.00001
 40
 143.0 0.0 0 164.0 0.0 0 188.0 0.0 0 188.0 0.0 0 190.0 0.0 0
 192.0 0.0 0 206.0 0.0 0 209.0 0.0 0 213.0 0.0 0 216.0 0.0 0
 220.0 0.0 0 227.0 0.0 0 230.0 0.0 0 234.0 0.0 0 246.0 0.0 0
 265.0 0.0 0 304.0 0.0 0 216.0 0.0 1 244.0 0.0 1 142.0 1.0 0
 156.0 1.0 0 163.0 1.0 0 198.0 1.0 0 205.0 1.0 0 232.0 1.0 0
 232.0 1.0 0 233.0 1.0 0 233.0 1.0 0 233.0 1.0 0 233.0 1.0 0
 239.0 1.0 0 240.0 1.0 0 261.0 1.0 0 280.0 1.0 0 280.0 1.0 0
 296.0 1.0 0 296.0 1.0 0 323.0 1.0 0 204.0 1.0 1 344.0 1.0 1

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に標本の数(ns)、フィッティングされるパラメータ数(p)、ロジスティク分布のべき乗パラメータ(gamma)、そして観測値の同順位(タイ)についての許容基準(tol)を指定しています。
  • 3行目に標本の観測値の数(nv)を指定しています。
  • 4〜11行目に標本の観測値(y)、説明変数(x)と打ち切り変数(icen)を指定しています。

出力結果

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

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

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

Score statistic
    4.584

Covariance matrix of score statistic
    7.653

Parameter estimates
    0.599

Covariance matrix of parameter estimates
    0.131

Chi-squared statistic =    2.746 with 1 d.f.

Standard errors of estimates and
approximate z-statistics
    0.361         1.657

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

ソースコード

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

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法


このソースコードをダウンロード
/* nag_rank_regsn_censored (g08rbc) 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 gamma, tol;
  Integer exit_status, i, p, j, nmax, ns, nsum;
  Integer pdx, pdprvr;
  NagError fail;
  Nag_OrderType order;

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

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

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_rank_regsn_censored (g08rbc) Example Program Results\n");

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

  /* Read number of samples, number of parameters to be fitted, */
  /* distribution power parameter and tolerance criterion for ties. */

  scanf("%ld%ld%lf%lf%*[^\n] ", &ns, &p, &gamma, &tol);
  printf("\n");

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

  printf("Number of samples =%2ld\n", ns);
  printf("Number of parameters fitted =%2ld\n", p);
  printf("Distribution power parameter =%10.5f\n", gamma);

  printf("Tolerance for ties =%10.5f\n", tol);

  printf("\n");
  /* 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]);
  }

  /* Allocate memory */
  if (!(eta = nAG_ALLOC(nmax, double)) ||
      !(parest = nAG_ALLOC(4 * p + 1, double)) ||
      !(prvr = nAG_ALLOC(7 * 6, 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)) ||
      !(icen = nAG_ALLOC(nsum, Integer)) ||
      !(irank = nAG_ALLOC(nmax, Integer)) || !(iwa = nAG_ALLOC(400, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
#ifdef nAG_COLUMN_MAJOR
  pdx = nsum;
  pdprvr = p + 1;
#else
  pdx = p;
  pdprvr = p;
#endif

  /* Read in observations, design matrix and censoring variable */

  for (i = 1; i <= nsum; ++i) {
    scanf("%lf", &y[i - 1]);

    for (j = 1; j <= p; ++j) {
      scanf("%lf", &X(i, j));
    }
    scanf("%ld", &icen[i - 1]);
  }
  scanf("%*[^\n] ");

  /* nag_rank_regsn_censored (g08rbc).
   * Regression using ranks, right-censored data
   */
  nag_rank_regsn_censored(order, ns, nv, y, p, x, pdx, icen, gamma,
                          nmax, tol, prvr, pdprvr, irank, zin, eta, vapvec,
                          parest, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rank_regsn_censored (g08rbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Score statistic\n");

  for (i = 1; i <= p; ++i)
    printf("%9.3f\n", parest[i - 1]);
  printf("\n");

  printf("Covariance matrix of score statistic\n");
  for (j = 1; j <= p; ++j) {
    for (i = 1; i <= j; ++i)
      printf("%9.3f\n", PRVR(i, j));
    printf("\n");
  }

  printf("Parameter estimates\n");
  for (i = 1; i <= p; ++i)
    printf("%9.3f\n", parest[p + i - 1]);
  printf("\n");
  printf("Covariance matrix of parameter estimates\n");
  for (i = 1; i <= p; ++i) {
    for (j = 1; j <= i; ++j)
      printf("%9.3f\n", PRVR(i + 1, j));
    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]);
END:
  nAG_FREE(eta);
  nAG_FREE(parest);
  nAG_FREE(prvr);
  nAG_FREE(vapvec);
  nAG_FREE(x);
  nAG_FREE(y);
  nAG_FREE(zin);
  nAG_FREE(icen);
  nAG_FREE(irank);
  nAG_FREE(iwa);
  nAG_FREE(nv);

  return exit_status;
}


関連情報
Privacy Policy  /  Trademarks