# NAG CL Interfaceg02hac (robustm)

Settings help

CL Name Style:

## 1Purpose

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

## 2Specification

 #include
 void g02hac (Nag_RegType regtype, Nag_PsiFun psifun, Nag_SigmaEst sigma_est, Nag_CovMatrixEst covmat_est, Integer n, Integer m, double x[], Integer tdx, double y[], double cpsi, const double hpsi[], double cucv, double dchi, double theta[], double *sigma, double c[], Integer tdc, double rs[], double wt[], double tol, Integer max_iter, Integer print_iter, const char *outfile, double info[], NagError *fail)
The function may be called by the names: g02hac, nag_correg_robustm or nag_robust_m_regsn_estim.

## 3Description

For the linear regression model
 $y = X θ + ε$
 where $y$ is a vector of length $n$ of the dependent variable, $X$ is a $n×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}$:
g02hac calculates the M-estimates given by the solution, $\stackrel{^}{\theta }$, to the equation
 $∑ i=1 n ψ ( r i /(σ w i )) w i x ij = 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: $\stackrel{^}{\sigma }={\mathrm{med}}_{i}\left[|{r}_{i}|\right]/{\beta }_{1}$
or as the solution to:
 $∑ i=1 n χ ( r i /( σ ^ w i )) w i 2 = (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 ψ ( r i /σ) w i x ij = 0 , j = 1 , 2 , … , m .$
This may be obtained by use of the transformations
• ${w}_{i}^{*}←\sqrt{{w}_{i}}$
• ${y}_{i}^{*}←{y}_{i}\sqrt{{w}_{i}}$
• ${x}_{ij}^{*}←{x}_{ij}\sqrt{{w}_{i}}, j=1,2,\dots ,m$
(see 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
 $1 n ∑ i=1 n Φ ( β 1 / w i ) = . 75$
where $\Phi$ is the standard Normal cumulative distribution function.
${\beta }_{2}$ is given by:
 $β 2 = ∫ -∞ ∞ χ (z) ϕ (z) dz , in Huber case; β 2 = 1 n ∑ i=1 n w i ∫ -∞ ∞ χ (z) ϕ (z) dz , in Mallows case; β 2 = 1 n ∑ i=1 n w i 2 ∫ -∞ ∞ χ (z/ w i ) ϕ (z) dz , in Schweppe case;$
where $\varphi$ is the standard Normal density, i.e.,
 $1 2π exp(- 1 2 x 2 ) .$
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
 $G ii = { ψ ( r i /(σ w i )) ( r i /(σ w i )) , r i ≠ 0 ψ ′ (0) , r i = 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
• ${\stackrel{~}{y}}_{i}={y}_{i}\sqrt{{G}_{ii}}$
• ${\stackrel{~}{x}}_{ij}={x}_{ij}\sqrt{{G}_{ii}}\text{, }j=1,2,\dots ,m$
and then obtaining the solution of the resulting least squares problem. 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 g02hac.
1. (a)Unit Weights
 $ψ (t) = t , χ (t) = t 2 2$
this gives least squares regression.
2. (b)Huber's Function
 $ψ (t) = max(-c,min(c,t)) , χ (t) = { t 2 2 , |t| ≤ d d 2 2 , |t| > d$
3. (c)Hampel's Piecewise Linear Function
 $ψ h 1 , h 2 , h 3 (t) = - ψ h 1 , h 2 , h 3 (-t) = { t , 0 ≤ t ≤ h 1 h 1 , h 1 ≤ t ≤ h 2 h 1 ( h 3 -t) / ( h 3 - h 2 ) , h 2 ≤ t ≤ h 3 0 , h 3 < t .$
 $χ (t) = { t 2 2 , |t| ≤ d d 2 2 , |t| > d$
4. (d)Andrew's Sine Wave Function
 $ψ (t) = { sin⁡t , -π ≤ t ≤ π 0 , |t| > π χ (t) = { t 2 2 , |t| ≤ d d 2 2 , |t| > d$
5. (e)Tukey's Bi-weight
 $ψ (t) = { t (1- t 2 ) 2 , |t| ≤ 1 0 , |t| > 1 χ (t) = { t 2 2 , |t| ≤ d d 2 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:
 $1 n ∑ i=1 n u ( ‖ z i ‖ 2 ) z i ziT = I$
and
 $z i = Ax i$
 where ${x}_{i}$ is a vector of length $m$ containing the $i$th row of $X$, $A$ is a $m×m$ lower triangular matrix, and $u$ is a suitable function.
The weights are then calculated as
 $w i = f ( ‖ z i ‖ 2 )$
for a suitable function $f$.
g02hac finds $A$ using the iterative procedure
 $A k = ( S k +I) A k-1$
where ${S}_{k}=\left({s}_{jl}\right)$,
 $s jl = { - min[max( h jl / n ,-BL),BL] j > ℓ - min[max( 1 2 ( h jj /n-1) ,-BD),BD] j = ℓ .$
and
 $h jl = ∑ i=1 n u ( ‖ z i ‖ 2 ) z ij z il$
and $BL$ and $BD$ are bounds set at 0.9.
Two weights are available in g02hac:
 $u (t) = g 1 ( c t )$
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.
2. (ii)Maronna's proposed weights
 $u (t) = c / t 2 |t| > c 1 |t| ≤ c$
 $f (t) = u (t) .$
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 = f H (XTX) −1 σ ^ 2$
where
 $f H = 1 n-m ∑ i=1 n ψ 2 ( r i / σ ^) ( 1 n ∑ i=1 n ψ ′ ( r i σ ^ )) 2 κ 2$
 $κ 2 = 1 + m n 1 n ∑ i=1 n ( ψ ′ ( r i / σ ^)- 1 n ∑ i=1 n ψ ′ ( r i / σ ^)) 2 ( 1 n ∑ i=1 n ψ( r i σ ^ )) 2$
See Huber (1981) and Marazzi (1987b).
For Mallows and Schweppe type regressions $C$ is of the form
 $σ ^ n 2 S 1 −1 S 2 S 1 −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 g02hac:
1. 1.Average over the ${r}_{i}$
 $Schweppe Mallows D i = ( 1 n ∑ j=1 n ψ ′ ( r j σ ^ w i )) w i D i = ( 1 n ∑ j=1 n ψ ′ ( r j σ ^ )) w i$
2. 2.Replace expected value by observed
 $Schweppe Mallows D i = ψ ′ ( r i σ ^ w i ) w i D i = ψ ′ ( r i σ ^ ) w i P i = ψ 2 ( r i σ ^ w i ) w i 2 P i = ψ 2 ( r i σ ^ ) w i 2$
Note: there is no explicit provision in the function 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.
g02hac 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{regtype}$Nag_RegType Input
On entry: specifies the type of regression to be performed.
${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$
Huber type regression.
${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$
Mallows type regression with Maronna's proposed weights.
${\mathbf{regtype}}=\mathrm{Nag_SchweppeReg}$
Schweppe type regression with Krasker–Welsch weights.
Constraint: ${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$, $\mathrm{Nag_MallowsReg}$ or $\mathrm{Nag_SchweppeReg}$.
2: $\mathbf{psifun}$Nag_PsiFun Input
On entry: specifies which $\psi$ function is to be used.
${\mathbf{psifun}}=\mathrm{Nag_Lsq}$
$\psi \left(t\right)=t$, i.e., least squares.
${\mathbf{psifun}}=\mathrm{Nag_HuberFun}$
Huber's function.
${\mathbf{psifun}}=\mathrm{Nag_HampelFun}$
Hampel's piecewise linear function.
${\mathbf{psifun}}=\mathrm{Nag_AndrewFun}$
Andrew's sine wave.
${\mathbf{psifun}}=\mathrm{Nag_TukeyFun}$
Tukey's bi-weight.
Constraint: ${\mathbf{psifun}}=\mathrm{Nag_Lsq}$, $\mathrm{Nag_HuberFun}$, $\mathrm{Nag_HampelFun}$, $\mathrm{Nag_AndrewFun}$ or $\mathrm{Nag_TukeyFun}$.
3: $\mathbf{sigma_est}$Nag_SigmaEst Input
On entry: specifies how $\sigma$ is to be estimated.
${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$
$\sigma$ is estimated by median absolute deviation of residuals.
${\mathbf{sigma_est}}=\mathrm{Nag_SigmaConst}$
$\sigma$ is held constant at its initial value.
${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$
$\sigma$ is estimated using the $\chi$ function.
Constraint: ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$, $\mathrm{Nag_SigmaConst}$ or $\mathrm{Nag_SigmaChi}$.
4: $\mathbf{covmat_est}$Nag_CovMatrixEst Input
On entry: if ${\mathbf{regtype}}\ne \mathrm{Nag_HuberReg}$, covmat_est specifies the approximations used in estimating the covariance matrix of $\stackrel{^}{\theta }$. ${\mathbf{covmat_est}}=\mathrm{Nag_CovMatAve}$, averaging over residuals. ${\mathbf{covmat_est}}=\mathrm{Nag_CovMatObs}$, replacing expected by observed.
If ${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$ then covmat_est is not referenced.
Constraint: ${\mathbf{covmat_est}}=\mathrm{Nag_CovMatAve}$ or $\mathrm{Nag_CovMatObs}$.
5: $\mathbf{n}$Integer Input
On entry: the number of observations, $n$.
Constraint: ${\mathbf{n}}>1$.
6: $\mathbf{m}$Integer Input
On entry: the number $m$, of independent variables.
Constraint: $1\le {\mathbf{m}}<{\mathbf{n}}$.
7: $\mathbf{x}\left[{\mathbf{n}}×{\mathbf{tdx}}\right]$double Input/Output
Note: the $\left(i,j\right)$th element of the matrix $X$ is stored in ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{tdx}}+j-1\right]$.
On entry: the values of the $X$ matrix, i.e., the independent variables. $x\left[\mathit{i}-1\right]\left[\mathit{j}-1\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$.
On exit: if ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, 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. Otherwise x is unchanged.
8: $\mathbf{tdx}$Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
9: $\mathbf{y}\left[{\mathbf{n}}\right]$double Input/Output
On entry: the data values of the dependent variable. ${\mathbf{y}}\left[\mathit{i}-1\right]$ must contain the value of $y$ for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
On exit: if ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, 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. Otherwise y is unchanged.
10: $\mathbf{cpsi}$double Input
On entry: if ${\mathbf{psifun}}=\mathrm{Nag_HuberFun}$, cpsi must specify the argument, $c$, of Huber's $\psi$ function. Otherwise cpsi is not referenced.
Constraint: if ${\mathbf{psifun}}=\mathrm{Nag_HuberFun}$ then ${\mathbf{cpsi}}>0.0$.
11: $\mathbf{hpsi}\left[3\right]$const double Input
On entry: if ${\mathbf{psifun}}=\mathrm{Nag_HampelFun}$ then ${\mathbf{hpsi}}\left[0\right]$, ${\mathbf{hpsi}}\left[1\right]$ and ${\mathbf{hpsi}}\left[2\right]$ must specify the arguments ${h}_{1}$, ${h}_{2}$, and ${h}_{3}$, of Hampel's piecewise linear $\psi$ function. Otherwise the elements of hpsi are not referenced.
Constraint: if ${\mathbf{psifun}}=\mathrm{Nag_HampelFun}$, $0.0\le {\mathbf{hpsi}}\left[0\right]\le {\mathbf{hpsi}}\left[1\right]\le {\mathbf{hpsi}}\left[2\right]$ and ${\mathbf{hpsi}}\left[2\right]>0.0$.
12: $\mathbf{cucv}$double Input
On entry: if ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$ then cucv must specify the value of the constant, $c$, of the function $u$ for Maronna's proposed weights.
If ${\mathbf{regtype}}=\mathrm{Nag_SchweppeReg}$ then cucv must specify the value of the function $u$ for the Krasker–Welsch weights.
If ${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$ then cucv is not referenced.
Constraints:
• if ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, ${\mathbf{cucv}}\ge {\mathbf{m}}$;
• if ${\mathbf{regtype}}=\mathrm{Nag_SchweppeReg}$, ${\mathbf{cucv}}\ge \sqrt{{\mathbf{m}}}$.
13: $\mathbf{dchi}$double Input
On entry: the constant, $d$, of the $\chi$ function.
dchi is referenced only if ${\mathbf{psifun}}\ne \mathrm{Nag_Lsq}$ and ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$.
Constraint: if ${\mathbf{psifun}}\ne \mathrm{Nag_Lsq}$ and ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$, ${\mathbf{dchi}}>0.0$.
14: $\mathbf{theta}\left[{\mathbf{m}}\right]$double Input/Output
On entry: starting values of the argument vector $\theta$. These may be obtained from least squares regression.
Alternatively if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$ and ${\mathbf{sigma}}=1$ or if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$ and sigma approximately equals the standard deviation of the dependent variable, $y$, then ${\mathbf{theta}}\left[\mathit{i}-1\right]=0.0$, for $\mathit{i}=1,2,\dots ,m$ may provide reasonable starting values.
On exit: ${\mathbf{theta}}\left[\mathit{i}-1\right]$ contains the M-estimate of ${\theta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
15: $\mathbf{sigma}$double * 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.
On exit: sigma contains the final estimate of $\sigma$, unless ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaConst}$.
Constraint: ${\mathbf{sigma}}>0.0$.
16: $\mathbf{c}\left[{\mathbf{m}}×{\mathbf{tdc}}\right]$double Output
On exit: the diagonal elements of c contain the estimated asymptotic standard errors of the estimates of $\theta$, i.e., ${\mathbf{c}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdc}}+\mathit{i}-1\right]$ contains the estimated asymptotic standard error of the estimate contained in ${\mathbf{theta}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,m$.
The elements above the diagonal contain the estimated asymptotic correlation between the estimates of $\theta$, i.e., ${\mathbf{c}}\left[\left(i-1\right)×{\mathbf{tdc}}+j-1\right]$, $1\le i contains the asymptotic correlation between the estimates contained in ${\mathbf{theta}}\left[i-1\right]$ and ${\mathbf{theta}}\left[j-1\right]$.
The elements below the diagonal contain the estimated asymptotic covariance between the estimates of $\theta$, i.e., ${\mathbf{c}}\left[\left(i-1\right)×{\mathbf{tdc}}+j-1\right]$, $1\le j contains the estimated asymptotic covariance between the estimates contained in ${\mathbf{theta}}\left[i-1\right]$ and ${\mathbf{theta}}\left[j-1\right]$.
17: $\mathbf{tdc}$Integer Input
On entry: the stride separating matrix column elements in the array c.
Constraint: ${\mathbf{tdc}}\ge {\mathbf{m}}$.
18: $\mathbf{rs}\left[{\mathbf{n}}\right]$double Output
On exit: contains the residuals from the model evaluated at final value of theta, i.e., ${\mathbf{rs}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,n$, contains the vector $\left(y-X\stackrel{^}{\theta }\right)$.
19: $\mathbf{wt}\left[{\mathbf{n}}\right]$double Output
On exit: contains the vector of weights. ${\mathbf{wt}}\left[\mathit{i}-1\right]$ contains the weight for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
20: $\mathbf{tol}$double Input
On entry: the relative precision for the calculation of $A$ (if ${\mathbf{regtype}}\ne \mathrm{Nag_HuberReg}$), the estimates of $\theta$ and the estimate of $\sigma$ (if ${\mathbf{sigma_est}}\ne \mathrm{Nag_SigmaConst}$). Convergence is assumed when the relative change in all elements being considered is less than tol.
If ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$ and ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$, tol is also used to determine the precision of ${\beta }_{1}$.
It is advisable for tol to be greater than $100×$ machine precision.
Constraint: ${\mathbf{tol}}>0.0$.
21: $\mathbf{max_iter}$Integer Input
On entry: the maximum number of iterations that should be used in the calculation of $A$ (if ${\mathbf{regtype}}\ne \mathrm{Nag_HuberReg}$), and of the estimates of $\theta$ and $\sigma$, and of ${\beta }_{1}$ (if ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$ and ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$)
Suggested value: A value of ${\mathbf{max_iter}}=50$ should be adequate for most uses.
Constraint: ${\mathbf{max_iter}}>0$.
On entry: the amount of information that is printed on each iteration.
${\mathbf{print_iter}}=0$
No information is printed.
${\mathbf{print_iter}}\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{print_iter}}\right)$ iterations.
Also, if ${\mathbf{regtype}}\ne \mathrm{Nag_HuberReg}$ and ${\mathbf{print_iter}}>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).
23: $\mathbf{outfile}$const char * Input
On entry: a null terminated character string giving the name of the file to which results should be printed. If outfile is NULL or an empty string then the stdout stream is used. Note that the file will be opened in the append mode.
24: $\mathbf{info}\left[4\right]$double Output
On exit: elements of info contain the following values:
• ${\mathbf{info}}\left[0\right]={\beta }_{1}$ if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$,
• or ${\mathbf{info}}\left[0\right]={\beta }_{2}$ if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$,
• ${\mathbf{info}}\left[1\right]=\text{}$ number of iterations used to calculate $A$.
• ${\mathbf{info}}\left[2\right]=\text{}$ number of iterations used to calculate final estimates of $\theta$ and $\sigma$.
• ${\mathbf{info}}\left[3\right]=k$, the rank of the weighted least squares equations.
25: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_2_INT_ARG_GE
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{m}}<{\mathbf{n}}$.
NE_2_INT_ARG_LT
On entry, ${\mathbf{tdc}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdc}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{tdx}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{m}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, ${\mathbf{psifun}}=\mathrm{Nag_HampelFun}$ and ${\mathbf{hpsi}}\left[0\right]=⟨\mathit{\text{value}}⟩$, ${\mathbf{hpsi}}\left[1\right]=⟨\mathit{\text{value}}⟩$ and ${\mathbf{hpsi}}\left[2\right]=⟨\mathit{\text{value}}⟩$. For this value of psifun, the elements of hpsi must satisfy the condition $0.0\le {\mathbf{hpsi}}\left[0\right]\le {\mathbf{hpsi}}\left[1\right]\le {\mathbf{hpsi}}\left[2\right]$ and ${\mathbf{hpsi}}\left[2\right]>0.0$.
On entry, argument covmat_est had an illegal value.
On entry, argument psifun had an illegal value.
On entry, argument regtype had an illegal value.
On entry, argument sigma_est had an illegal value.
NE_BETA1_ITER_EXCEEDED
The number of iterations required to calculate ${\beta }_{1}$ exceeds max_iter. This is only applicable if ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$ and ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$.
NE_COV_MAT_FACTOR_ZERO
In calculating the correlation factor for the asymptotic variance-covariance matrix, the factor for covariance matrix $\text{}=0$.
For this error, either the value of
 $1 n ∑ i=1 n ψ ′ ( r i / σ ^) = 0 ,$
 or $\kappa =0$, or ${\sum }_{i=1}^{n}{\psi }^{2}\left({r}_{i}/\stackrel{^}{\sigma }\right)=0$. See Section 9. In this case c is returned as ${\left({X}^{\mathrm{T}}X\right)}^{-1}$. (This is only applicable if ${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$).
NE_ERR_DOF_LEQ_ZERO
${\mathbf{n}}=⟨\mathit{\text{value}}⟩$, rank of ${\mathbf{x}}=⟨\mathit{\text{value}}⟩$. The degrees of freedom for error, ${\mathbf{n}}-$ (rank of x) must be $>0.0$.
NE_ESTIM_SIGMA_ZERO
The estimated value of $\sigma$ was $0.0$ during an iteration.
NE_INT_ARG_LE
On entry, max_iter must not be less than or equal to 0: ${\mathbf{max_iter}}=⟨\mathit{\text{value}}⟩$.
NE_INT_ARG_LT
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_INVALID_DCHI_FUN
On entry, ${\mathbf{psifun}}\ne \mathrm{Nag_Lsq}$, ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$ and ${\mathbf{dchi}}=⟨\mathit{\text{value}}⟩$. For these values of psifun and sigma_est, dchi must be $>0.0$.
NE_INVALID_HUBER_FUN
On entry, ${\mathbf{psifun}}=\mathrm{Nag_HuberFun}$ and ${\mathbf{cpsi}}=⟨\mathit{\text{value}}⟩$. For this value of psifun, cpsi must be $>0.0$.
NE_INVALID_MALLOWS_REG_C
On entry, ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, ${\mathbf{cucv}}=⟨\mathit{\text{value}}⟩$and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. For this value of regtype, cucv must be $\ge {\mathbf{m}}$.
NE_INVALID_SCHWEPPE_REG_C
On entry, ${\mathbf{regtype}}=\mathrm{Nag_SchweppeReg}$, ${\mathbf{cucv}}=⟨\mathit{\text{value}}⟩$and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. For this value of regtype, cucv must be $\ge \sqrt{{\mathbf{m}}}$.
NE_LSQ_FAIL_CONV
The iterations to solve the weighted least squares equations failed to converge.
NE_NOT_APPEND_FILE
Cannot open file $⟨\mathit{string}⟩$ for appending.
NE_NOT_CLOSE_FILE
Cannot close file $⟨\mathit{string}⟩$.
NE_REAL_ARG_LE
On entry, sigma must not be less than or equal to 0.0: ${\mathbf{sigma}}=⟨\mathit{\text{value}}⟩$.
On entry, tol must not be less than or equal to 0.0: ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
NE_REG_MAT_SINGULAR
Failure to invert matrix while calculating covariance.
If ${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$, then $\left({X}^{\mathrm{T}}X\right)$ is almost singular.
If ${\mathbf{regtype}}\ne \mathrm{Nag_HuberReg}$, 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.
NE_THETA_ITER_EXCEEDED
The number of iterations required to calculate $\theta$ and $\sigma$ exceeds max_iter. In this case, ${\mathbf{info}}\left[2\right]={\mathbf{max_iter}}$ on exit.
NE_VAR_THETA_LEQ_ZERO
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 the 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. See Section 9.
NE_WT_ITER_EXCEEDED
The number of iterations required to calculate the weights exceeds max_iter. This is only applicable if ${\mathbf{regtype}}\ne \mathrm{Nag_HuberReg}$.
NE_WT_LSQ_NOT_FULL_RANK
The weighted least squares equations are not of full rank.

## 7Accuracy

The precision of the estimates is determined by tol, see Section 5. 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

g02hac is not threaded in any implementation.

In cases when ${\mathbf{sigma_est}}\ne \mathrm{Nag_SigmaRes}$ 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 with fail set to one of the following values:

## 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 g02hac, $\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 (g02hace.c)

### 10.2Program Data

Program Data (g02hace.d)

### 10.3Program Results

Program Results (g02hace.r)