G10 Chapter Contents
G10 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG10ZAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G10ZAF orders and weights data which is entered unsequentially, weighted or unweighted.

## 2  Specification

 SUBROUTINE G10ZAF ( WEIGHT, N, X, Y, WT, NORD, XORD, YORD, WTORD, RSS, IWRK, IFAIL)
 INTEGER N, NORD, IWRK(N), IFAIL REAL (KIND=nag_wp) X(N), Y(N), WT(*), XORD(N), YORD(N), WTORD(N), RSS CHARACTER(1) WEIGHT

## 3  Description

Given a set of observations $\left({x}_{i},{y}_{i}\right)$, for $i=1,2,\dots ,n$, with corresponding weights ${w}_{i}$, G10ZAF rearranges the observations so that the ${x}_{i}$ are in ascending order.
For any equal ${x}_{i}$ in the ordered set, say ${x}_{j}={x}_{j+1}=\cdots ={x}_{j+k}$, a single observation ${x}_{j}$ is returned with a corresponding ${y}^{\prime }$ and ${w}^{\prime }$, calculated as
 $w′=∑l=0kwi+l$
and
 $y′=∑l= 0kwi+lyi+l w′ .$
Observations with zero weight are ignored. If no weights are supplied by you, then unit weights are assumed; that is ${w}_{\mathit{i}}=1$, for $\mathit{i}=1,2,\dots ,n$.
In addition, the within group sum of squares is computed for the tied observations using West's algorithm (see West (1979)).

## 4  References

Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

## 5  Parameters

1:     WEIGHT – CHARACTER(1)Input
On entry: indicates whether user-defined weights are to be used.
• If ${\mathbf{WEIGHT}}=\text{'W'}$, user-defined weights are to be used and must be supplied in WT.
• If ${\mathbf{WEIGHT}}=\text{'U'}$, the data is treated as unweighted.
Constraint: ${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'U'}$.
2:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge 1$.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the values, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
4:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the values ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
5:     WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$, WT must contain the $n$ weights. Otherwise WT is not referenced and unit weights are assumed.
Constraints:
• if ${\mathbf{WEIGHT}}=\text{'W'}$, ${\mathbf{WT}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,n$;
• if ${\mathbf{WEIGHT}}=\text{'W'}$, ${\sum }_{i=1}^{n}{\mathbf{WT}}\left(i\right)>0$.
6:     NORD – INTEGEROutput
On exit: the number of distinct observations.
7:     XORD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the first NORD elements contain the ordered and distinct ${x}_{i}$.
8:     YORD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the first NORD elements contain the values ${y}^{\prime }$ corresponding to the values in XORD.
9:     WTORD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the first NORD elements contain the values ${w}^{\prime }$ corresponding to the values of XORD and YORD.
On exit: the within group sum of squares for tied observations.
11:   IWRK(N) – INTEGER arrayWorkspace
12:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction 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, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{WEIGHT}}\ne \text{'W'}$ or $\text{'U'}$, or ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{WEIGHT}}=\text{'W'}$ and at least one element of WT is $\text{}<0.0$, or all elements of WT are $0.0$.

## 7  Accuracy

For a discussion on the accuracy of the algorithm for computing mean and variance see West (1979).

G10ZAF may be used to compute the pure error sum of squares in simple linear regression along with G02DAF; see Draper and Smith (1985).

## 9  Example

A set of unweighted observations are input and G10ZAF used to produce a set of strictly increasing weighted observations.

### 9.1  Program Text

Program Text (g10zafe.f90)

### 9.2  Program Data

Program Data (g10zafe.d)

### 9.3  Program Results

Program Results (g10zafe.r)