📄 ocn_g02.h
字号:
printf("Variable Parameter Estimate Standard error\n\n");
for (j=0; j<ip; j++)
printf("%6ld%20.4e%20.4e\n", j+1, b[j], se[j]);
printf("\n");
printf("Obs Residuals h\n\n");
for (i=0; i<n; i++)
printf("%6ld%20.4e%20.4e\n", i+1, res[i], h[i]);
}
The output is as follows:
The input data are as follows
n = 12, m = 4, tol = 0.000001e0
x
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
y
33.630, 39.620, 38.180, 41.460, 38.020, 35.830, 35.990,
36.580, 42.920, 37.800, 40.430, 37.890,
Model not of full rank, rank = 4
Residual sum of squares = 2.2227e+001
Degrees of freedom = 8.0
Variable Parameter Estimate Standard error
1 3.0557e+001 3.8494e-001
2 5.4467e+000 8.3896e-001
3 6.7433e+000 8.3896e-001
4 1.1047e+001 8.3896e-001
5 7.3200e+000 8.3896e-001
Obs Residuals h
1 -2.3733e+000 3.3333e-001
2 1.7433e+000 3.3333e-001
3 8.8000e-001 3.3333e-001
4 -1.4333e-001 3.3333e-001
5 1.4333e-001 3.3333e-001
6 -1.4700e+000 3.3333e-001
7 -1.8867e+000 3.3333e-001
8 5.7667e-001 3.3333e-001
9 1.3167e+000 3.3333e-001
10 1.7967e+000 3.3333e-001
11 -1.1733e+000 3.3333e-001
12 5.9000e-001 3.3333e-001
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 1: m = _value_. On entry, ip must not be less than 1: ip = _value_. On entry, sx[_value_] must not be less than 0: sx[_value_] = _value_.
17: On entry, tdx = _value_ while m = _value_. These parameters must satisfy tdx = m. On entry, tdq = _value_ while ip+1 = _value_. These parameters must satisfy tdq = ip+1. On entry, n = _value_ while ip = _value_. These parameters must satisfy n = ip.
5: On entry, tol must not be less than 0.0: tol = _value_. On entry, wt[_value_] must not be less than 0.0: wt[_value_] = _value_.
70: On entry, parameter mean had an illegal value.
425: Either a value of sx is < 0, or ip is incompatible with mean and sx, or ip > the effective number of observations.
427: The singular value decomposition has failed to converge.
426: The degrees of freedom for the residuals are zero, i.e., the designated number of parameters = the effective number of observations. In this case the parameter estimates will be returned along with the diagonal elements of H, but neither standard errors nor the variance-covariance matrix will be calculated.
73: Memory allocation failed.
successful call of the nag_regsn_mult_linear function
*/
int nag_regsn_mult_linear(
Nag_IncludeMean mean,
int n, // the number of observations
double x[], //the variable for which ith observation for the jth potential indepent variable.
int tdx, // the second dimension of the array x.
int m, // the total number of independent variables in the data set.
int sx[], // indicates which of the potential independent variables are to be included in the model.
int ip, // the number p of independent variables in the model, including the mean or intercept if present.
double y[], // the observations on the dependent variable.
double wt[], // an (optional) weight is specified to be used in the weighted regression.
double *rss, // the residual sum of squares for the regression.
double *df, // the degree of freedom associated with the residual sum of squares.
double b[], // return the least-squares estimates of the parameters of the regression model.
double se[], // return the standard errors of the ip parameter estimates given in b.
double cov[], // return the variance-covariance matrix of estimated parameters in b.
double res[], // return the (weighted) residuals.
double h[], // return the diagonal elements of H.
double q[], // return the results of the QR decomposition.
int tdq, // the second dimension of the array q.
Boolean *svd, // return TRUE if a singular value decomposition has been performed, otherwise FALSE
int *rank, // return the rank of the independent variables.
double p[], // details of the QR decomposition and SVD if used.
double tol, // the value of tol is used to decide what is the rank of the independent variables.
double com_ar[] // return information which is needed by nag_regsn_mult_linear_newyvar_if svd = TRUE.
);// Parameter estimates, standard errors, residuals ...
/** g02dcc
adds or deletes an observation from a general regression model fitted by nag_regsn_mult_linear.
Example:
A data set consisting of 12 observations with four independent variables is read in
and a general linear regression model fitted by nag_regsn_mult_linear (g02dac) and
parameter estimates printed. The last observation is then dropped and the parameter
estimates recalculated, using nag_regsn_mult_linear_upd model (g02ddc), and printed.
void test_nag_regsn_mult_linear_addrem_obs()
{
double rss, tol;
int i, ip, rank, j, m, n;
double df;
Boolean svd;
char meanc, weight;
Nag_IncludeMean mean;
Nag_UpdateObserv update;
double b[5];
double cov[15];
double h[12];
double p[35];
double res[12], se[5];
double com_ar[45], wt[12];
matrix q;
q.SetSize(12,6);
double *wtptr;
int success;
n = 12;
m = 4;
weight = 'u';
meanc = 'z';
if (meanc=='m')
mean = Nag_MeanInclude;
else
mean = Nag_MeanZero;
if (weight=='w')
wtptr = wt;
else
wtptr = NULL;
matrix xm = {{1.0, 0.0, 0.0, 0.0, 0}, {0.0, 0.0, 0.0, 1.0, 0},
{0.0, 1.0, 0.0, 0.0, 0}, {0.0, 0.0, 1.0, 0.0, 0},
{0.0, 0.0, 0.0, 1.0, 0}, {0.0, 1.0, 0.0, 0.0, 0},
{0.0, 0.0, 0.0, 1.0, 0}, {1.0, 0.0, 0.0, 0.0, 0},
{0.0, 0.0, 1.0, 0.0, 0}, {1.0, 0.0, 0.0, 0.0, 0},
{0.0, 0.0, 1.0, 0.0, 0}, {1.0, 1.0, 1.0, 1.0, 0}};
double y[12] = {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[5] = {1, 1, 1, 1, 4};
ip = 4;
tol = 0.00001e0;
success = nag_regsn_mult_linear(mean, n, xm, 5, m, sx, ip, y, wtptr, &rss,
&df, b, se, cov, res, h, q, 6, &svd, &rank,
p, tol, com_ar);
if(success ==0)
{
printf("Results from g02dac\n\n");
if (svd)
printf("Model not of full rank\n");
printf("Residual sum of squares = %12.4e\n", rss);
printf("Degrees of freedom = %3.1f\n\n", df);
printf("Variable Parameter Estimate Standard error\n\n");
for (j=0; j<ip; j++)
printf("%6ld%20.4e%20.4e\n", j+1, b[j], se[j]);
printf("\n");
update = Nag_ObservDel;
success = nag_regsn_mult_linear_addrem_obs(update, mean, m, sx, q, 6, ip,
xm, 12, 5, 12, y[11], wtptr, &rss);
printf("Results from dropping an observation using g02dcc\n");
n = n - 1;
nag_regsn_mult_linear_upd_model(n, ip, q, 6, &rss, &df, b, se, cov,
&svd, &rank, p, tol);
printf("Residual sum of squares = %12.4e\n", rss);
printf("Degrees of freedom = %3.1f\n\n", df);
printf("Variable Parameter Estimate Standard error\n\n");
for (j=0; j<ip; j++)
printf("%6ld%20.4e%20.4e\n", j+1, b[j], se[j]);
}
else
{
printf("There is some problem with the function nag_regsn_mult_linear\n");
}
}
The output is as follows:
Results from g02dac
Residual sum of squares = 5.2748e+003
Degrees of freedom = 8.0
Variable Parameter Estimate Standard error
1 2.0724e+001 1.3801e+001
2 1.4085e+001 1.6240e+001
3 2.6324e+001 1.3801e+001
4 2.2597e+001 1.3801e+001
Results from dropping an observation using g02dcc
Residual sum of squares = 2.1705e+001
Degrees of freedom = 7.0
Variable Parameter Estimate Standard error
1 3.6003e+001 1.0166e+000
2 3.7005e+001 1.2451e+000
3 4.1603e+001 1.0166e+000
4 3.7877e+001 1.0166e+000
Return:
The function returns NAG error code or 0 if no error.
11: On entry, ip must not be less than 1: ip = _value_. On entry, m must not be less than 1: m = _value_. On entry, ix must not be less than 1: ix = _value_. On entry, nr must not be less than 1: nr = _value_.
17: On entry tdq = _value_ while ip + 1 = _value_. These parameters must satisfy tdq = ip + 1. On entry tdx = _value_ while m = _value_. These parameters must satisfy tdx = m.
19: On entry ix = _value_ while nr = _value_. These parameters must satisfy ix = nr.
5: On entry, wt must not be less than 0.0: wt = _value_. On entry, rss must not be less than 0.0: rss = _value_.
70: On entry, update had an illegal value. On entry, mean had an illegal value.
437: On entry, for mean = Nag MeanInclude, number of non-zero values of sx must be equal to ip - 1: number of non-zero values of sx = _value_, ip - 1 = _value_. On entry, for mean = Nag MeanZero, number of non-zero values of sx must be equal to ip: number of non-zero values of sx = _value_, ip = _value_.
429: The rss could not be updated because the input rss was less than the calculated decrease in rss when the new observation was deleted.
428: The R matrix could not be updated: to, either, delete non-existent observation, or, add an observation to R matrix with zero diagonal element.
73: Memory allocation failed.
successful call of the int nag_regsn_mult_linear_addrem_obs function.
*/
int nag_regsn_mult_linear_addrem_obs(
Nag_UpdateObserv update,
Nag_IncludeMean mean,
int m, // the total number of independent variables in the data set.
int sx[], // indicates which of the potential independent variables are to be included in the model.
double q[], // array q taken from output by_nag_regsn_mult_linear
int tdq, // the last dimension of the array q.
int ip, // the number of linear terms in general linear regression model (including mean if there is one).
double x[], // the ip values for the dependent variables of the observation to be added or deleted.
int nr, // the number of rows of the notional two dimensional array x.
int tdx, // the trailing dimension of the notional two dimensional array x.
int ix, // the row of the two dimensional array x that contains the values for the dependent variables of the observation to be added or deleted.
double y, // the value of the dependent variable for the observation to be added or deleted.
double *wt, // an (optional) weight for a new observation.
double *rss // the value of the residual sums of squares for the original set of observations.
); // Regression coefficients, standard errors and variance-covariance matrix.
/** g02ddc
calculates the regression parameters for a general linear regression model. It is intended to
be called after nag_regsn_mult_linear_addrem_obs, nag_regsn_mult_linear_add_var or
nag_regsn_mult_linear_delete_var.
Example:
A data set consisting of 12 observations and four independent variables is input and
a regression model fitted by calls to nag_regsn_mult_linear_add_var (g02dec). The
parameters are then calculated by nag_regsn_mult_linear_upd_model and the results printed.
void test_nag_regsn_mult_linear_upd_model()
{
double rss, tol;
int i, ip, rank, j, m, n;
double df;
Boolean svd;
char weight;
double cov[15];
double p[35];
double b[5];
matrix q;
q.SetSize(12, 6);
int success;
double se[5], wt[12], xe[12];
double *wtptr;
n = 12;
m = 4;
weight = 'u';
if (weight=='w')
wtptr = wt;
else
wtptr = NULL;
matrix x = {{1.0, 0.0, 0.0, 0.0, 0}, {0.0, 0.0, 0.0, 1.0, 0},
{0.0, 1.0, 0.0, 0.0, 0}, {0.0, 0.0, 1.0, 0.0, 0},
{0.0, 0.0, 0.0, 1.0, 0}, {0.0, 1.0, 0.0, 0.0, 0},
{0.0, 0.0, 0.0, 1.0, 0}, {1.0, 0.0, 0.0, 0.0, 0},
{0.0, 0.0, 1.0, 0.0, 0}, {1.0, 0.0, 0.0, 0.0, 0},
{0.0, 0.0, 1.0, 0.0, 0}, {0.0, 1.0, 0.0, 0.0, 0}};
q[0][0] = 33.63;
q[1][0] = 39.62;
q[2][0] = 38.18;
q[3][0] = 41.46;
q[4][0] = 38.02;
q[5][0] = 35.83;
q[6][0] = 35.99;
q[7][0] = 36.58;
q[8][0] = 42.92;
q[9][0] = 37.80;
q[10][0] = 40.43;
q[11][0] = 37.89;
printf("\nThe input data are as follows\n");
printf("n = 12, m = 4, wtptr = NULL, tol = 0.000001e0, tdq = 6\n");
printf("x\n");
for(i = 0; i < 12; i ++)
{
for(j = 0; j < 3; j++)
printf("%2.1f, ",x[i][j]);
printf("\n");
}
printf("q[i][0]\n");
for(i = 0; i < 12; i++)
printf("%4.2f, ", q[i][0]);
printf("\n\nThe results are follows\n\n");
tol = 0.000001e0;
ip = 0;
for (j=0; j<m; ++j)
{
for (i=0; i<n; i++)
xe[i] = x[i][j];
success = nag_regsn_mult_linear_add_var(n, ip, q, 6, p, wtptr, xe, &rss, tol);
if( success == 0)
ip += 1;
else if (success ==NE_NVAR_NOT_IND)
printf(" *New variable not added *\n");
else
printf("This function has some problem\n");
}
rss = 0.0;
success = nag_regsn_mult_linear_upd_model(n, ip, q, 6, &rss, &df, b, se, cov, &svd,
&rank, p, tol);
if(success == 0)
{
printf("\n");
if (svd)
printf("Model not of full rank\n\n");
printf("Residual sum of squares = %12.4e\n", rss);
printf("Degrees of freedom = %3.1f\n\n", df);
printf("Variable Parameter Estimate Standard error\n\n");
for (j=0; j<ip; j++)
printf("%6ld%20.4e%20.4e\n", j+1, b[j], se[j]);
printf("\n");
}
else
printf("\nThe function of g02ddc has some problem\n");
}
The output is as follows:
The input data are as follows
n = 12, m = 4, wtptr = NULL, tol = 0.000001e0, tdq = 6
x
1.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0,
q[i][0]
33.63, 39.62, 38.18, 41.46, 38.02, 35.83, 35.99, 36.58, 42.92, 37.80, 40.43, 37.89,
The results are follows
Residual sum of squares = 2.2227e+001
Degrees of freedom = 8.0
Variable Parameter Estimate Standard error
1 3.6003e+001 9.6235e-001
2 3.7300e+001 9.6235e-001
3 4.1603e+001 9.6235e-001
4 3.7877e+001 9.6235e-001
Return:
The function returns NAG error code or 0 if no error.
11: On entry, n must not be less than 1: n = _value_. On entry, ip must not be less than 1: ip = _value_.
17: On entry tdq = _value_ while ip + 1 = _value_. These parameters must satisfy tdq = ip + 1. On entry, n = _value_ while ip = _value_. These parameters must satisfy n = ip.
430: The degrees of freedom for error are less than or equal to 0. In this case the estimates,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -