NAG Library Routine Document

g04daf (contrasts)


    1  Purpose
    7  Accuracy


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


Fortran Interface
Subroutine g04daf ( nt, tmean, irep, rms, rdf, nc, ct, ldct, est, tabl, ldtabl, tol, usetx, tx, ifail)
Integer, Intent (In):: nt, irep(nt), nc, ldct, ldtabl
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: tmean(nt), rms, rdf, ct(ldct,nc), tol, tx(nt)
Real (Kind=nag_wp), Intent (Inout):: tabl(ldtabl,*)
Real (Kind=nag_wp), Intent (Out):: est(nc)
Logical, Intent (In):: usetx
C Header Interface
#include nagmk26.h
void  g04daf_ (const Integer *nt, const double tmean[], const Integer irep[], const double *rms, const double *rdf, const Integer *nc, const double ct[], const Integer *ldct, double est[], double tabl[], const Integer *ldtabl, const double *tol, const logical *usetx, const double tx[], Integer *ifail)


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), σ^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 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 τ^i, i=1,2,,t, and let the jth contrast be defined by λij, i=1,2,,t, then the estimate of the contrast is simply:
and the sum of squares for the contrast is:
SSj=Λj2 i=1tλij2/ni (1)
where ni is the number of observations for the ith treatment. Such a contrast has one degree of freedom so that the appropriate F-statistic is SSj/σ^2.
The two contrasts λij and λij are orthogonal if i=1tλijλij=0 and the contrast λij is orthogonal to the overall mean if i=1tλij=0. In practice these sums will be tested against a small quantity, ε. 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)
Λj*=i= 1tτi*λij  
with τi*, for i=1,2,,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).


Cochran W G and Cox G M (1957) Experimental Designs Wiley
John J A (1987) Cyclic Designs Chapman and Hall
Winer B J (1970) Statistical Principles in Experimental Design McGraw–Hill


1:     nt – IntegerInput
On entry: t, the number of treatment means.
Constraint: nt2.
2:     tmeannt – Real (Kind=nag_wp) arrayInput
On entry: the treatment means, τ^i, for i=1,2,,t.
3:     irepnt – Integer arrayInput
On entry: the replication for each treatment mean, ni, for i=1,2,,t.
4:     rms – Real (Kind=nag_wp)Input
On entry: the residual mean square, σ^2.
Constraint: rms>0.0.
5:     rdf – Real (Kind=nag_wp)Input
On entry: the residual degrees of freedom.
Constraint: rdf1.0.
6:     nc – IntegerInput
On entry: the number of contrasts.
Constraint: nc1.
7:     ctldctnc – Real (Kind=nag_wp) arrayInput
On entry: the columns of ct must contain the nc contrasts, that is ctij must contain λij, for i=1,2,,t and j=1,2,,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: ldctnt.
9:     estnc – Real (Kind=nag_wp) arrayOutput
On exit: the estimates of the contrast, Λj, for j=1,2,,nc.
10:   tablldtabl* – 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: ldtablnc.
12:   tol – Real (Kind=nag_wp)Input
On entry: the tolerance, ε used to check if the contrasts are orthogonal and if they are orthogonal to the mean. If tol0.0 the value machine precision is used.
13:   usetx – LogicalInput
On entry: if usetx=.TRUE. the means τi* are provided in tx and the formula (2) is used instead of formula (1).
If usetx=.FALSE. formula (1) is used and tx is not referenced.
14:   txnt – Real (Kind=nag_wp) arrayInput
On entry: if usetx=.TRUE. tx must contain the means τi*, for i=1,2,,t.
15:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ 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​ 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 ifail0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry ifail=0 or -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:
On entry,nc<1,
On entry,a contrast is not orthogonal to the mean,
orat least two contrasts are not orthogonal.
If ifail=2 full results are returned but they should be interpreted with care.
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.


The computations are stable.

Parallelism and Performance

g04daf is not threaded in any implementation.

Further Comments

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.


The data is from 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.

Program Text

Program Text (g04dafe.f90)

Program Data

Program Data (g04dafe.d)

Program Results

Program Results (g04dafe.r)

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