PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_correg_linregm_fit (g02da)
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
where
- is a vector of observations on the dependent variable,
- is an by matrix of the independent variables of column rank ,
- is a vector of length of unknown arguments, and
- is a vector of length of unknown random errors such that , where is a known diagonal matrix.
If , the identity matrix, then least squares estimation is used. If , then for a given weight matrix , weighted least squares estimation is used.
The least squares estimates of the arguments minimize while the weighted least squares estimates minimize .
nag_correg_linregm_fit (g02da) finds a
decomposition of
(or
in weighted case), i.e.,
where
and
is a
by
upper triangular matrix and
is an
by
orthogonal matrix. If
is of full rank, then
is the solution to
where
(or
) and
is the first
elements of
. If
is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of
,
where
is a
by
diagonal matrix with nonzero diagonal elements,
being the rank of
, and
and
are
by
orthogonal matrices. This gives the solution
being the first
columns of
, i.e.,
, and
being the first
columns of
.
Details of the SVD, are made available, in the form of the matrix
:
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, , where are the fitted values. The fitted values can be written as for an by matrix . The th diagonal elements of , , give a measure of the influence of the th values of the independent variables on the fitted regression model. The values are sometimes known as leverages. Both and 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, , 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., , for . 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
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:
– string (length ≥ 1)
-
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:
or .
- 2:
– double array
-
ldx, the first dimension of the array, must satisfy the constraint
.
must contain the th observation for the th independent variable, for and .
- 3:
– int64int32nag_int array
-
Indicates which independent variables are to be included in the model.
- The variable contained in the th column of x is included in the regression model.
Constraints:
- , for ;
- if , exactly values of isx must be ;
- if , exactly ip values of isx must be .
- 4:
– int64int32nag_int scalar
-
The number of independent variables in the model, including the mean or intercept if present.
Constraints:
- if , ;
- if , ;
- otherwise .
- 5:
– double array
-
, the observations on the dependent variable.
Optional Input Parameters
- 1:
– 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.)
, the number of observations.
Constraint:
.
- 2:
– 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.)
, the total number of independent variables in the dataset.
Constraint:
.
- 3:
– double array
-
The dimension of the array
wt
must be at least
if
, and at least
otherwise
If If provided,
wt must contain the weights to be used in the weighted regression.
If
, the
th 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
.
Constraint:
if , , for .
- 4:
– double scalar
Default:
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
, 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:
.
Output Parameters
-
The residual sum of squares for the regression.
- 2:
– int64int32nag_int scalar
-
The degrees of freedom associated with the residual sum of squares.
- 3:
– double array
-
,
contains the least squares estimates of the parameters of the regression model,
.
If
,
will contain the estimate of the mean parameter and
will contain the coefficient of the variable contained in column
of
x, where
is the
th positive value in the array
isx.
If
,
will contain the coefficient of the variable contained in column
of
x, where
is the
th positive value in the array
isx.
- 4:
– double array
-
,
contains the standard errors of the
ip parameter estimates given in
b.
- 5:
– double array
-
The first
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
and the parameter estimate given in
,
, is stored in
.
- 6:
– double array
-
The (weighted) residuals,
, for .
- 7:
– double array
-
The diagonal elements of ,
, for .
- 8:
– double array
-
The results of the
decomposition:
- the first column of q contains ;
- the upper triangular part of columns to contain the matrix;
- the strictly lower triangular part of columns to contain details of the matrix.
- 9:
– logical scalar
-
If a singular value decomposition has been performed then
svd will be
true, otherwise
svd will be
false.
- 10:
– int64int32nag_int scalar
-
The rank of the independent variables.
If , .
If
,
irank is an estimate of the rank of the independent variables.
irank is calculated as the number of singular values greater that
(largest singular value). It is possible for the SVD to be carried out but
irank to be returned as
ip.
- 11:
– double array
-
Details of the
decomposition and SVD if used.
If
, only the first
ip elements of
p are used these will contain the zeta values for the
decomposition (see
nag_lapack_dgeqrf (f08ae) for details).
If
, the first
ip elements of
p will contain the zeta values for the
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
stored by columns.
- 12:
– double array
-
If on exit
,
wk contains information which is needed by
nag_correg_linregm_fit_newvar (g02dg); otherwise
wk is used as workspace.
- 13:
– int64int32nag_int scalar
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.
-
-
On entry, | , |
or | , |
or | , |
or | , |
or | , |
or | , |
or | . |
-
-
On entry, | or , |
or | or . |
-
-
On entry, | and a value of . |
-
-
On entry, | a value of , |
or | the value of ip is incompatible with the values of mean_p and isx, |
or | ip is greater than the effective number of observations. |
- W
-
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 , but neither standard errors nor the variance-covariance matrix will be calculated.
-
-
The singular value decomposition has failed to converge, see
nag_eigen_real_triang_svd (f02wu). This is an unlikely error.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
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 (
) 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 (
), adjusted
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 (
), adjusted
and Akaike's information criteria (AIC) are calculated.
Open in the MATLAB editor:
g02da_example
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);
[rss, idf, b, se, covar, res, h, q, svd, irank, p, wk, ifail] = ...
g02da(mean_p, x, isx, ip, y);
[sw, wmean, c, ifail] = g02bu(y);
en = double(idf + irank);
rsq = 1 - rss/c(1);
mult = (en-1)/double(idf);
arsq = 1 - mult*(1-rsq);
aic = en*log(rss/en) + 2*double(irank);
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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015