hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_anova_contrasts (g04da)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


nag_anova_contrasts (g04da) computes sum of squares for a user-defined contrast between means.


[est, tabl, ifail] = g04da(tmean, irep, rms, rdf, ct, tabl, tol, usetx, tx, 'nt', nt, 'nc', nc)
[est, tabl, ifail] = nag_anova_contrasts(tmean, irep, rms, rdf, ct, tabl, tol, usetx, tx, 'nt', nt, 'nc', nc)


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


Compulsory Input Parameters

1:     tmeannt – double array
The treatment means, τ^i, for i=1,2,,t.
2:     irepnt int64int32nag_int array
The replication for each treatment mean, ni, for i=1,2,,t.
3:     rms – double scalar
The residual mean square, σ^2.
Constraint: rms>0.0.
4:     rdf – double scalar
The residual degrees of freedom.
Constraint: rdf1.0.
5:     ctldctnc – double array
ldct, the first dimension of the array, must satisfy the constraint ldctnt.
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.
6:     tablldtabl: – double array
The first dimension of the array tabl must be at least nc.
The second dimension of the array tabl must be at least 5.
The elements of tabl that are not referenced as described below remain unchanged.
7:     tol – double scalar
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.
8:     usetx – logical scalar
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.
9:     txnt – double array
If usetx=true tx must contain the means τi*, for i=1,2,,t.

Optional Input Parameters

1:     nt int64int32nag_int scalar
Default: the dimension of the arrays irep, tmean, tx and the first dimension of the array ct. (An error is raised if these dimensions are not equal.)
t, the number of treatment means.
Constraint: nt2.
2:     nc int64int32nag_int scalar
Default: the first dimension of the array tabl and the second dimension of the array ct. (An error is raised if these dimensions are not equal.)
The number of contrasts.
Constraint: nc1.

Output Parameters

1:     estnc – double array
The estimates of the contrast, Λj, for j=1,2,,nc.
2:     tablldtabl: – double array
The first dimension of the array tabl will be nc.
The second dimension of the array tabl will be 5.
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.
3:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Note: nag_anova_contrasts (g04da) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

On entry,nc<1,
W  ifail=2
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.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


The computations are stable.

Further Comments

If the treatments have a factorial structure nag_anova_factorial (g04ca) should be used and if the treatments have no structure the means can be compared using nag_anova_confidence (g04db).


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 nag_correg_coeffs_pearson_miss_case (g02bb). Two contrasts are analysed, one comparing the control with use of sulphur, the other comparing spring with autumn application.
function g04da_example

fprintf('g04da example results\n\n');

n1 = int64(1);
iblock = n1;

y  = [12 10 24 29 30 18 32 26  9  9 ...
      16  4 30  7 21  9 16 10 18 18 ...
      18 24 12 19 10  4  4  5 17  7 ...
      16 17];
it = [n1  1  1  1  1  1  1  1  2  2 ...
       2  2  3  3  3  3  4  4  4  4 ...
       5  5  5  5  6  6  6  6  7  7 ...
       7  7];

% Contrasts
nt = 7*n1;
nc = 2*n1;
ct = [6,  0;
     -1,  1;
     -1, -1;
     -1,  1;
     -1, -1;
     -1,  1;
     -1, -1];
names = {'Cntl v S    '; 'Spring v A  '};

% Calculate ANOVA table
tol  = 0;
irdf = int64(0);
[gmean, bmean, tmean, table, c, irep, r, ef, ifail] = ...
  g04bb( ...
         y, iblock, nt, it, tol, irdf);

% Display ANOVA table results
fprintf('ANOVA table\n\n');
fprintf(' Source        df         SS          MS          F        Prob\n\n');
fmt5 = '%s%5.0f%12.1f%12.1f%12.3f%11.4f\n';
fmt3 = '%s%5.0f%12.1f%12.1f\n';
fmt2 = '%s%5.0f%12.1f\n\n';
if iblock > 1
  fprintf(fmt5, 'Blocks      ', table(1,1:5));
fprintf(fmt5, 'Treatments  ', table(2,1:5));
fprintf(fmt3, 'Residual    ', table(3,1:3));
fprintf(fmt2, 'Total       ', table(4,1:2));

% Extract the residual mean square and degrees of freedom from table
rms = table(3,3);
rdf = table(3,1);

% Compute sums of squares for contrast
usetx = false;
tx    = zeros(nt, 1);
table = zeros(nc,5);
[est, table, ifail] = g04da( ...
                             tmean, irep, rms, rdf, ct, table, tol, usetx, tx);

% Display results
fprintf('Orthogonal Contrasts\n\n');
for i = 1:nc
  fprintf(fmt5, names{i}, table(i,1:5));

g04da example results

ANOVA table

 Source        df         SS          MS          F        Prob

Treatments      6       972.3       162.1       3.608     0.0103
Residual       25      1122.9        44.9
Total          31      2095.2

Orthogonal Contrasts

Cntl v S        1       518.0       518.0      11.533     0.0023
Spring v A      1       228.2       228.2       5.080     0.0332

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015