nag_regress_confid_interval (g02cbc) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_regress_confid_interval (g02cbc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_regress_confid_interval (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.

2  Specification

#include <nag.h>
#include <nagg02.h>
void  nag_regress_confid_interval (Nag_SumSquare mean, Integer n, const double x[], const double y[], const double wt[], double clm, double clp, double yhat[], double yml[], double ymu[], double yl[], double yu[], double h[], double res[], double *rms, NagError *fail)

3  Description

nag_regress_confid_interval (g02cbc) fits a straight line model of the form,
E y = a + bx
where E y  is the expected value of the variable y , to the data points
x 1 , y 1 , x 2 , y 2 , , x n , y n ,
such that
y i = a + bx i + e i ,   i = 1 , 2 , , n ,
where the e i  values are independent random errors. The i th data point may have an associated weight w i . The values of a  and b  are estimated by minimizing w i e i 2  (if the weights option is not selected then w i = 1.0 ). The fitted values y ^ i  are calculated using
y ^ i = a ^ + bx ^ i
where
a ^ = y-b - x - b ^ = w i x i - x - y i - y - w i x i - x - 2
and the weighted means x -  and y -  are given by
y - = w i y i w i   and   x - = w i x i w i .
The residuals of the regression are calculated using
res i = y i - y ^ i
and the residual mean square about the regression rms , is determined using
rms = w i y i - y ^ i 2 df
where df  (the number of degrees of freedom) has the following values
Note: the weights should be scaled to give the required degrees of freedom.
The function calculates predicted y  estimates for a value of x , x i * , is given by
y i * = a ^ + bx ^ i *
this prediction has a standard error
serr_pred = rms 1 + 1 w i + x i * - x - 2 w i x i - x - 2 .
The 1-α  confidence interval for this estimation of y  is given by
y i * ± t df 1 - α / 2 . serr_pred
where t df 1 - α / 2  refers to the 1 - α / 2  point of the t  distribution with df  degrees of freedom (e.g., when df = 20  and α=0.1 , t 20 0.95 = 2.086 ). If you specify the probability clp = 0.9 α=0.1  then the lower limit of this interval is
yl i = y i-t * i-t df 0.95 . serr_pred
and the upper limit is
yu i = y i * + t df 0.95 . serr_pred .
The mean value of y  at x i  is estimated by the fitted value y ^ i . This has a standard error of
serr_arg = rms 1 w i + x i - x - 2 w i x i - x - 2
and a 1-α  confidence interval is given by
y ^ i ± t df 1 - α / 2 . serr_arg .
For example, if you specify the probability clm = 0.6 α=0.4  then the lower limit of this interval is
yml i = y ^ i-t i-t df 0.8 . serr_arg
and the upper limit is
ymu i = y ^ i + t df 0.8 . serr_arg .
The leverage, h i , is a measure of the influence a value x i  has on the fitted line at that point, y ^ i . The leverage is given by
h i = w i w i + w i x i - x - 2 w i x i - x - 2
so it can be seen that
serr_arg = rms h i / w i and ​ serr_pred = rms 1 + h i / w i
Similar formulae can be derived for the case when the line goes through the origin, that is a=0 .

4  References

Snedecor G W and Cochran W G (1967) Statistical Methods Iowa State University Press

5  Arguments

1:     meanNag_SumSquareInput
On entry: indicates whether nag_regress_confid_interval (g02cbc) is to include a constant term in the regression.
mean=Nag_AboutMean
The constant term, a , is included.
mean=Nag_AboutZero
The constant term, a , is not included, i.e., a=0 .
Constraint: mean=Nag_AboutMean or Nag_AboutZero.
2:     nIntegerInput
On entry: the number of observations, n.
Constraints:
  • if mean=Nag_AboutMean, n2 ;
  • if mean=Nag_AboutZero, n1 .
3:     x[n]const doubleInput
On entry: observations on the independent variable, x .
Constraint: all the values of x  must not be identical.
4:     y[n]const doubleInput
On entry: observations on the dependent variable, y .
5:     wt[n]const doubleInput
On entry: if weighted estimates are required then wt must contain the weights to be used in the weighted regression. Otherwise wt need not be defined and may be set to the null pointer NULL, i.e., (double *)0. Usually wt[i-1]  will be an integral value corresponding to the number of observations associated with the i th data point, or zero if the i th data point is to be ignored. The sum of the weights therefore represents the effective total number of observations used to create the regression line. If wt = NULL , then the effective number of observations is n .
Constraint: wt = NULL ​ or ​ wt[i-1] 0.0 , for i=1,2,,n.
6:     clmdoubleInput
On entry: the confidence level for the confidence intervals for the mean.
Constraint: 0.0 < clm < 1.0 .
7:     clpdoubleInput
On entry: the confidence level for the prediction intervals.
Constraint: 0.0 < clp < 1.0 .
8:     yhat[n]doubleOutput
On exit: the fitted values, y ^ i .
9:     yml[n]doubleOutput
On exit: yml[i-1]  contains the lower limit of the confidence interval for the regression line at x[i-1] .
10:   ymu[n]doubleOutput
On exit: ymu[i-1]  contains the upper limit of the confidence interval for the regression line at x[i-1] .
11:   yl[n]doubleOutput
On exit: yl[i-1]  contains the lower limit of the confidence interval for the individual y value at x[i-1] .
12:   yu[n]doubleOutput
On exit: yu[i-1]  contains the upper limit of the confidence interval for the individual y value at x[i-1] .
13:   h[n]doubleOutput
On exit: the leverage of each observation on the regression.
14:   res[n]doubleOutput
On exit: the residuals of the regression.
15:   rmsdouble *Output
On exit: the residual mean square about the regression.
16:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_BAD_PARAM
On entry, argument mean had an illegal value.
NE_INT_ARG_LT
On entry, n=value.
Constraint: if mean=Nag_AboutMean, n2.
On entry, n=value.
Constraint: if mean=Nag_AboutZero, n1.
NE_NEG_WEIGHT
On entry, at least one of the weights is negative.
NE_REAL_ARG_GE
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 .
NE_REAL_ARG_LE
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 .
NE_SW_LOW
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.
NE_WT_LOW
On entry, wt must contain at least 1 positive element if mean=Nag_AboutZero or at least 2 positive elements if mean=Nag_AboutMean.
NE_X_IDEN
On entry, all elements of x are equal.
NW_RMS_EQ_ZERO
Residual mean sum of squares is zero, i.e., a perfect fit was obtained.

7  Accuracy

The computations are believed to be stable.

8  Further Comments

None.

9  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.

9.1  Program Text

Program Text (g02cbce.c)

9.2  Program Data

Program Data (g02cbce.d)

9.3  Program Results

Program Results (g02cbce.r)


nag_regress_confid_interval (g02cbc) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012