NAG CL Interface
g02cbc (linregs_​noconst)

1 Purpose

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>
void  g02cbc (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)
The function may be called by the names: g02cbc, nag_correg_linregs_noconst or nag_regress_confid_interval.

3 Description

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: mean Nag_SumSquare Input
On entry: indicates whether 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: n Integer Input
On entry: N, the number of observations.
Constraints:
  • if mean=Nag_AboutMean, n2 ;
  • if mean=Nag_AboutZero, n1 .
3: x[n] const double Input
On entry: observations on the independent variable, x .
Constraint: all the values of x must not be identical.
4: y[n] const double Input
On entry: observations on the dependent variable, y .
5: wt[n] const double Input
On entry: if weighted estimates are required then wt must contain the weights to be used in the weighted regression. 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 weights are not provided then wt must be set to NULL and the effective number of observations is n.
Constraint: if wtis notNULL, wt[i-1] = 0.0 , for i=1,2,,n.
6: clm double Input
On entry: the confidence level for the confidence intervals for the mean.
Constraint: 0.0 < clm < 1.0 .
7: clp double Input
On entry: the confidence level for the prediction intervals.
Constraint: 0.0 < clp < 1.0 .
8: yhat[n] double Output
On exit: the fitted values, y ^ i .
9: yml[n] double Output
On exit: yml[i-1] contains the lower limit of the confidence interval for the regression line at x[i-1] .
10: ymu[n] double Output
On exit: ymu[i-1] contains the upper limit of the confidence interval for the regression line at x[i-1] .
11: yl[n] double Output
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] double Output
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] double Output
On exit: the leverage of each observation on the regression.
14: res[n] double Output
On exit: the residuals of the regression.
15: rms double * Output
On exit: the residual mean square about the regression.
16: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

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 Parallelism and Performance

g02cbc is not threaded in any implementation.

9 Further Comments

None.

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

10.1 Program Text

Program Text (g02cbce.c)

10.2 Program Data

Program Data (g02cbce.d)

10.3 Program Results

Program Results (g02cbce.r)