📄 ocn_g02.h
字号:
else
{
printf("The function nag_corr_cov has some problem\n");
}
}
The output is as follows:
The input data are as follows
n = 15, n = 3, nx = 1, ny = 2
x
112.00, 0.30, 0.09,
140.00, 0.49, 0.16,
143.00, 0.61, 0.22,
120.00, 0.49, 0.14,
196.00, 2.64, 0.75,
294.00, 3.45, 0.86,
5134.46, 1.34, 518.00,
4.46, 1.34, 430.00,
1.22, 0.47, 274.00,
1.22, 0.47, 255.00,
0.32, 0.22, 236.00,
0.29, 0.23, 256.00,
0.50, 0.26, 222.00,
0.32, 0.16, 213.00,
0.32, 0.16, 0.00,
sz
-1, -1, 1,
The results are as follows
Correelation matrix
1.0000 0.1943 0.5257
1.0000 -0.1255
1.0000
Partial Correlation matrix
1.0000 0.3084
1.0000
Return:
The function returns NAG error or 0 if no error.
11: On entry, m must not be less than 3: m = <value>. On entry, ny must not be less than 2: ny = <value>. On entry, nx must not be less than 1; nx = <value>.
29: On entry, ny = <value>, nx = <value> and m = <value>. These parameters must satisfy ny + nx <= m.
17: On entry, tdr = <value> while m = <value>. These parameters must satisfy tdr >= m. On entry, tdp = <value> while ny = <value>. These parameters must satisfy tdp >= ny.
466: On entry, ny = <value> and there are not exactly ny values of isz < 0. Number of values of isz < 0 = <value>.
467: On entry, nx = <value> and there are not exactly nx values of isz < 0.
73: Memory allocation failed.
74: An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please consult NAG for assistance.
498: On entry, either the variance-covariance matrix or the correlation matrix is not of full rank. Try removing some of the x variables by setting the appropriate elements of isz to zero.
499: Either a diagonal element of the partial variance-covariance matrix is zero and/or a computed partial correlation coefficient is greater than one. Both indicate that the matrix input in r was not positive-definite.
successful call of the nag_partial_corr function.
*/
int nag_partial_corr(
int m, // the number of variables in the variance-covariance/correlation matrix given in r.
int ny, // the number of Y variables for which partial correlation coefficents are to be computed.
int nx, // the number of X variables which are to be considered as fixed.
int sz[], // indicates which variables belong to set X and Y
double r[], // the variance-covariance or correlation matrix for the m variables as given by nag_corr_cov
int tdr, // the second dimension of the array r.
double p[], // p contains the strict upper triangular part of the ny by ny partial correlation matrix and the lower triangle of the ny by ny partial variance-covariance matrix.
int tdp // the second dimension of the array p.
); // Partial correlation/variance-covariance matrix
/** g02cac
performs a simple linear regression with or without a constant term.
The data is optionally weighted.
Example:
A program to calculate regression constants, a-hat and b-hat, the standard error of the regression
constants, the regression coefficient of determination and the degrees of freedom about the regression.
void test_nag_simple_linear_regression()
{
Nag_SumSquare mean;
char m, w;
int i, n;
double a, b, err_a, err_b, rsq, rss, df;
double *wtptr;
int success;
m = 'm';
w = 'w';
n = 8;
double x[10] = {1.0, 0.0, 4.0, 7.5, 2.5, 0.0, 10.0, 5.0};
double y[10] = {20.0, 15.5, 28.3, 45.0, 24.5, 10.0, 99.0, 31.2};
double wt[10] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
printf("The input data are as follows\n");
printf("n = 8, mean = Nag_AboutMean, wtptr =wt\n");
printf("\nx\n");
for(i = 0; i < 8; i++)
printf("%2.1f, ",x[i]);
printf("\ny\n");
for(i = 0; i < 8; i++)
printf("%3.1f, ",y[i]);
printf("\nwt\n");
for(i = 0; i < 8; i++)
printf("%2.1f, ",wt[i]);
printf("\n");
if (m == 'M' || m == 'm')
mean = Nag_AboutMean;
else
mean = Nag_AboutZero;
if (w == 'W' || w == 'w')
{
wtptr = wt;
}
success = nag_simple_linear_regression(mean, n, x, y, wtptr, &a, &b, &err_a, &err_b,
&rsq, &rss, &df);
if(success == 0)
{
if (mean == Nag_AboutMean)
{
printf("\nRegression constant a = %6.4f\n", a);
printf("Standard error of the regression constant a = %6.4f\n",err_a);
}
printf("Regression coefficient b = %6.4f\n", b);
printf("Standard error of the regression coefficient b = %6.4f\n", err_b);
printf("The regression coefficient of determination = %6.4f\n", rsq);
printf("The sum of squares of the residuals about the regression = %6.4f\n", rss);
printf("Number of degrees of freedom about the regression = %6.4f\n",df);
}
else
{
printf("The function nag_simple_linear_regression does not work\n");
}
}
The output is as follows:
The input data are as follows
n = 8, mean = Nag_AboutMean, wtptr =wt
x
1.0, 0.0, 4.0, 7.5, 2.5, 0.0, 10.0, 5.0,
y
20.0, 15.5, 28.3, 45.0, 24.5, 10.0, 99.0, 31.2,
wt
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
Regression constant a = 7.5982
Standard error of the regression constant a = 6.6858
Regression coefficient b = 7.0905
Standard error of the regression coefficient b = 1.3224
The regression coefficient of determination = 0.8273
The sum of squares of the residuals about the regression = 965.2454
Number of degrees of freedom about the regression = 6.0000
Return:
The function returns NAG error code or 0 if no error.
70: On entry, parameter mean had an illegal value.
11: On entry, n must not be less than 1: n = _value_ if mean = Nag AboutZero. On entry, n must not be less than 2: n = _value_ if mean = Nag AboutMean.
399: On entry, at least one of the weights is negative.
446: On entry, wt must contain at least 1 positive element if mean = Nag AboutZero or at least 2p ositive elements if mean = Nag AboutMean.
452: On entry, all elements of x and/or y are equal.
447: On entry, the sum of elements of wt must be greater than 1.0 if mean = Nag AboutZero or greater than 2.0 if mean = Nag AboutMean.
426: On entry, the degrees of freedom for the residual are zero, i.e., the designated number of parameters = the effective number of observations.
448: Residual sum of squares is zero, i.e., a perfect .t was obtained.
successful call of the function nag_simple_linear_regression function.
*/
int nag_simple_linear_regression(
Nag_SumSquare mean,
int n, // the number of observations
double x[], // the x observation
double y[], // the y observation
double wt[], // an (optional) weight is specified to be used in the weighted regression.
double *a, // the regression constant a
double *b, // the regression coefficient b
double *a_serr, // the standard error of the regression constant a.
double *b_serr, // the standard error of the regression coefficient b.
double *rsq, // the coefficient of determination
double *rss, // the sum of squares of the residuals about the regression.
double *df // the degrees of freedom associated with the residual sum of squares.
); // Regression const., SE of regression const., regression coefficient of determination sum of square, degree of freedom
/** g02cbc
performs a simple linear regression with or without a constant term.
The data is optionally weighted, and confidence intervals are calculated for the predicted
and average values of y at a given x.
Example:
A program to calculate the fitted value of y and the upper and lower limits of the
confidence interval for the regression line as well as the individual y values.
void test_nag_regress_confid_interval()
{
Nag_SumSquare mean;
int n;
double clm, clp;
double yhat[10], yml[10], ymu[10], yl[10], yu[10], h[10],
res[10], rms;
int i, success;
char m, w;
n = 9;
clm = 0.95;
clp = 0.95;
m = 'm';
w = 'w';
double x[10] = {1.0, 2.0, 4.0, 2.0, 2.0, 3.0, 7.0, 4.0, 2.0};
double y[10] = {4.0, 4.0, 5.1, 4.0, 6.0, 5.2, 9.1, 2.0, 4.1};
double wt[10] = {1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
printf("The input data are as follows\n");
printf("n = 9, clp = 0.95, clm = 0.95, mean = Nag_AboutMean\n");
printf("\nx\n");
for(i = 0; i < 9; i++)
printf("%2.1f, ",x[i]);
printf("\ny\n");
for(i = 0; i < 9; i++)
printf("%3.1f, ",y[i]);
printf("\nwt\n");
for(i = 0; i < 9; i++)
printf("%2.1f, ",wt[i]);
if (m == 'm' || m == 'M')
mean = Nag_AboutMean;
else if (m == 'z'|| m == 'Z')
mean = Nag_AboutZero;
success = nag_regress_confid_interval(mean, n, x, y, wt, clm, clp,
yhat, yml, ymu, yl, yu, h, res,&rms);
if(success == 0)
{
printf("\nThe results are as follows\n");
printf ("\ni yhat[i] yml[i] ymu[i] yl[i] yu[i] \n\n");
for (i=0; i < n; ++i)
{
printf("%ld %10.2f %10.2f", i, yhat[i], yml[i]);
printf(" %10.2f %10.2f %10.2f\n",ymu[i], yl[i], yu[i]);
}
}
else
{
printf("\nThe function nag_regress_confid_interval has some problem\n");
}
}
The output is as follows:
The input data are as follows
n = 9, clp = 0.95, clm = 0.95, mean = Nag_AboutMean
x
1.0, 2.0, 4.0, 2.0, 2.0, 3.0, 7.0, 4.0, 2.0,
y
4.0, 4.0, 5.1, 4.0, 6.0, 5.2, 9.1, 2.0, 4.1,
wt
1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
The results are as follows
i yhat[i] yml[i] ymu[i] yl[i] yu[i]
0 3.47 1.76 5.18 -0.46 7.40
1 4.14 2.87 5.42 0.38 7.90
2 5.49 4.15 6.84 1.71 9.27
3 4.14 2.87 5.42 0.38 7.90
4 4.14 2.87 5.42 0.38 7.90
5 4.82 3.70 5.94 1.11 8.53
6 7.52 4.51 10.53 2.87 12.16
7 5.49 4.15 6.84 1.71 9.27
8 4.14 2.87 5.42 0.38 7.90
Return:
The function returns NAG error or 0 if no error.
70: On entry, parameter mean had an illegal value.
11: On entry, n must not be less than 1: n = _value_ if mean = Nag AboutZero. On entry, n must not be less than 2: n = _value_ if mean = Nag AboutMean.
8: On entry, clm must not be greater than or equal to 1.0: clm = _value_. On entry, clp must not be greater than or equal to 1.0: clp = _value_.
6: On entry, clm must not be less than or equal to 0.0: clm = _value_. On entry, clp must not be less than or equal to 0.0: clp = _value_.
399: On entry, at least one of the weights is negative.
446: On entry, wt must contain at least 1 positive element if mean = Nag AboutZero or at least 2 positive elements if mean = Nag AboutMean.
453: On entry, all elements of x are equal.
447: On entry, the sum of elements of wt must be greater than 1.0 if mean = Nag AboutZero and 2.0 if mean = Nag AboutMean.
449: Residual mean sum of squares is zero, i.e., a perfect .t was obtained.
successful call of the function nag_regress_confid_interval function.
*/
int nag_regress_confid_interval(
Nag_SumSquare mean,
int n, // the number of observations
double x[], // observations on the independent variable X (all the values X must not be identical)
double y[], // observations on the dependent variable, Y
double wt[], // an (optional) weight is specified to be used in the weighted regression.
double clm, // the conffidence level for the confidence intervals for the mean.
double clp, // the conffidence level for the prediction intervals.
double yhat[], // the fitted values
double yml[], // contains the lower limit of the confidence interval for the regression line.
double ymu[], // contains the upper limit of the confidence interval for the regression line.
double yl[], // contains the lower limit of the confidence interval for the individual y value.
double yu[], // contains the upper limit of the confidence interval for the individual y value
double h[], // the leverage of each observation on the regression.
double res[], // the residuals of the regression.
double *rms // the residual mean square about the regression.
); // Confidence Interval for the regression line and for the individual y value.
/** g02dac
performs a general multiple linear regression when the independent variables
may be linearly dependent. Parameter estimates, standard errors, residuals and influence
statistics are computed. nag_regsn_mult_linear may be used to perform a weighted regression.
Example:
Data from an experiment with four treatments and three observations per treatment
are read in. The treatments are represented by dummy (0-1) variables. An
unweighted model is fitted with a mean included in the model.
void test_nag_regsn_mult_linear()
{
double rss, tol;
int i, ip, rank, j, m, n;
double df;
Boolean svd;
char weight, meanc;
Nag_IncludeMean mean;
double b[20];
double cov[210], h[20], p[440];
double res[20], se[20];
double com_ar[495];
double wt[20];
matrix q;
q.SetSize(20, 21);
double *wtptr;
n = 12;
m = 4;
mean = Nag_MeanInclude;
wtptr =NULL;
matrix x = {
{1.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 1.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 1.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 1.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
double y[20] = {33.63, 39.62, 38.18, 41.46, 38.02, 35.83,
35.99, 36.58, 42.92, 37.80, 40.43, 37.89};
int sx[20] = {1, 1, 1, 1};
printf("The input data are as follows\n");
printf("n = 12, m = 4, tol = 0.000001e0\n");
printf("x\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
printf("%2.1f, ",x[i][j]);
printf("\n");
}
printf("\ny\n");
for(i = 0; i < n; i ++)
{
printf("%5.3f, ", y[i]);
if((i + 1) % 7 == 0)
printf("\n");
}
ip = 0;
if (mean==Nag_MeanInclude)
ip += 1;
for (i=0; i<m; i++)
if (sx[i]>0) ip += 1;
tol = 0.00001e0;
nag_regsn_mult_linear(mean, n, x, 20, m, sx, ip, y,
wtptr, &rss, &df, b, se, cov, res, h, q, 21, &svd, &rank, p, tol, com_ar);
if (svd)
{ printf("\nModel not of full rank, rank = %4ld\n", rank);
ASSERT( rank == 4 );
}
printf("Residual sum of squares = %12.4e\n", rss);
ASSERT( is_equal( round( rss, 3 ), 22.227 ) );
printf("Degrees of freedom = %3.1f\n\n", df);
ASSERT( is_equal( round( df, 1 ), 8.0 ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -