# NAG Library Routine Document

## 1Purpose

g02haf performs bounded influence regression ($M$-estimates). Several standard methods are available.

## 2Specification

Fortran Interface
 Subroutine g02haf ( indw, ipsi, indc, n, m, x, ldx, y, cpsi, h1, h2, h3, cucv, dchi, c, ldc, rs, wgt, tol, stat,
 Integer, Intent (In) :: indw, ipsi, isigma, indc, n, m, ldx, ldc, maxit, nitmon Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: cpsi, h1, h2, h3, cucv, dchi, tol Real (Kind=nag_wp), Intent (Inout) :: x(ldx,m), y(n), theta(m), sigma, c(ldc,m) Real (Kind=nag_wp), Intent (Out) :: rs(n), wgt(n), stat(4)
#include nagmk26.h
 void g02haf_ (const Integer *indw, const Integer *ipsi, const Integer *isigma, const Integer *indc, const Integer *n, const Integer *m, double x[], const Integer *ldx, double y[], const double *cpsi, const double *h1, const double *h2, const double *h3, const double *cucv, const double *dchi, double theta[], double *sigma, double c[], const Integer *ldc, double rs[], double wgt[], const double *tol, const Integer *maxit, const Integer *nitmon, double stat[], Integer *ifail)

## 3Description

For the linear regression model
 $y = Xθ+ε ,$
 where $y$ is a vector of length $n$ of the dependent variable, $X$ is a $n$ by $m$ matrix of independent variables of column rank $k$, $\theta$ is a vector of length $m$ of unknown arguments, and $\epsilon$ is a vector of length $n$ of unknown errors with $\mathrm{var}\left({\epsilon }_{i}\right)={\sigma }^{2}$,
g02haf calculates the M-estimates given by the solution, $\stackrel{^}{\theta }$, to the equation
 $∑ i=1 n ψ ri / σwi wi xij = 0 , j=1,2,…,m ,$ (1)
where ${r}_{i}$ is the $i$th residual, i.e., the $i$th element of $r=y-X\stackrel{^}{\theta }$,
$\psi$ is a suitable weight function,
${w}_{i}$ are suitable weights,
and $\sigma$ may be estimated at each iteration by the median absolute deviation of the residuals
 $σ^ = medi⁡ ri / β1$
or as the solution to
 $∑ i= 1 n χ ri / σ ^ wi wi2 = n-k β2$
for suitable weight function $\chi$, where ${\beta }_{1}$ and ${\beta }_{2}$ are constants, chosen so that the estimator of $\sigma$ is asymptotically unbiased if the errors, ${\epsilon }_{i}$, have a Normal distribution. Alternatively $\sigma$ may be held at a constant value.
The above describes the Schweppe type regression. If the ${w}_{i}$ are assumed to equal $1$ for all $i$ then Huber type regression is obtained. A third type, due to Mallows, replaces (1) by
 $∑ i=1 n ψ ri / σ wi xij = 0 , j=1,2,…,m .$
This may be obtained by use of the transformations
 $wi*←wi yi*←yi wi xij*←xij wi, j= 1,2,…,m$
(see Section 3 of Marazzi (1987a)).
For Huber and Schweppe type regressions, ${\beta }_{1}$ is the 75th percentile of the standard Normal distribution. For Mallows type regression ${\beta }_{1}$ is the solution to
 $1n ∑ i=1 n Φ β1 / wi = 0.75 ,$
where $\Phi$ is the standard Normal cumulative distribution function (see s15abf).
${\beta }_{2}$ is given by
 $β2 = ∫ -∞ ∞ χz ϕz dz in the Huber case; β2 = 1 n ∑ i=1 n wi ∫ -∞ ∞ χz ϕz dz in the Mallows case; β2 = 1n ∑ i=1 n wi2 ∫ -∞ ∞ χ z/wi ϕz dz in the Schweppe case;$
where $\varphi$ is the standard Normal density, i.e., $\frac{1}{\sqrt{2\pi }}\mathrm{exp}\left(-\frac{1}{2}{x}^{2}\right)\text{.}$
The calculation of the estimates of $\theta$ can be formulated as an iterative weighted least squares problem with a diagonal weight matrix $G$ given by
 $Gii = ψ ri / σwi ri / σwi , ri≠0 ψ′ 0 , ri=0 ,$
where ${\psi }^{\prime }\left(t\right)$ is the derivative of $\psi$ at the point $t$.
The value of $\theta$ at each iteration is given by the weighted least squares regression of $y$ on $X$. This is carried out by first transforming the $y$ and $X$ by
 $y~i=yiGii x~ij=xijGii, j=1,2,…,m$
and then using f04jgf. If $X$ is of full column rank then an orthogonal-triangular ($QR$) decomposition is used; if not, a singular value decomposition is used.
The following functions are available for $\psi$ and $\chi$ in g02haf.
(a) Unit Weights
 $ψt = t , χt = t22 .$
This gives least squares regression.
(b) Huber's Function
 $ψt = max-c,minc,t , χt = t2 2 , t≤d d2 2 , t>d$
(c) Hampel's Piecewise Linear Function
 $ψ h1 , h2 , h3 t = - ψ h1 , h2 , h3 -t = t, 0≤t≤ h1 h1, h1 ≤t≤ h2 h1 h3 - t / h3 - h2 , h2 ≤t≤ h3 0, h3
 $χt = t2 2 , t≤d d2 2 , t>d$
(d) Andrew's Sine Wave Function
 $ψt = sin⁡t, -π≤t≤π 0, t>π χt = t2 2 , t≤d d2 2 , t>d$
(e) Tukey's Bi-weight
 $ψt = t 1 - t2 2 , t≤ 1 0, t> 1 χt = t2 2 , t≤d d2 2 , t>d$
where $c$, ${h}_{1}$, ${h}_{2}$, ${h}_{3}$, and $d$ are given constants.
Several schemes for calculating weights have been proposed, see Hampel et al. (1986) and Marazzi (1987a). As the different independent variables may be measured on different scales, one group of proposed weights aims to bound a standardized measure of influence. To obtain such weights the matrix $A$ has to be found such that:
 $1n ∑ i=1 n u zi2 zi ziT = I$
and
 $zi = Axi ,$
 where ${x}_{i}$ is a vector of length $m$ containing the $i$th row of $X$, $A$ is an $m$ by $m$ lower triangular matrix, and $u$ is a suitable function.
The weights are then calculated as
 $wi = f zi2$
for a suitable function $f$.
g02haf finds $A$ using the iterative procedure
 $Ak = Sk+I Ak-1 ,$
where ${S}_{k}=\left({s}_{jl}\right)$,
 $sjl = - minmax h jl / n ,-BL,BL , j>l - minmax 12 hjj/n-1 ,-BD,BD , j=l$
and
 $hjl = ∑ i= 1 n u zi2 zij zil$
and $BL$ and $BD$ are bounds set at $0.9$.
Two weights are available in g02haf:
 $u t = g1 ct ,$
 where ${g}_{1}\left(t\right)={t}^{2}+\left(1-{t}^{2}\right)\left(2\Phi \left(t\right)-1\right)-2t\varphi \left(t\right)$, $\Phi \left(t\right)$ is the standard Normal cumulative distribution function, $\varphi \left(t\right)$ is the standard Normal probability density function, and $f\left(t\right)=\frac{1}{t}$.
These are for use with Schweppe type regression.
(ii) Maronna's Proposed Weights
 $ut = c t2 t>c 1 t≤c ft=ut .$
These are for use with Mallows type regression.
Finally the asymptotic variance-covariance matrix, $C$, of the estimates $\theta$ is calculated.
For Huber type regression
 $C = fH XTX -1 σ^2 ,$
where
 $fH = 1 n-m ∑ i= 1 n ψ2 ri / σ ^ 1n ∑ i= 1 n ψ′ ri σ^ 2 κ2$
 $κ2 = 1 + mn 1n ∑ i=1 n ψ′ ri / σ^ - 1n ∑ i=1 n ψ′ ri / σ^ 2 1n ∑ i=1 n ψ′ ri σ^ 2 .$
See Huber (1981) and Marazzi (1987b).
For Mallows and Schweppe type regressions $C$ is of the form
 $σ^n 2 S1-1 S2 S1-1 ,$
where ${S}_{1}=\frac{1}{n}{X}^{\mathrm{T}}DX$ and ${S}_{2}=\frac{1}{n}{X}^{\mathrm{T}}PX$.
$D$ is a diagonal matrix such that the $i$th element approximates $E\left({\psi }^{\prime }\left({r}_{i}/\left(\sigma {w}_{i}\right)\right)\right)$ in the Schweppe case and $E\left({\psi }^{\prime }\left({r}_{i}/\sigma \right){w}_{i}\right)$ in the Mallows case.
$P$ is a diagonal matrix such that the $i$th element approximates $E\left({\psi }^{2}\left({r}_{i}/\left(\sigma {w}_{i}\right)\right){w}_{i}^{2}\right)$ in the Schweppe case and $E\left({\psi }^{2}\left({r}_{i}/\sigma \right){w}_{i}^{2}\right)$ in the Mallows case.
Two approximations are available in g02haf:
1. Average over the ${r}_{i}$
 $Schweppe Mallows Di = 1n ∑ j=1 n ψ′ rj σ^ wi wi Di = 1n ∑ j=1 n ψ′ rj σ^ wi Pi = 1n ∑ j=1 n ψ2 rj σ^ wi wi2 Pi = 1n ∑ j=1 n ψ2 rj σ^ wi2$
2. Replace expected value by observed
 $Schweppe Mallows Di = ψ′ ri σ^ wi wi Di = ψ′ ri σ ^ wi Pi = ψ2 ri σ ^ wi wi2 Pi = ψ2 ri σ ^ wi2 .$
Note:  there is no explicit provision in the routine for a constant term in the regression model. However, the addition of a dummy variable whose value is $1.0$ for all observations will produce a value of $\stackrel{^}{\theta }$ corresponding to the usual constant term.
g02haf is based on routines in ROBETH; see Marazzi (1987a).

## 4References

Hampel F R, Ronchetti E M, Rousseeuw P J and Stahel W A (1986) Robust Statistics. The Approach Based on Influence Functions Wiley
Huber P J (1981) Robust Statistics Wiley
Marazzi A (1987a) Weights for bounded influence regression in ROBETH Cah. Rech. Doc. IUMSP, No. 3 ROB 3 Institut Universitaire de Médecine Sociale et Préventive, Lausanne
Marazzi A (1987b) Subroutines for robust and bounded influence regression in ROBETH Cah. Rech. Doc. IUMSP, No. 3 ROB 2 Institut Universitaire de Médecine Sociale et Préventive, Lausanne

## 5Arguments

1:     $\mathbf{indw}$ – IntegerInput
On entry: specifies the type of regression to be performed.
${\mathbf{indw}}<0$
Mallows type regression with Maronna's proposed weights.
${\mathbf{indw}}=0$
Huber type regression.
${\mathbf{indw}}>0$
Schweppe type regression with Krasker–Welsch weights.
2:     $\mathbf{ipsi}$ – IntegerInput
On entry: specifies which $\psi$ function is to be used.
${\mathbf{ipsi}}=0$
$\psi \left(t\right)=t$, i.e., least squares.
${\mathbf{ipsi}}=1$
Huber's function.
${\mathbf{ipsi}}=2$
Hampel's piecewise linear function.
${\mathbf{ipsi}}=3$
Andrew's sine wave.
${\mathbf{ipsi}}=4$
Tukey's bi-weight.
Constraint: ${\mathbf{ipsi}}=0$, $1$, $2$, $3$ or $4$.
3:     $\mathbf{isigma}$ – IntegerInput
On entry: specifies how $\sigma$ is to be estimated.
${\mathbf{isigma}}<0$
$\sigma$ is estimated by median absolute deviation of residuals.
${\mathbf{isigma}}=0$
$\sigma$ is held constant at its initial value.
${\mathbf{isigma}}>0$
$\sigma$ is estimated using the $\chi$ function.
4:     $\mathbf{indc}$ – IntegerInput
On entry: if ${\mathbf{indw}}\ne 0$, indc specifies the approximations used in estimating the covariance matrix of $\stackrel{^}{\theta }$.
${\mathbf{indc}}=1$
Averaging over residuals.
${\mathbf{indc}}\ne 1$
Replacing expected by observed.
${\mathbf{indw}}=0$
indc is not referenced.
5:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
6:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of independent variables.
Constraint: $1\le {\mathbf{m}}<{\mathbf{n}}$.
7:     $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: the values of the $X$ matrix, i.e., the independent variables. ${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}\mathit{j}$th element of $X$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
If ${\mathbf{indw}}<0$, then during calculations the elements of x will be transformed as described in Section 3. Before exit the inverse transformation will be applied. As a result there may be slight differences between the input x and the output x.
On exit: unchanged, except as described above.
8:     $\mathbf{ldx}$ – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g02haf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
9:     $\mathbf{y}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: the data values of the dependent variable.
${\mathbf{y}}\left(\mathit{i}\right)$ must contain the value of $y$ for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{indw}}<0$, then during calculations the elements of y will be transformed as described in Section 3. Before exit the inverse transformation will be applied. As a result there may be slight differences between the input y and the output y.
On exit: unchanged, except as described above.
10:   $\mathbf{cpsi}$ – Real (Kind=nag_wp)Input
On entry: if ${\mathbf{ipsi}}=1$, cpsi must specify the argument, $c$, of Huber's $\psi$ function.
If ${\mathbf{ipsi}}\ne 1$ on entry, cpsi is not referenced.
Constraint: if ${\mathbf{cpsi}}>0.0$, ${\mathbf{ipsi}}=1$.
11:   $\mathbf{h1}$ – Real (Kind=nag_wp)Input
12:   $\mathbf{h2}$ – Real (Kind=nag_wp)Input
13:   $\mathbf{h3}$ – Real (Kind=nag_wp)Input
On entry: if ${\mathbf{ipsi}}=2$, h1, h2, and h3 must specify the arguments ${h}_{1}$, ${h}_{2}$, and ${h}_{3}$, of Hampel's piecewise linear $\psi$ function. h1, h2, and h3 are not referenced if ${\mathbf{ipsi}}\ne 2$.
Constraint: if ${\mathbf{ipsi}}=2$, $0.0\le {\mathbf{h1}}\le {\mathbf{h2}}\le {\mathbf{h3}}$ and ${\mathbf{h3}}>0.0$.
14:   $\mathbf{cucv}$ – Real (Kind=nag_wp)Input
On entry: if ${\mathbf{indw}}<0$, must specify the value of the constant, $c$, of the function $u$ for Maronna's proposed weights.
If ${\mathbf{indw}}>0$, must specify the value of the function $u$ for the Krasker–Welsch weights.
If ${\mathbf{indw}}=0$, is not referenced.
Constraints:
• if ${\mathbf{indw}}<0$, ${\mathbf{cucv}}\ge {\mathbf{m}}$;
• if ${\mathbf{indw}}>0$, ${\mathbf{cucv}}\ge \sqrt{{\mathbf{m}}}$.
15:   $\mathbf{dchi}$ – Real (Kind=nag_wp)Input
On entry: $d$, the constant of the $\chi$ function. dchi is not referenced if ${\mathbf{ipsi}}=0$, or if ${\mathbf{isigma}}\le 0$.
Constraint: if ${\mathbf{ipsi}}\ne 0$ and ${\mathbf{isigma}}>0$, ${\mathbf{dchi}}>0.0$.
16:   $\mathbf{theta}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: starting values of the argument vector $\theta$. These may be obtained from least squares regression. Alternatively if ${\mathbf{isigma}}<0$ and ${\mathbf{sigma}}=1$ or if ${\mathbf{isigma}}>0$ and sigma approximately equals the standard deviation of the dependent variable, $y$, then ${\mathbf{theta}}\left(\mathit{i}\right)=0.0$, for $\mathit{i}=1,2,\dots ,m$ may provide reasonable starting values.
On exit: ${\mathbf{theta}}\left(\mathit{i}\right)$ contains the M-estimate of ${\theta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
17:   $\mathbf{sigma}$ – Real (Kind=nag_wp)Input/Output
On entry: a starting value for the estimation of $\sigma$. sigma should be approximately the standard deviation of the residuals from the model evaluated at the value of $\theta$ given by theta on entry.
Constraint: ${\mathbf{sigma}}>0.0$.
On exit: contains the final estimate of $\sigma$ if ${\mathbf{isigma}}\ne 0$ or the value assigned on entry if ${\mathbf{isigma}}=0$.
18:   $\mathbf{c}\left({\mathbf{ldc}},{\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the diagonal elements of c contain the estimated asymptotic standard errors of the estimates of $\theta$, i.e., ${\mathbf{c}}\left(i,i\right)$ contains the estimated asymptotic standard error of the estimate contained in ${\mathbf{theta}}\left(i\right)$.
The elements above the diagonal contain the estimated asymptotic correlation between the estimates of $\theta$, i.e., ${\mathbf{c}}\left(i,j\right)$, $1\le i contains the asymptotic correlation between the estimates contained in ${\mathbf{theta}}\left(i\right)$ and ${\mathbf{theta}}\left(j\right)$.
The elements below the diagonal contain the estimated asymptotic covariance between the estimates of $\theta$, i.e., ${\mathbf{c}}\left(i,j\right)$, $1\le j contains the estimated asymptotic covariance between the estimates contained in ${\mathbf{theta}}\left(i\right)$ and ${\mathbf{theta}}\left(j\right)$.
19:   $\mathbf{ldc}$ – IntegerInput
On entry: the first dimension of the array c as declared in the (sub)program from which g02haf is called.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{m}}$.
20:   $\mathbf{rs}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the residuals from the model evaluated at final value of theta, i.e., rs contains the vector $\left(y-X\stackrel{^}{\theta }\right)$.
21:   $\mathbf{wgt}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the vector of weights. ${\mathbf{wgt}}\left(\mathit{i}\right)$ contains the weight for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
22:   $\mathbf{tol}$ – Real (Kind=nag_wp)Input
On entry: the relative precision for the calculation of $A$ (if ${\mathbf{indw}}\ne 0$), the estimates of $\theta$ and the estimate of $\sigma$ (if ${\mathbf{isigma}}\ne 0$). Convergence is assumed when the relative change in all elements being considered is less than tol.
If ${\mathbf{indw}}<0$ and ${\mathbf{isigma}}<0$, tol is also used to determine the precision of ${\beta }_{1}$.
It is advisable for tol to be greater than .
Constraint: ${\mathbf{tol}}>0.0$.
23:   $\mathbf{maxit}$ – IntegerInput
On entry: the maximum number of iterations that should be used in the calculation of $A$ (if ${\mathbf{indw}}\ne 0$), and of the estimates of $\theta$ and $\sigma$, and of ${\beta }_{1}$ (if ${\mathbf{indw}}<0$ and ${\mathbf{isigma}}<0$).
A value of ${\mathbf{maxit}}=50$ should be adequate for most uses.
Constraint: ${\mathbf{maxit}}>0$.
24:   $\mathbf{nitmon}$ – IntegerInput
On entry: the amount of information that is printed on each iteration.
${\mathbf{nitmon}}=0$
No information is printed.
${\mathbf{nitmon}}\ne 0$
The current estimate of $\theta$, the change in $\theta$ during the current iteration and the current value of $\sigma$ are printed on the first and every $\mathrm{abs}\left({\mathbf{nitmon}}\right)$ iterations.
Also, if ${\mathbf{indw}}\ne 0$ and ${\mathbf{nitmon}}>0$ then information on the iterations to calculate $A$ is printed. This is the current estimate of $A$ and the maximum value of ${S}_{ij}$ (see Section 3).
When printing occurs the output is directed to the current advisory message unit (see x04abf).
25:   $\mathbf{stat}\left(4\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the following values are assigned to stat:
• ${\mathbf{stat}}\left(1\right)={\beta }_{1}$ if ${\mathbf{isigma}}<0$, or ${\mathbf{stat}}\left(1\right)={\beta }_{2}$ if ${\mathbf{isigma}}>0$.
• ${\mathbf{stat}}\left(2\right)=\text{}$ number of iterations used to calculate $A$.
• ${\mathbf{stat}}\left(3\right)=\text{}$ number of iterations used to calculate final estimates of $\theta$ and $\sigma$.
• ${\mathbf{stat}}\left(4\right)=k$, the rank of the weighted least squares equations.
26:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. 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 $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Note: g02haf may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{ldc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>{\mathbf{m}}$.
${\mathbf{ifail}}=2$
On entry: ${\mathbf{ipsi}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ipsi}}=0$, $1$, $2$, $3$ or $4$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{cucv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{cucv}}\ge \sqrt{{\mathbf{m}}}$.
On entry, ${\mathbf{dchi}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{dchi}}>0.0$.
On entry, h1,h2,h3 incorrectly set.
On entry, ${\mathbf{sigma}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sigma}}>0.0$.
On entry: ${\mathbf{cpsi}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{cpsi}}>0.0$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{maxit}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxit}}>0$.
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=5$
The number of iterations required to calculate the weights exceeds maxit. (Only if ${\mathbf{indw}}\ne 0$.)
${\mathbf{ifail}}=6$
The number of iterations required to calculate ${\beta }_{1}$ exceeds maxit. (Only if ${\mathbf{indw}}<0$ and ${\mathbf{isigma}}<0$.)
${\mathbf{ifail}}=7$
Iterations to calculate estimates of theta failed to converge in maxit iterations: ${\mathbf{maxit}}=〈\mathit{\text{value}}〉$.
The number of iterations required to calculate $\theta$ and $\sigma$ exceeds maxit.
${\mathbf{ifail}}=8$
Weighted least squares equations not of full rank: rank $\text{}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=9$
Failure to invert matrix while calculating covariance.
If ${\mathbf{indw}}=0$ then $\left({X}^{\mathrm{T}}X\right)$ is almost singular.
If ${\mathbf{indw}}\ne 0$ then ${S}_{1}$ is singular or almost singular. This may be due to too many diagonal elements of the matrix being zero, see Section 9.
${\mathbf{ifail}}=10$
Factor for covariance matrix $\text{}=0$, uncorrected $\mathrm{INVERSE}{{\mathbf{x}}}^{\mathrm{T}}×{\mathbf{x}}$ given.
In calculating the correlation factor for the asymptotic variance-covariance matrix either the value of
 $1n ∑ i=1 n ψ′ ri / σ^ = 0 , or κ = 0 , or ∑ i=1 n ψ2 ri / σ^ = 0 .$
See Section 9. In this case c is returned as ${X}^{\mathrm{T}}X$.
(Only if ${\mathbf{indw}}=0$.)
${\mathbf{ifail}}=11$
Variance of an element of ${\mathbf{theta}}\le 0.0$, correlations set to $0$.
The estimated variance for an element of $\theta \le 0$.
In this case the diagonal element of c will contain the negative variance and the above diagonal elements in the row and column corresponding to the element will be returned as zero.
This error may be caused by rounding errors or too many of the diagonal elements of $P$ being zero, where $P$ is defined in Section 3. See Section 9.
${\mathbf{ifail}}=12$
Error degrees of freedom $n-k\le 0$, where $n=〈\mathit{\text{value}}〉$ and the rank of x, $k=〈\mathit{\text{value}}〉$.
Estimated value of sigma is zero.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The precision of the estimates is determined by tol. As a more stable method is used to calculate the estimates of $\theta$ than is used to calculate the covariance matrix, it is possible for the least squares equations to be of full rank but the $\left({X}^{\mathrm{T}}X\right)$ matrix to be too nearly singular to be inverted.

## 8Parallelism and Performance

g02haf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02haf 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.

In cases when ${\mathbf{isigma}}\ge 0$ it is important for the value of sigma to be of a reasonable magnitude. Too small a value may cause too many of the winsorized residuals, i.e., $\psi \left({r}_{i}/\sigma \right)$, to be zero or a value of ${\psi }^{\prime }\left({r}_{i}/\sigma \right)$, used to estimate the asymptotic covariance matrix, to be zero. This can lead to errors ${\mathbf{ifail}}={\mathbf{8}}$ or ${\mathbf{9}}$ (if ${\mathbf{indw}}\ne 0$), ${\mathbf{ifail}}={\mathbf{10}}$ (if ${\mathbf{indw}}=0$) and ${\mathbf{ifail}}={\mathbf{11}}$.
g02hbf, g02hdf and g02hff together carry out the same calculations as g02haf but for user-supplied functions for $\psi$, $\chi$, ${\psi }^{\prime }\text{}$ and $u$.

## 10Example

The number of observations and the number of $x$ variables are read in followed by the data. The option arguments are then read in (in this case giving Schweppe type regression with Hampel's $\psi$ function and Huber's $\chi$ function and then using the ‘replace expected by observed’ option in calculating the covariances). Finally a set of values for the constants are read in.
After a call to g02haf, $\stackrel{^}{\theta }$, its standard error and $\stackrel{^}{\sigma }$ are printed. In addition the weight and residual for each observation is printed.

### 10.1Program Text

Program Text (g02hafe.f90)

### 10.2Program Data

Program Data (g02hafe.d)

### 10.3Program Results

Program Results (g02hafe.r)

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