G04 Chapter Contents
G04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG04DAF

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

G04DAF computes sum of squares for a user-defined contrast between means.

## 2  Specification

 SUBROUTINE G04DAF ( NT, TMEAN, IREP, RMS, RDF, NC, CT, LDCT, EST, TABL, LDTABL, TOL, USETX, TX, IFAIL)
 INTEGER NT, IREP(NT), NC, LDCT, LDTABL, IFAIL REAL (KIND=nag_wp) TMEAN(NT), RMS, RDF, CT(LDCT,NC), EST(NC), TABL(LDTABL,*), TOL, TX(NT) LOGICAL USETX

## 3  Description

In the analysis of designed experiments the first stage is to compute the basic analysis of variance table, the estimate of the error variance (the residual or error mean square), ${\stackrel{^}{\sigma }}^{2}$, and the (variance ratio) $F$-statistic for the $t$ treatments. If this $F$-test is significant then the second stage of the analysis is to explore which treatments are significantly different.
If there is a structure to the treatments then this may lead to hypotheses that can be defined before the analysis and tested using linear contrasts. For example, if the treatments were three different fixed temperatures, say $18$, $20$ and $22$, and an uncontrolled temperature (denoted by $\mathrm{N}$) then the following contrasts might be of interest.
 $18 20 22 N a 13 13 13 -1 b -1 0 1 0$
The first represents the average difference between the controlled temperatures and the uncontrolled temperature. The second represents the linear effect of an increasing fixed temperature.
For a randomized complete block design or a completely randomized design, let the treatment means be ${\stackrel{^}{\tau }}_{i}$, $i=1,2,\dots ,t$, and let the $j$th contrast be defined by ${\lambda }_{ij}$, $i=1,2,\dots ,t$, then the estimate of the contrast is simply:
 $Λj=∑i=1tτ^iλij$
and the sum of squares for the contrast is:
 $SSj=Λj2 ∑i=1tλij2/ni$ (1)
where ${n}_{i}$ is the number of observations for the $i$th treatment. Such a contrast has one degree of freedom so that the appropriate $F$-statistic is ${\mathrm{SS}}_{j}/{\stackrel{^}{\sigma }}^{2}$.
The two contrasts ${\lambda }_{ij}$ and ${\lambda }_{i{j}^{\prime }}$ are orthogonal if $\sum _{i=1}^{t}{\lambda }_{ij}{\lambda }_{i{j}^{\prime }}=0$ and the contrast ${\lambda }_{ij}$ is orthogonal to the overall mean if $\sum _{i=1}^{t}{\lambda }_{ij}=0$. In practice these sums will be tested against a small quantity, $\epsilon$. If each of a set of contrasts is orthogonal to the mean and they are all mutually orthogonal then the contrasts provide a partition of the treatment sum of squares into independent components. Hence the resulting $F$-tests are independent.
If the treatments come from a design in which treatments are not orthogonal to blocks then the sum of squares for a contrast is given by:
 $SSj=ΛjΛj* ∑i=1tλij2/ni$ (2)
where
 $Λj*=∑i= 1tτi*λij$
with ${\tau }_{\mathit{i}}^{*}$, for $\mathit{i}=1,2,\dots ,t$, being adjusted treatment means computed by first eliminating blocks then computing the treatment means from the block adjusted observations without taking into account the non-orthogonality between treatments and blocks. For further details see John (1987) and Morgan (1993).

## 4  References

Cochran W G and Cox G M (1957) Experimental Designs Wiley
John J A (1987) Cyclic Designs Chapman and Hall
Morgan G W (1993) Analysis of variance using the NAG Fortran Library: Examples from Cochran and Cox NAG Technical Report TR 3/93 NAG Ltd, Oxford
Winer B J (1970) Statistical Principles in Experimental Design McGraw–Hill

## 5  Parameters

1:     NT – INTEGERInput
On entry: $t$, the number of treatment means.
Constraint: ${\mathbf{NT}}\ge 2$.
2:     TMEAN(NT) – REAL (KIND=nag_wp) arrayInput
On entry: the treatment means, ${\stackrel{^}{\tau }}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,t$.
3:     IREP(NT) – INTEGER arrayInput
On entry: the replication for each treatment mean, ${n}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,t$.
4:     RMS – REAL (KIND=nag_wp)Input
On entry: the residual mean square, ${\stackrel{^}{\sigma }}^{2}$.
Constraint: ${\mathbf{RMS}}>0.0$.
5:     RDF – REAL (KIND=nag_wp)Input
On entry: the residual degrees of freedom.
Constraint: ${\mathbf{RDF}}\ge 1.0$.
6:     NC – INTEGERInput
On entry: the number of contrasts.
Constraint: ${\mathbf{NC}}\ge 1$.
7:     CT(LDCT,NC) – REAL (KIND=nag_wp) arrayInput
On entry: the columns of CT must contain the NC contrasts, that is ${\mathbf{CT}}\left(\mathit{i},\mathit{j}\right)$ must contain ${\lambda }_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,t$ and $\mathit{j}=1,2,\dots ,{\mathbf{NC}}$.
8:     LDCT – INTEGERInput
On entry: the first dimension of the array CT as declared in the (sub)program from which G04DAF is called.
Constraint: ${\mathbf{LDCT}}\ge {\mathbf{NT}}$.
9:     EST(NC) – REAL (KIND=nag_wp) arrayOutput
On exit: the estimates of the contrast, ${\Lambda }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathbf{NC}}$.
10:   TABL(LDTABL,$*$) – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array TABL must be at least $5$.
On entry: the elements of TABL that are not referenced as described below remain unchanged.
On exit: the rows of the analysis of variance table for the contrasts. For each row column 1 contains the degrees of freedom, column 2 contains the sum of squares, column 3 contains the mean square, column 4 the $F$-statistic and column 5 the significance level for the contrast. Note that the degrees of freedom are always one and so the mean square equals the sum of squares.
11:   LDTABL – INTEGERInput
On entry: the first dimension of the array TABL as declared in the (sub)program from which G04DAF is called.
Constraint: ${\mathbf{LDTABL}}\ge {\mathbf{NC}}$.
12:   TOL – REAL (KIND=nag_wp)Input
On entry: the tolerance, $\epsilon$ used to check if the contrasts are orthogonal and if they are orthogonal to the mean. If ${\mathbf{TOL}}\le 0.0$ the value machine precision is used.
13:   USETX – LOGICALInput
On entry: if ${\mathbf{USETX}}=\mathrm{.TRUE.}$ the means ${\tau }_{i}^{*}$ are provided in TX and the formula (2) is used instead of formula (1).
If ${\mathbf{USETX}}=\mathrm{.FALSE.}$ formula (1) is used and TX is not referenced.
14:   TX(NT) – REAL (KIND=nag_wp) arrayInput
On entry: if ${\mathbf{USETX}}=\mathrm{.TRUE.}$ TX must contain the means ${\tau }_{\mathit{i}}^{*}$, for $\mathit{i}=1,2,\dots ,t$.
15:   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, because for this routine the values of the output parameters 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).

## 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).
Note: G04DAF 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{NC}}<1$, or ${\mathbf{NT}}<2$, or ${\mathbf{LDCT}}<{\mathbf{NT}}$, or ${\mathbf{LDTABL}}<{\mathbf{NC}}$, or ${\mathbf{RMS}}\le 0.0$, or ${\mathbf{RDF}}<1.0$.
${\mathbf{IFAIL}}=2$
 On entry, a contrast is not orthogonal to the mean, or at least two contrasts are not orthogonal.
If ${\mathbf{IFAIL}}={\mathbf{2}}$ full results are returned but they should be interpreted with care.

## 7  Accuracy

The computations are stable.

If the treatments have a factorial structure G04CAF should be used and if the treatments have no structure the means can be compared using G04DBF.

## 9  Example

The data is given in Morgan (1993) and is for a completely randomized experiment on potato scab with seven treatments representing amounts of sulphur applied, whether the application was in spring or autumn and a control treatment. The one-way anova is computed using G02BBF. Two contrasts are analysed, one comparing the control with use of sulphur, the other comparing spring with autumn application.

### 9.1  Program Text

Program Text (g04dafe.f90)

### 9.2  Program Data

Program Data (g04dafe.d)

### 9.3  Program Results

Program Results (g04dafe.r)