/* * * 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 /* 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 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"); } } 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 = . On entry, ny must not be less than 2: ny = . On entry, nx must not be less than 1; nx = . 29: On entry, ny = , nx = and m = . These parameters must satisfy ny + nx <= m. 17: On entry, tdr = while m = . These parameters must satisfy tdr >= m. On entry, tdp = while ny = . These parameters must satisfy tdp >= ny. 466: On entry, ny = and there are not exactly ny values of isz < 0. Number of values of isz < 0 = . 467: On entry, nx = 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; i0) 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 ) ); printf("Variable Parameter Estimate Standard error\n\n"); for (j=0; j 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 0) { for (j=0; j ip + 1. 17: On entry, nres = _value_ while n = _value_. These parameters must satisfy nres = n. 6: On entry, rms must not be less than or equal to 0.0: rms = _value_. On entry, h[_value_] must not be less than or equal to 0.0: h[_value_] = _value_. 8: On entry, h[_value_] must not be greater than or equal to 1.0: h[_value_] = _value_. 433: On entry, the value of a residual is too large for the given value of rms: res[_value_] = _value_, rms = _value_. successful call of the nag_regsn_std_resid_influence function. */ int nag_regsn_std_resid_influence( int n, // number of observations included in the regression. int ip, // the number of linear parameters estimated in the regression model. int nres, // the number of residuals. double res[], // the residuals. double h[], // the diagonal elements of H, h(i), corresponding to the residuals in res. double rms, // the estimate of s2 based on all n observations, s2, i.e., the residual mean square. double sres[] // the standardised residuals and influence statistics. ); // Internal studentized statistic, External studentized statistic, Cook's D residual, Atkinson's T residual. /** g02gac fits a generalized linear model with normal errors. Example: The model: [y = ß1 + ß2x + epsilon] for a sample of 5 observations. void test_nag_glm_normal() { char linkc, meanc, weightc; Nag_Link link; Nag_IncludeMean mean; int i, j, m, n, ip; int max_iter, print_iter, rank; double ex_power, scale, tol, eps, rss, df; int sx[2]; double b[2]; matrix v; v.SetSize(5,8); double wt[5]; matrix x; x.SetSize(5,2); double y[5], se[2], cov[3]; double *wtptr; double *offsetptr; offsetptr = NULL; int success; linkc = 'r'; meanc = 'm'; weightc = 'n'; n = 5; m = 1; print_iter = 0; scale = 0.0; set_enum(linkc, &link, meanc, &mean); wtptr = NULL; x[0][0] = 1.0; y[0] = 25.0; x[1][0] = 2.0; y[1] = 10.0; x[2][0] = 3.0; y[2] = 6.0; x[3][0] = 4.0; y[3] = 4.0; x[4][0] = 5.0; y[4] = 3.0; sx[0] = 1; ip = 0; for (j=0; j0) ip += 1; if (mean == Nag_MeanInclude) ip += 1; if (link == Nag_Expo) printf("there is some problemtttttttt"); else ex_power = 0.0; printf("ipip = %d",ip); max_iter = 10; tol = 5e-5; eps = 1e-6; String str = "c:\\c\\test.txt"; success = nag_glm_normal(link, mean, n, x, 2, m, sx, ip, y, wtptr, offsetptr, &scale, ex_power, &rss, &df, b, &rank,se, cov, v, 8, tol, max_iter, print_iter, str, eps); if (success == NE_NOERROR || success == NE_LSQ_ITER_NOT_CONV || success == NE_RANK_CHANGED || success == NE_ZERO_DOF_ERROR) { printf("\nResidual sum of squares = %12.4e\n", rss); printf("Degrees of freedom = %3.1f\n\n", df); printf(" Estimate Standard error\n\n"); for (i=0; i0) nvar = nvar + 1; if (mean == Nag_MeanInclude) nvar =nvar + 1; max_iter = 10; tol = 5e-5; eps = 1e-6; str = "c:\\c\\c"; success = nag_glm_binomial(link, mean, n, x, 2, m, ivar, nvar, y, binom, wtptr, offsetptr, &dev, &df, beta, &rank, se, cov, v, 8, tol, max_iter, print_iter, str, eps); if(success == NE_NOERROR || success == NE_SVD_NOT_CONV || success == NE_LSQ_ITER_NOT_CONV || success == NE_RANK_CHANGED || success == NE_ZERO_DOF_ERROR) { printf("\nDeviance = %12.4e\n", dev); printf("Degrees of freedom = %3.1f\n\n", df); printf("Estimate Standard error\n\n"); for (i=0; i0) ip =ip + 1; if (mean == Nag_MeanInclude) ip = ip + 1; ex_power = 0.0; max_iter = 10; tol = 5e-5; eps = 1e-6; success = nag_glm_poisson(link, mean, n, x, 9, m,sx, ip, y, wtptr, offsetptr, ex_power, &dev, &df, b, &rank, se, cov, v, 15, tol, max_iter, print_iter, "c:\\c\\tre.txt", eps); if (success == NE_NOERROR || success == NE_LSQ_ITER_NOT_CONV || success == NE_RANK_CHANGED || success == NE_ZERO_DOF_ERROR) { printf("\nDeviance = %12.4e\n", dev); printf("Degrees of freedom = %3.1f\n\n", df); printf(" Estimate Standard error\n\n"); for (i=0; i0) ip = ip + 1; if (mean == Nag_MeanInclude) ip = ip + 1; ex_power = 0.0; max_iter = 10; tol = 5e-5; eps = 1e-6; success = nag_glm_gamma(link, mean, n, x, 2, m, sx, ip, y, wtptr, offsetptr, &scale, ex_power, &dev, &df, b, &rank, se, cov, v, 8, tol, max_iter, print_iter, "c:\\c\\test.txt", eps); if (success == NE_NOERROR || success == NE_LSQ_ITER_NOT_CONV || success == NE_RANK_CHANGED || success == NE_ZERO_DOF_ERROR) { printf("\nDeviance = %12.4e\n", dev); printf("Degrees of freedom = %3.1f\n\n", df); printf(" Estimate Standard error\n\n"); for (i=0; i 0.0. 475: On entry, regtype = Nag MallowsReg, cucv =_value_ and m =_value_. For this value of regtype, cucv must be >= m. 476: On entry, regtype = Nag_SchweppeReg, cucv =_value_ and m =_value_. For this value of regtype, cucv must be >= m^(1/2). 474: On entry, psifun _= Nag Lsq, sigma est = Nag SigmaChi and dchi =_value_. For these values of psifun and sigma est, dchi must be > 0.0. 477: On entry, psifun = Nag HampelFun and hpsi[0] =_value_, hpsi[1] =_value_ and hpsi[2] =_value_. For this value of psifun, the elements of hpsi must satisfyt he condition 0.0 = hpsi[0] = hpsi[1] = hpsi[2] and hpsi[2] > 0.0. 478: The number of iterations required to calculate the weights exceeds max_iter. This is only applicable if regtype _= Nag_HuberReg. 479: The number of iterations required to calculate ß1 exceeds max_iter. This is onlya pplicable if regtype = Nag MallowsReg and sigma est = Nag SigmaRes. 480: The number of iterations required to calculate theta and sigma exceeds max_iter. In this case, info[2] = max_iter on exit. 481: The iterations to solve the weighted least-squares equations failed to converge. 482: The weighted least-squares equations are not of full rank. 483: Failure to invert matrix while calculating covariance. If regtype = Nag HuberReg, then (XTX) is almost singular. If regtype _= Nag HuberReg, then S1 is singular or almost singular. This maybe due to too many diagonal elements of the matrix being zero, see Section 6. 484: In calculating the correlation factor for the asymptotic variance-covariance matrix, the factor for covariance matrix = 0. For this error, see Section 6. In this case c is returned as inv(t(X)*X). (This is only applicable if regtype = Nag HuberReg). 485: The estimated variance for an element of ? = 0. In this case the diagonal element of c will contain the negative variance and the above diagonal elements in the row and the column corresponding to the element will be returned as zero. This error maybe caused byrounding errors or too many of the diagonal elements of p being zero. See Section 6. 486: n = value_, rank of x =_value_. The degrees of freedom for error, n - (rank of x) must be > 0.0. 487: The estimated value of s was 0.0 during an iteration. 78: Cannot open file string_ for appending. 79: Cannot close file string_. 73: Memory allocation failed. successful call of the nag_robust_m_regsn_estim function. */ int nag_robust_m_regsn_estim( Nag_RegType regtype, Nag_PsiFun psifun, Nag_SigmaEst sigma_est, Nag_CovMatrixEst covmat_est, int n, // the number of observations. int m, // the number m, of independent variables. double x[], // the values of the X matrix, i.e., the independent variables. int tdx, // the second dimension of the array x. double y[], // the data values of the dependent variable. double cpsi, // if psifun = Nag HuberFun, cpsi must specify the parameter, c, of Huber's psi function. double hpsi[], // if psifun = Nag_HampelFun then hpsi[0], hpsi[1] and hpsi[2] must specify the parameters h1, h2, and h3, of Hampel's piecewise linear psi function. double cucv, // if regtype = Nag_MallowsReg then cucv must specify the value of the constant, c, of the function u for Maronna's proposed weights. If regtype = Nag_SchweppeReg then cucv must specify the value of the function u for the Krasker-Welsch weights. double dchi, // the constant, d, of the Chi function. double theta[], // starting values of the parameter vector theta. These maybe obtained from least-squares regression. double *sigma, // a starting value for the estimation of sigma. double c[], // the diagonal elements of c contain the estimated asymptotic standard errors of the estimates of theta, int tdc, // the second dimension of the array c. double rs[], // contains the residuals from the model evaluated at final value of theta. double wt[], // contains the vector of weights. double tol, // the relative precision for the calculation of A. int max_iter, // the maximum number of iterations that should be used in the calculation of A. int print_iter, // the amount of information that is printed on each iteration. const char *outfile, // a null terminated character string giving the name of the .le to which results should be printed. double info[] // elements of info contain the values of parameters, rank, and number of iterations. ); // Sigma, Theta, Weight, Standard Error.... /** g02hkc computes a robust estimate of the covariance matrix for an expected fraction of gross errors. Example: A sample of 10ob servations on three variables is read in and the robust estimate of the covariance matrix is computed assuming 10% gross errors are to be expected and file c;\c\test.txt exists. void test_nag_robust_corr_estim() { int i, j, k, m, n; int ifail; matrix x; x.SetSize(20,10); double theta[10]; int tdx=10; int max_iter, l1, l2; int print_iter; double eps, cov[15]; int iter; double tol; x[0][0] = 3.4; x[0][1] = 6.9 ; x[0][2] = 12.2; x[1][0] = 6.4 ; x[1][1] = 2.5; x[1][2] = 15.1; x[2][0] = 4.9; x[2][1] = 5.5; x[2][2] = 14.2; x[3][0] = 7.3; x[3][1] = 1.9; x[3][2] = 18.2; x[4][0] = 8.8; x[4][1] = 3.6; x[4][2] = 11.7; x[5][0] = 8.4; x[5][1] = 1.3; x[5][2] = 17.9; x[6][0] = 5.3; x[6][1] = 3.1; x[6][2] = 15.0; x[7][0] = 2.7; x[7][1] = 8.1; x[7][2] = 7.7; x[8][0] = 6.1; x[8][1] = 3.0; x[8][2] = 21.9; x[9][0] = 5.3; x[9][1] = 2.2; x[9][2] = 13.9; n = 10; m = 3; eps = 0.1; max_iter = 100; tol = 5e-5; print_iter = 1; printf("The input data are as follows\n"); printf("\nn = 10, m = 3, eps = 0.1, max_iter = 100, tol = 5e-5, print_iter = 1\n"); printf("\nthe ouput file name = c:\\c\\test.txt\n"); String strFile = "c:\\c\\test.txt"; nag_robust_corr_estim(n, m, x, tdx, eps, cov, theta, max_iter, print_iter, strFile, tol, &iter); printf("\n\ng02hkc required %ld iterations to converge\n\n", iter); printf("Covariance matrix\n"); l2 = 0; for (j = 1; j <= m; ++j) { l1 = l2 + 1; l2 += j; for (k = l1; k <= l2; ++k) { printf("%6.3f\t", cov[k - 1]); } printf("\n"); } printf("\n\ntheta\n"); for (j = 1; j <= m; ++j) { printf("%6.3f\n", theta[j - 1]); } } The output is as follows The input data are as follows n = 10, m = 3, eps = 0.1, max_iter = 100, tol = 5e-5, print_iter = 1 the ouput file name = c:\c\test.txt g02hkc required 23 iterations to converge Covariance matrix 3.461 -3.681 5.348 4.682 -6.645 14.439 theta 5.818 3.681 15.037 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_. 19: On entry, m = _value_ while n = _value_. These parameters must satisfy m = n. 17: On entry, tdx = _value_ while m = _value_ . These parameters must satisfy tdx = m. 12: On entry, max_iter must not be less than or equal to 0: max_iter = _value_. 5: On entry, eps must not be less than 0.0: eps = _value_. 8: On entry, eps must be not be greater than or equal to 1.0: eps = _value_. 6: On entry, tol must not be less than or equal to 0.0: tol = _value_. 497: On entry, column _value_ of array x has constant value. 3: Too many iterations(_value_). The iterative procedure to find the co-variance matrix C, has failed to converge in max_iter iterations. 490: The iterative procedure to find C has become unstable. This may happen if the value of eps is too large. 78: Cannot open file _string for appending. 79: Cannot close file _string . 73: Memory allocation failed. successful call of the nag_robust_corr_estim function. */ int nag_robust_corr_estim( int n, // the number of observations. int m, // the number of independent variables double x[], // must contain the ith observation for the jth variable. int tdx, // the second dimension of the array x. double eps, // the expected fraction of gross errors expected in the sample. double cov[], // the covariance matrix. double theta[], // the robust estimate of the location parameters. int max_iter, // the maximum number of iterations that will be used during the calculation of the covariance matrix. int print_iter, // indicates if the printing of information on the iterations is required. const char *outfile, // a null terminated character string giving the name of the file to which results should be printed. double tol, // the relative precision for the final estimates of the covariance matrix. int *iter // the number of iterations performed. ); // Robust Covariance matrix, Robust Parameter Estimates. /* end proto */ #ifdef __cplusplus } #endif #endif /* not NAGG02 */