📄 ocn_g02.h
字号:
/* <OCN_g02.h>
*
* Copyright 1996 Numerical Algorithms Group
*
* Include file for NAG C Library g02 Chapter
*
* Mark 4, revised, 1996.
* Mark 5 revised. IER-2156 (Feb 1998).
* Mark 6 revised. IER-3041 (July 2000).
*/
#ifndef NAGG02
#define NAGG02
//#importdll "ONAG" // NAG DLL prepared by OriginLab
#pragma dll(ONAG)
#include <NAG\nag_types.h>
/* begin proto */
/** g02brc
calculates Kendall and Spearman rank correlation coefficients.
Example1:
Assume we have two Matrix, "Matrix1" and "Matrix2", they are all 20X20.
"Matrix1" first three columns and first 7 rows contains totally 21 data.
We will put the result to "Matrix2".
int m = 3, n = 7, tdx = 20, tdc = 20;
matrix x, corr;
corr.SetSize(20,20);
int *svarptr;
int *sobsptr;
int success;
int svar[20] = {1, 1, 1};
svarptr = svar;
int sobs[20] = {1, 1, 1, 1, 1, 1, 1};
sobsptr = sobs;
Matrix xx("Matrix1");
x = xx;
success = nag_ken_spe_corr_coeff(n, m, x, tdx, svarptr, sobsptr, corr, tdc);
//put the result to the Matrix2.
Matrix mcorr("Matrix2");
mcorr = corr;
Example2:
A program to calculate the Kendall and Spearman rank correlation coefficients from a set of data.
The data consists of 7 observations for each of 3 variables, and there is no weight.
void test_nag_ken_spe_corr_coeff()
{
int i, j, m, n, tdx, tdc;
matrix x, corr;
x.SetSize(20,20);
corr.SetSize(20,20);
int *svarptr;
int *sobsptr;
int success;
char s, w;
tdx = 20;
tdc = 20;
m = 3;
n = 7;
x[0][0] = 1.0;
x[0][1] = 2.0;
x[0][2] = 4.0;
x[1][0] = 7.0;
x[1][1] = 7.0;
x[1][2] = 3.0;
x[2][0] = 2.0;
x[2][1] = 3.0;
x[2][2] = 4.0;
x[3][0] = 4.0;
x[3][1] = 4.0;
x[3][2] = 5.0;
x[4][0] = 5.0;
x[4][1] = 6.0;
x[4][2] = 7.0;
x[5][0] = 3.0;
x[5][1] = 1.0;
x[5][2] = 3.0;
x[6][0] = 6.0;
x[6][1] = 5.0;
x[6][2] = 5.0;
int svar[20] = {1, 1, 1};
svarptr = svar;
int sobs[20] = {1, 1, 1, 1, 1, 1, 1};
sobsptr = sobs;
printf("The input data are as follows\n");
printf("m = 3, n = 7, and all variables and observations are included\n");
printf("x\n");
for(i = 0; i < 7; i++)
{
for(j = 0; j < 3; j++)
printf("%2.1f, ",x[i][j]);
printf("\n");
}
success = nag_ken_spe_corr_coeff(n, m, x, tdx, svarptr, sobsptr, corr, tdc);
if(success == 0)
{
printf("\nCorrelation coefficients:\n\n");
for (i=0; i<m; i++)
{
for (j=0; j<m; j++)
printf("%8.5f ", corr[i][j]);
printf("\n");
}
}
else
{
printf("The function g02brc has some problem\n");
}
}
The output is as follows:
The input data are as follows
m = 3, n = 7, and all variables and observations are included
x
1.0, 2.0, 4.0,
7.0, 7.0, 3.0,
2.0, 3.0, 4.0,
4.0, 4.0, 5.0,
5.0, 6.0, 7.0,
3.0, 1.0, 3.0,
6.0, 5.0, 5.0,
Correlation coefficients:
1.00000 0.85714 0.12849
0.71429 1.00000 0.33040
0.10287 0.41148 1.00000
Return:
The function returns NAG error code or 0 if no error.
11: On entry, n must not be less than 2: n = _value_. On entry, m must not be less than 2: m = _value_.
17: On entry tdx = _value_ while m = _value_. These parameters must satisfy tdx = m. On entry tdc = _value_ while m = _value_. These parameters must satisfy tdc = m.
53: Value _value_ given to svar[_value_] not valid. Correct range for elements of svar is svar[i] = 0. Value _value_ given to sobs[_value_] not valid. Correct range for elements of sobs is sobs[i] = 0.
450: Too fewobserv ations have been selected. On entry, sobs must contain at least 2 positive elements.
451: No variables have been selected. On entry, svar must contain at least 1 positive element.
73: Memory allocation failed.
74: An initial error has occured in this function. Check the function call and any array sizes.
successful call of the nag_ken_spe_corr_coe function
*/
int nag_ken_spe_corr_coeff(
int n, // the number of observations
int m, // the number of variables
double x[], // the variable of each observation contained the ith observation on the jth variable
int tdx, // the second dimension of the array x.
int svar[], // indicates which variables are to be included.
int sobs[], // indicates which observations are to be included.
double corr[], // return Spearman and Kendall rank correlation.
int tdc // the second dimension of the array corr as declared in the function from which nag_ken_spe_corr_coe. is called.
); // Spearman and Kendall rank correlation coefficients.
/** g02bxc
calculates the Pearson product-moment correlation coefficients and the
variance-covariance matrix for a set of data. Weights may be used.
Example:
A program to calculate the means, standard deviations, variance-covariance matrix and a matrix
of Pearson product-moment correlation coefficients for a set of 3 observations of 3 variables.
void test_nag_corr_cov()
{
matrix r, v;
r.SetSize(5,5);
v.SetSize(5,5);
double *wtptr;
double sw, wmean[5], std[5];
int i, j, n, m;
char w;
int tdx, tdr, tdv;
int test;
int success;
tdx = 5;
tdr = 5;
tdv = 5;
test = 0;
m = 3;
n = 3;
w = 'w';
double wt[5] = {9.1231, 3.7011, 4.5230};
matrix x = { {0.9310, 0.0900, 0.8870,0,0}, {0.0009, 0.0099, 0.0999,0,0},
{0.1300, 1.3070, 0.3700,0,0}, {0, 0, 0, 0, 0} , {0, 0, 0, 0, 0} };
if (w == 'w')
wtptr = wt;
else
wtptr = NULL;
success = nag_corr_cov(n, m, x, tdx, NULL, wtptr, &sw, wmean, std, r, tdr, v, tdv);
if(success == 0)
{
if (wtptr)
printf("\nCase %ld --- Using weights\n", ++test);
else
printf("\nCase %ld --- Not using weights\n", ++test);
printf ("\nInput data\t\t weight\n");
for(i=0; i<n; i++)
printf("%6.1f%6.1f%6.1f%6.1f\n",x[i][0],x[i][1],x[i][2],wt[i]);
printf("\n");
printf("Sample means\n");
for(i=0; i<m; i++)
printf("%6.1f\n",wmean[i]);
printf("\nStandard deviation\n");
for(i=0; i<m; i++)
printf("%6.1f\n",std[i]);
printf("\nCorrelation matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<m; j++)
printf(" %7.4f ",r[i][j]);
printf("\n");
}
printf("\nVariance matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<m; j++)
printf(" %7.3f ",v[i][j]);
printf("\n");
}
printf("\nSum of weights %6.1f\n", sw);
ASSERT( is_equal( round(sw,1), 17.3 ) );
}
else
{
printf("There is some problem with function nag_corr_cov\n");
}
}
The output is as follows:
Case 0 --- Using weights
Input data weight
0.9 0.1 0.9 9.1
0.0 0.0 0.1 3.7
0.1 1.3 0.4 4.5
Sample means
0.5
0.4
0.6
Standard deviation
0.4
0.6
0.3
Correlation matrix
1.0000 -0.4932 0.9839
-0.4932 1.0000 -0.3298
0.9839 -0.3298 1.0000
Variance matrix
0.197 -0.123 0.149
-0.123 0.316 -0.063
0.149 -0.063 0.117
Sum of weights 17.3
Return:
The function returns NAG error code or 0 if no error.
12: On entry, n must be greater than 1: n = _value_.
11: On entry, m must not be less than 1: m = _value_.
17: On entry, tdx = _value_ while m = _value_. These parameters must satisfy tdx = m. On entry, tdr = _value_ while m = _value_. The parameters must satisfy tdr = m. On entry, tdv = _value_ while m = _value_. These parameters must satisfy tdv = m.
399: On entry, at least one of the weights is negative.
444: On entry, at least one element of sx is negative.
445: On entry, no element of sx is positive.
442: On entry, the sum of weights is less than 1.0.
443: A variable has zero variance. At least one variable has zero variance. In this case v and std are as calculated, but r will contain zero for any correlation involving a variable with zero variance.
73: Memory allocation failed.
successful call of the nag_corr_cov function
*/
int nag_corr_cov(
int n, // number of observervations
int m, // number of variables
double x[], // the variable of each observation contained the ith observation on the jth variable
int tdx, // the second dimension of the array x.
int sx[], // indicates which p variables to include in the analysis
double wt[], // the (optional) frequency weighting for each observation.
double *sw, // the sum of weights
double wmean[], // the sample means
double std[], // the standard deviations
double r[], // the matrix of Pearson product-moment correlation coefficients.
int tdr, // the second dimension of the array r.
double v[], // the variance-covariance matrix.
int tdv // the second dimension of the array v.
); // sum of weights, sample means, standard deviation, Pearson correlation coefficients variance-covariance matrix.
/** g02byc
computes a partial correlation/variance-vovariance matrix from a correlation or
variance-covariance matrix computed by nag_corr-cov.
Example:
Data, given by Osborn (1979), on the number of deaths, smoke (mg/m^3) and sulphur
dioxide (parts/million) during an intense period of fog is input. The correlations
are computed using nag_corr_cov (g02bxc) and the partial correlation between deaths
and smoke given sulphur dioxide is computed using nag_partial_corr.
void test_nag_partial_corr()
{
double r[9];
double std[3];
double v[9];
double xbar[3];
double sw;
int success;
int j, k, i, m, n, nx, ny;
n = 15;
m =3;
double x[45] = {112, 0.30, 0.09, 140, 0.49, 0.16, 143, 0.61, 0.22,
120, 0.49, 0.14, 196, 2.64, 0.75, 294, 3.45, 0.86,
513 4.46, 1.34, 518, 4.46, 1.34, 430, 1.22, 0.47,
274, 1.22, 0.47, 255, 0.32, 0.22, 236, 0.29, 0.23,
256, 0.50, 0.26, 222, 0.32, 0.16, 213, 0.32, 0.16};
nx = 1;
ny = 2;
int sz[3] = {-1, -1, 1};
printf("The input data are as follows\n");
printf("n = 15, n = 3, nx = 1, ny = 2\n");
printf("x\n");
for(i = 0; i < 45; i++)
{
printf("%3.2f, ",x[i]);
if((i + 1) % 3 == 0)
printf("\n");
}
printf("\nsz\n");
for(i = 0; i < 3; i++)
printf("%d, ", sz[i]);
printf("\n");
printf("\nThe results are as follows\n");
success = nag_corr_cov(n, m, x, m, NULL, NULL, &sw, xbar, std, r, m, v, m);
if(success == 0)
{
printf("\nCorreelation matrix\n\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
if(i > j)
printf(" ");
else
printf("%7.4f ",r[i * m + j]);
printf("\n");
}
success = nag_partial_corr(m, ny, nx, sz, v, m, r, m);
if(success == 0)
{
printf("\n");
printf("\nPartial Correlation matrix\n\n");
for(i = 0; i < ny; i++)
{
for(j = 0; j < ny; j++)
{
if(i > j)
printf(" ");
else if( i == j)
printf("%7.4f ",1.0);
else
printf("%7.4f ",r[i * m + j]);
}
printf("\n");
}
}
else
{
printf("The function call of nag_partial_corr has some problem\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -