g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_cp_stat (g02ecc)

## 1  Purpose

nag_cp_stat (g02ecc) calculates ${R}^{2}$ and ${C}_{p}$-values from the residual sums of squares for a series of linear regression models.

## 2  Specification

 #include #include
 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)

## 3  Description

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) ${R}^{2}$ 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 $\text{Total Sum of Squares}={\mathbf{tss}}=\sum {\left(y-\stackrel{-}{y}\right)}^{2}$ (if mean is fitted, otherwise ${\mathbf{tss}}=\sum {y}^{2}$) and $\text{Regression Sum of Squares}=\text{RegSS}={\mathbf{tss}}-{\mathbf{rss}}$, where ${\mathbf{rss}}=\text{residual sum of squares}=\sum {\left(y-\stackrel{^}{y}\right)}^{2}$.
The ${R}^{2}$-values can be examined to find a model with a high ${R}^{2}$-value but with small number of independent variables.
(b) ${C}_{p}$ statistic.
 $Cp=rssσ^2 -n-2p,$
where $p$ is the number of arguments (including the mean) in the model and ${\stackrel{^}{\sigma }}^{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 ${C}_{p}\simeq p$. ${C}_{p}$ is often plotted against $p$ to see which models are closest to the ${C}_{p}=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.

## 4  References

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

## 5  Arguments

1:     meanNag_IncludeMeanInput
On entry: indicates if a mean term is to be included.
${\mathbf{mean}}=\mathrm{Nag_MeanInclude}$
A mean term, intercept, will be included in the model.
${\mathbf{mean}}=\mathrm{Nag_MeanZero}$
The model will pass through the origin, zero-point.
Constraint: ${\mathbf{mean}}=\mathrm{Nag_MeanInclude}$ or $\mathrm{Nag_MeanZero}$.
2:     nIntegerInput
On entry: $n$, the number of observations used in the regression model.
Constraint: ${\mathbf{n}}$ must be greater than $2×{p}_{\mathrm{max}}$, where ${p}_{\mathrm{max}}$ is the largest number of independent variables fitted (including the mean if fitted).
3:     sigsqdoubleInput
On entry: the best estimate of true variance of the errors, ${\stackrel{^}{\sigma }}^{2}$.
Constraint: ${\mathbf{sigsq}}>0.0$.
4:     tssdoubleInput
On entry: the total sum of squares for the regression model.
Constraint: ${\mathbf{tss}}>0.0$.
5:     nmodIntegerInput
On entry: the number of regression models.
Constraint: ${\mathbf{nmod}}>0$.
6:     nterms[nmod]const IntegerInput
On entry: ${\mathbf{nterms}}\left[\mathit{i}-1\right]$ must contain the number of independent variables (not counting the mean) fitted to the $\mathit{i}$th model, for $\mathit{i}=1,2,\dots ,{\mathbf{nmod}}$.
On entry: ${\mathbf{rss}}\left[i-1\right]$ must contain the residual sum of squares for the $i$th model.
Constraint: ${\mathbf{rss}}\left[\mathit{i}-1\right]\le {\mathbf{tss}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{nmod}}$.
8:     rsq[nmod]doubleOutput
On exit: ${\mathbf{rsq}}\left[\mathit{i}-1\right]$ contains the ${R}^{2}$-value for the $\mathit{i}$th model, for $\mathit{i}=1,2,\dots ,{\mathbf{nmod}}$.
9:     cp[nmod]doubleOutput
On exit: ${\mathbf{cp}}\left[\mathit{i}-1\right]$ contains the ${C}_{p}$-value for the $\mathit{i}$th model, for $\mathit{i}=1,2,\dots ,{\mathbf{nmod}}$.
10:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{nmod}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nmod}}>0$.
NE_INTERNAL_ERROR
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.
NE_MODEL_PARAMETERS
On entry, number of parameters for model $〈\mathit{\text{value}}〉$ is too large for n. ${\mathbf{n}}=〈\mathit{\text{value}}〉$, number of parameters $\text{}=〈\mathit{\text{value}}〉$.
NE_REAL
On entry, ${\mathbf{sigsq}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sigsq}}>0.0$.
On entry, ${\mathbf{tss}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tss}}>0.0$.
NE_REAL_ARRAY_ELEM_CONS
On entry, ${\mathbf{cp}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{cp}}\left[i\right]\ge 0.0$, for all $i$.
On entry, ${\mathbf{rss}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{tss}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{rss}}\left[i\right]\le {\mathbf{tss}}$, for all $i$.

## 7  Accuracy

Accuracy is sufficient for all practical purposes.

None.

## 9  Example

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 ${R}^{2}$ and ${C}_{p}$ are then computed and printed along with the names of variables in the models.

### 9.1  Program Text

Program Text (g02ecce.c)

### 9.2  Program Data

Program Data (g02ecce.d)

### 9.3  Program Results

Program Results (g02ecce.r)