NAG Library Routine Document
g02daf
(linregm_fit)
1
Purpose
g02daf performs a general multiple linear regression when the independent variables may be linearly dependent. Parameter estimates, standard errors, residuals and influence statistics are computed. g02daf may be used to perform a weighted regression.
2
Specification
Fortran Interface
Subroutine g02daf ( |
mean,
weight,
n,
x,
ldx,
m,
isx,
ip,
y,
wt,
rss,
idf,
b,
se,
cov,
res,
h,
q,
ldq,
svd,
irank,
p,
tol,
wk,
ifail) |
Integer, Intent (In) | :: |
n,
ldx,
m,
isx(m),
ip,
ldq | Integer, Intent (Inout) | :: |
ifail | Integer, Intent (Out) | :: |
idf,
irank | Real (Kind=nag_wp), Intent (In) | :: |
x(ldx,m),
y(n),
wt(*),
tol | Real (Kind=nag_wp), Intent (Inout) | :: |
q(ldq,ip+1) | Real (Kind=nag_wp), Intent (Out) | :: |
rss,
b(ip),
se(ip),
cov(ip*(ip+1)/2),
res(n),
h(n),
p(2*ip+ip*ip),
wk(max(2,5*(ip-1)+ip*ip)) | Logical, Intent (Out) | :: |
svd | Character (1), Intent (In) | :: |
mean,
weight |
|
C Header Interface
#include nagmk26.h
void |
g02daf_ (
const char *mean,
const char *weight,
const Integer *n,
const double x[],
const Integer *ldx,
const Integer *m,
const Integer isx[],
const Integer *ip,
const double y[],
const double wt[],
double *rss,
Integer *idf,
double b[],
double se[],
double cov[],
double res[],
double h[],
double q[],
const Integer *ldq,
logical *svd,
Integer *irank,
double p[],
const double *tol,
double wk[],
Integer *ifail,
const Charlen length_mean,
const Charlen length_weight) |
|
3
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 .
g02daf 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
g02dkf after using
g02daf. 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 g02daf.
The output of g02daf 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
g02dcf, adding or deleting a variable using
g02def and
g02dff or estimating and testing an estimable function using
g02dnf.
4
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
5
Arguments
- 1: – Character(1)Input
-
On entry: 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: – Character(1)Input
-
On entry: indicates if weights are to be used.
- Least squares estimation is used.
- Weighted least squares is used and weights must be supplied in array wt.
Constraint:
or .
- 3: – IntegerInput
-
On entry: , the number of observations.
Constraint:
.
- 4: – Real (Kind=nag_wp) arrayInput
-
On entry: must contain the th observation for the th independent variable, for and .
- 5: – IntegerInput
-
On entry: the first dimension of the array
x as declared in the (sub)program from which
g02daf is called.
Constraint:
.
- 6: – IntegerInput
-
On entry: , the total number of independent variables in the dataset.
Constraint:
.
- 7: – Integer arrayInput
-
On entry: 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 .
- 8: – IntegerInput
-
On entry: the number of independent variables in the model, including the mean or intercept if present.
Constraints:
- if , ;
- if , ;
- otherwise .
- 9: – Real (Kind=nag_wp) arrayInput
-
On entry: , the observations on the dependent variable.
- 10: – Real (Kind=nag_wp) arrayInput
-
Note: the dimension of the array
wt
must be at least
if
, and at least
otherwise.
On entry: if
,
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 referenced and the effective number of observations is
.
Constraint:
if , , for .
-
On exit: the residual sum of squares for the regression.
- 12: – IntegerOutput
-
On exit: the degrees of freedom associated with the residual sum of squares.
- 13: – Real (Kind=nag_wp) arrayOutput
-
On exit:
,
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.
- 14: – Real (Kind=nag_wp) arrayOutput
-
On exit:
,
contains the standard errors of the
ip parameter estimates given in
b.
- 15: – Real (Kind=nag_wp) arrayOutput
-
On exit: the first
elements of
cov 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
.
- 16: – Real (Kind=nag_wp) arrayOutput
-
On exit: the (weighted) residuals,
, for .
- 17: – Real (Kind=nag_wp) arrayOutput
-
On exit: the diagonal elements of ,
, for .
- 18: – Real (Kind=nag_wp) arrayOutput
-
On exit: 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.
- 19: – IntegerInput
-
On entry: the first dimension of the array
q as declared in the (sub)program from which
g02daf is called.
Constraint:
.
- 20: – LogicalOutput
-
On exit: if a singular value decomposition has been performed then
svd will be .TRUE., otherwise
svd will be .FALSE..
- 21: – IntegerOutput
-
On exit: 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.
- 22: – Real (Kind=nag_wp) arrayOutput
-
On exit: 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
f08aef (dgeqrf) for details).
If
, the first
ip elements of
p will contain the zeta values for the
decomposition (see
f08aef (dgeqrf) for details) and the next
ip elements of
p contain singular values. The following
ip by
ip elements contain the matrix
stored by columns.
- 23: – Real (Kind=nag_wp)Input
-
On entry: 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.
Suggested value:
.
Constraint:
.
- 24: – Real (Kind=nag_wp) arrayOutput
-
On exit: if on exit
,
wk contains information which is needed by
g02dgf; otherwise
wk is used as workspace.
- 25: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: .
On entry, and .
Constraint: .
On entry, and .
Constraint: .
On entry, and .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: .
-
On entry, .
Constraint: or .
On entry, .
Constraint: or .
-
On entry, .
Constraint: , for .
-
On entry,
.
Constraint:
ip must be compatible with the number of nonzero elements in
isx.
On entry, .
Constraint: , for .
-
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.
-
SVD solution failed to converge.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
The accuracy of
g02daf is closely related to the accuracy of
f02wuf and
f08aef (dgeqrf). These routine documents should be consulted.
8
Parallelism and Performance
g02daf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02daf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
Standardized residuals and further measures of influence can be computed using
g02faf.
g02faf requires, in particular, the results stored in
res and
h.
10
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.
g02buf is then called to calculate the total sums of squares and the coefficient of determination (
), adjusted
and Akaike's information criteria (AIC) are calculated.
g02buf is then called to calculate the total sums of squares and the coefficient of determination (
), adjusted
and Akaike's information criteria (AIC) are calculated.
10.1
Program Text
Program Text (g02dafe.f90)
10.2
Program Data
Program Data (g02dafe.d)
10.3
Program Results
Program Results (g02dafe.r)