hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_correg_linregm_fit (g02da)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_correg_linregm_fit (g02da) 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_correg_linregm_fit (g02da) may be used to perform a weighted regression.

Syntax

[rss, idf, b, se, covar, res, h, q, svd, irank, p, wk, ifail] = g02da(mean_p, x, isx, ip, y, 'n', n, 'm', m, 'wt', wt, 'tol', tol)
[rss, idf, b, se, covar, res, h, q, svd, irank, p, wk, ifail] = nag_correg_linregm_fit(mean_p, x, isx, ip, y, 'n', n, 'm', m, 'wt', wt, 'tol', tol)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 23: weight was removed from the interface; wt was made optional

Description

The general linear regression model is defined by
y=Xβ+ε,  
where
If V=I, the identity matrix, then least squares estimation is used. If VI, then for a given weight matrix WV-1, weighted least squares estimation is used.
The least squares estimates β^ of the arguments β minimize y-XβT y-Xβ  while the weighted least squares estimates minimize y-XβT Wy-Xβ .
nag_correg_linregm_fit (g02da) finds a QR decomposition of X (or W1/2X in weighted case), i.e.,
X=QR*or  W1/2X=QR*,  
where R*= R 0  and R is a p by p upper triangular matrix and Q is an n by n orthogonal matrix. If R is of full rank, then β^ is the solution to
Rβ^=c1,  
where c=QTy (or QTW1/2y) and c1 is the first p elements of c. If R is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of R,
R=Q* D 0 0 0 PT,  
where D is a k by k diagonal matrix with nonzero diagonal elements, k being the rank of R, and Q* and P are p by p orthogonal matrices. This gives the solution
β^=P1D-1Q*1Tc1,  
P1 being the first k columns of P, i.e., P=P1P0, and Q*1 being the first k columns of Q*.
Details of the SVD, are made available, in the form of the matrix P*:
P*= D-1 P1T P0T .  
This will be only one of the possible solutions. Other estimates may be obtained by applying constraints to the arguments. These solutions can be obtained by using nag_correg_linregm_constrain (g02dk) after using nag_correg_linregm_fit (g02da). Only certain linear combinations of the arguments will have unique estimates; these are known as estimable functions.
The fit of the model can be examined by considering the residuals, ri=yi-y^, where y^=Xβ^ are the fitted values. The fitted values can be written as Hy for an n by n matrix H. The ith diagonal elements of H, hi, give a measure of the influence of the ith values of the independent variables on the fitted regression model. The values hi are sometimes known as leverages. Both ri and hi are provided by nag_correg_linregm_fit (g02da).
The output of nag_correg_linregm_fit (g02da) also includes β^, the residual sum of squares and associated degrees of freedom, n-k, the standard errors of the parameter estimates and the variance-covariance matrix of the parameter estimates.
In many linear regression models the first term is taken as a mean term or an intercept, i.e., Xi,1=1, for i=1,2,,n. This is provided as an option. Also only some of the possible independent variables are required to be included in a model, a facility to select variables to be included in the model is provided.
Details of the QR decomposition and, if used, the SVD, are made available. These allow the regression to be updated by adding or deleting an observation using nag_correg_linregm_obs_edit (g02dc), adding or deleting a variable using nag_correg_linregm_var_add (g02de) and nag_correg_linregm_var_del (g02df) or estimating and testing an estimable function using nag_correg_linregm_estfunc (g02dn).

References

Cook R D and Weisberg S (1982) Residuals and Influence in Regression Chapman and Hall
Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
McCullagh P and Nelder J A (1983) Generalized Linear Models Chapman and Hall
Searle S R (1971) Linear Models Wiley

Parameters

Compulsory Input Parameters

1:     mean_p – string (length ≥ 1)
Indicates if a mean term is to be included.
mean_p='M'
A mean term, intercept, will be included in the model.
mean_p='Z'
The model will pass through the origin, zero-point.
Constraint: mean_p='M' or 'Z'.
2:     xldxm – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn.
xij must contain the ith observation for the jth independent variable, for i=1,2,,n and j=1,2,,m.
3:     isxm int64int32nag_int array
Indicates which independent variables are to be included in the model.
isxj>0
The variable contained in the jth column of x is included in the regression model.
Constraints:
  • isxj0, for j=1,2,,m;
  • if mean_p='M', exactly ip-1 values of isx must be >0;
  • if mean_p='Z', exactly ip values of isx must be >0.
4:     ip int64int32nag_int scalar
The number of independent variables in the model, including the mean or intercept if present.
Constraints:
  • if mean_p='M', 1ipm+1;
  • if mean_p='Z', 1ipm;
  • otherwise 1ipn.
5:     yn – double array
y, the observations on the dependent variable.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array y and the first dimension of the array x. (An error is raised if these dimensions are not equal.)
n, the number of observations.
Constraint: n2.
2:     m int64int32nag_int scalar
Default: the dimension of the array isx and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
m, the total number of independent variables in the dataset.
Constraint: m1.
3:     wt: – double array
The dimension of the array wt must be at least n if weight='W', and at least 1 otherwise
If If provided, wt must contain the weights to be used in the weighted regression.
If wti=0.0, the ith observation is not included in the model, in which case the effective number of observations is the number of observations with nonzero weights. The values of res and h will be set to zero for observations with zero weights.
If wt is not provided, the effective number of observations is n.
Constraint: if weight='W', wti0.0, for i=1,2,,n.
4:     tol – double scalar
Default: 0.000001
The value of tol is used to decide if the independent variables are of full rank and if not what is the rank of the independent variables. The smaller the value of tol the stricter the criterion for selecting the singular value decomposition. If tol=0.0, the singular value decomposition will never be used; this may cause run time errors or inaccurate results if the independent variables are not of full rank.
Constraint: tol0.0.

Output Parameters

1:     rss – double scalar
The residual sum of squares for the regression.
2:     idf int64int32nag_int scalar
The degrees of freedom associated with the residual sum of squares.
3:     bip – double array
bi, i=1,2,,ip contains the least squares estimates of the parameters of the regression model, β^.
If mean_p='M', b1 will contain the estimate of the mean parameter and bi+1 will contain the coefficient of the variable contained in column j of x, where isxj is the ith positive value in the array isx.
If mean_p='Z', bi will contain the coefficient of the variable contained in column j of x, where isxj is the ith positive value in the array isx.
4:     seip – double array
sei, i=1,2,,ip contains the standard errors of the ip parameter estimates given in b.
5:     covarip×ip+1/2 – double array
The first ip×ip+1/2 elements of covar contain the upper triangular part of the variance-covariance matrix of the ip parameter estimates given in b. They are stored packed by column, i.e., the covariance between the parameter estimate given in bi and the parameter estimate given in bj, ji, is stored in covarj×j-1/2+i.
6:     resn – double array
The (weighted) residuals, ri, for i=1,2,,n.
7:     hn – double array
The diagonal elements of H, hi, for i=1,2,,n.
8:     qldqip+1 – double array
The results of the QR decomposition:
  • the first column of q contains c;
  • the upper triangular part of columns 2 to ip+1 contain the R matrix;
  • the strictly lower triangular part of columns 2 to ip+1 contain details of the Q matrix.
9:     svd – logical scalar
If a singular value decomposition has been performed then svd will be true, otherwise svd will be false.
10:   irank int64int32nag_int scalar
The rank of the independent variables.
If svd=false, irank=ip.
If svd=true, irank is an estimate of the rank of the independent variables.
irank is calculated as the number of singular values greater that tol× (largest singular value). It is possible for the SVD to be carried out but irank to be returned as ip.
11:   p2×ip+ip×ip – double array
Details of the QR decomposition and SVD if used.
If svd=false, only the first ip elements of p are used these will contain the zeta values for the QR decomposition (see nag_lapack_dgeqrf (f08ae) for details).
If svd=true, the first ip elements of p will contain the zeta values for the QR decomposition (see nag_lapack_dgeqrf (f08ae) for details) and the next ip elements of p contain singular values. The following ip by ip elements contain the matrix P* stored by columns.
12:   wkmax2,5×ip-1+ip×ip – double array
If on exit svd=true, wk contains information which is needed by nag_correg_linregm_fit_newvar (g02dg); otherwise wk is used as workspace.
13:   ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

   ifail=1
On entry,n<2,
orm<1,
orldx<n,
orldq<n,
ortol<0.0,
orip0,
orip>n.
   ifail=2
On entry,mean_p'M' or 'Z',
orweight'W' or 'U'.
   ifail=3
On entry,weight='W' and a value of wt<0.0.
   ifail=4
On entry,a value of isx<0,
orthe value of ip is incompatible with the values of mean_p and isx,
orip is greater than the effective number of observations.
W  ifail=5
The degrees of freedom for the residuals are zero, i.e., the designated number of arguments is equal to 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.
   ifail=6
The singular value decomposition has failed to converge, see nag_eigen_real_triang_svd (f02wu). This is an unlikely error.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The accuracy of nag_correg_linregm_fit (g02da) is closely related to the accuracy of nag_eigen_real_triang_svd (f02wu) and nag_lapack_dgeqrf (f08ae). These function documents should be consulted.

Further Comments

Standardized residuals and further measures of influence can be computed using nag_correg_linregm_stat_resinf (g02fa). nag_correg_linregm_stat_resinf (g02fa) requires, in particular, the results stored in res and h.

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. nag_correg_ssqmat (g02bu) is then called to calculate the total sums of squares and the coefficient of determination (R2), adjusted R2 and Akaike's information criteria (AIC) are calculated.
nag_correg_ssqmat (g02bu) is then called to calculate the total sums of squares and the coefficient of determination (R2), adjusted R2 and Akaike's information criteria (AIC) are calculated.
function g02da_example


fprintf('g02da example results\n\n');

x = [1, 0, 0, 0;
     0, 0, 0, 1;
     0, 1, 0, 0;
     0, 0, 1, 0;
     0, 0, 0, 1;
     0, 1, 0, 0;
     0, 0, 0, 1;
     1, 0, 0, 0;
     0, 0, 1, 0;
     1, 0, 0, 0;
     0, 0, 1, 0;
     0, 1, 0, 0];
y = [33.63;     39.62;     38.18;     41.46;     38.02;     35.83;
     35.99;     36.58;     42.92;     37.80;     40.43;     37.89];

[n,m]  = size(x);
isx    = ones(m,1,'int64');
mean_p = 'M';
ip     = int64(m+1);

% Fit general linear regression model
[rss, idf, b, se, covar, res, h, q, svd, irank, p, wk, ifail] = ...
  g02da(mean_p, x, isx, ip, y);

% Calculate total sums of squares about mean
[sw, wmean, c, ifail] = g02bu(y);

% Effective number of observations
en = double(idf + irank);
% Calculate R-squared, corrected R-squared and AIC
rsq = 1 - rss/c(1);
mult = (en-1)/double(idf);
arsq = 1 - mult*(1-rsq);
aic = en*log(rss/en) + 2*double(irank);

% Display results
if svd
  fprintf('Model not of full rank, rank = %4d\n\n', irank);
end
fprintf('Residual sum of squares = %12.4e\n', rss);
fprintf('Degrees of freedom      = %4d\n', idf);
fprintf('R-squared               = %12.4e\n', rsq);
fprintf('Adjusted R-squared      = %12.4e\n', arsq);
fprintf('AIC                     = %12.4e\n', aic);
fprintf('\nVariable   Parameter estimate   Standard error\n\n');
ivar = double([1:ip]');
fprintf('%6d%20.4e%20.4e\n',[ivar b se]');
fprintf('\n   Obs          Residuals              H\n\n');
ivar = double([1:n]');
fprintf('%6d%20.4e%20.4e\n',[ivar res h]');


g02da example results

Model not of full rank, rank =    4

Residual sum of squares =   2.2227e+01
Degrees of freedom      =    8
R-squared               =   7.0042e-01
Adjusted R-squared      =   5.8808e-01
AIC                     =   1.5397e+01

Variable   Parameter estimate   Standard error

     1          3.0557e+01          3.8494e-01
     2          5.4467e+00          8.3896e-01
     3          6.7433e+00          8.3896e-01
     4          1.1047e+01          8.3896e-01
     5          7.3200e+00          8.3896e-01

   Obs          Residuals              H

     1         -2.3733e+00          3.3333e-01
     2          1.7433e+00          3.3333e-01
     3          8.8000e-01          3.3333e-01
     4         -1.4333e-01          3.3333e-01
     5          1.4333e-01          3.3333e-01
     6         -1.4700e+00          3.3333e-01
     7         -1.8867e+00          3.3333e-01
     8          5.7667e-01          3.3333e-01
     9          1.3167e+00          3.3333e-01
    10          1.7967e+00          3.3333e-01
    11         -1.1733e+00          3.3333e-01
    12          5.9000e-01          3.3333e-01

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015