NAG C Library Function Document

nag_cp_stat (g02ecc)


nag_cp_stat (g02ecc) calculates R2 and Cp-values from the residual sums of squares for a series of linear regression models.


#include <nag.h>
#include <nagg02.h>
void  nag_cp_stat (Nag_IncludeMean mean, Integer n, double sigsq, double tss, Integer nmod, const Integer nterms[], const double rss[], double rsq[], double cp[], NagError *fail)


When selecting a linear regression model for a set of n observations a balance has to be found between the number of independent variables in the model and fit as measured by the residual sum of squares. The more variables included the smaller will be the residual sum of squares. Two statistics can help in selecting the best model.
(a) R2 represents the proportion of variation in the dependent variable that is explained by the independent variables.
R2=Regression Sum of SquaresTotal Sum of Squares,  
where Total Sum of Squares=tss= y-y- 2 (if mean is fitted, otherwise tss=y2) and
Regression Sum of Squares=RegSS=tss-rss, where
rss=residual sum of squares= y-y^ 2.
The R2-values can be examined to find a model with a high R2-value but with small number of independent variables.
(b) Cp statistic.
Cp=rssσ^2 -n-2p,  
where p is the number of parameters (including the mean) in the model and σ^2 is an estimate of the true variance of the errors. This can often be obtained from fitting the full model.
A well fitting model will have Cpp. Cp is often plotted against p to see which models are closest to the Cp=p line.
nag_cp_stat (g02ecc) may be called after nag_all_regsn (g02eac) which calculates the residual sums of squares for all possible linear regression models.


Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
Weisberg S (1985) Applied Linear Regression Wiley


1:     mean Nag_IncludeMeanInput
On entry: indicates if a mean term is to be included.
A mean term, intercept, will be included in the model.
The model will pass through the origin, zero-point.
Constraint: mean=Nag_MeanInclude or Nag_MeanZero.
2:     n IntegerInput
On entry: n, the number of observations used in the regression model.
Constraint: n must be greater than 2×pmax, where pmax is the largest number of independent variables fitted (including the mean if fitted).
3:     sigsq doubleInput
On entry: the best estimate of true variance of the errors, σ^2.
Constraint: sigsq>0.0.
4:     tss doubleInput
On entry: the total sum of squares for the regression model.
Constraint: tss>0.0.
5:     nmod IntegerInput
On entry: the number of regression models.
Constraint: nmod>0.
6:     nterms[nmod] const IntegerInput
On entry: nterms[i-1] must contain the number of independent variables (not counting the mean) fitted to the ith model, for i=1,2,,nmod.
7:     rss[nmod] const doubleInput
On entry: rss[i-1] must contain the residual sum of squares for the ith model.
Constraint: rss[i-1]tss, for i=1,2,,nmod.
8:     rsq[nmod] doubleOutput
On exit: rsq[i-1] contains the R2-value for the ith model, for i=1,2,,nmod.
9:     cp[nmod] doubleOutput
On exit: cp[i-1] contains the Cp-value for the ith model, for i=1,2,,nmod.
10:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
On entry, nmod=value.
Constraint: nmod>0.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
On entry: the number of parameters, p, is value and n=value.
Constraint: n2p.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
On entry, sigsq=value.
Constraint: sigsq>0.0.
On entry, tss=value.
Constraint: tss>0.0.
A value of Cp is less than 0.0. This may occur if sigsq is too large or if rss, n or IP are incorrect.
On entry, rss[value]=value and tss=value.
Constraint: rss[i] or tss, for all i.


Accuracy is sufficient for all practical purposes.

Parallelism and Performance

nag_cp_stat (g02ecc) is not threaded in any implementation.

Further Comments



The data, from an oxygen uptake experiment, is given by Weisberg (1985). The independent and dependent variables are read and the residual sums of squares for all possible models computed using nag_all_regsn (g02eac). The values of R2 and Cp are then computed and printed along with the names of variables in the models.

Program Text

Program Text (g02ecce.c)

Program Data

Program Data (g02ecce.d)

Program Results

Program Results (g02ecce.r)