Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_anova_contrasts (g04da)

## Purpose

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

## Syntax

[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)

## 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).

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{tmean}\left({\mathbf{nt}}\right)$ – double array
The treatment means, ${\stackrel{^}{\tau }}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,t$.
2:     $\mathrm{irep}\left({\mathbf{nt}}\right)$int64int32nag_int array
The replication for each treatment mean, ${n}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,t$.
3:     $\mathrm{rms}$ – double scalar
The residual mean square, ${\stackrel{^}{\sigma }}^{2}$.
Constraint: ${\mathbf{rms}}>0.0$.
4:     $\mathrm{rdf}$ – double scalar
The residual degrees of freedom.
Constraint: ${\mathbf{rdf}}\ge 1.0$.
5:     $\mathrm{ct}\left(\mathit{ldct},{\mathbf{nc}}\right)$ – double array
ldct, the first dimension of the array, must satisfy the constraint $\mathit{ldct}\ge {\mathbf{nt}}$.
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}}$.
6:     $\mathrm{tabl}\left(\mathit{ldtabl},:\right)$ – double array
The first dimension of the array tabl must be at least ${\mathbf{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:     $\mathrm{tol}$ – double scalar
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.
8:     $\mathrm{usetx}$ – logical scalar
If ${\mathbf{usetx}}=\mathit{true}$ the means ${\tau }_{i}^{*}$ are provided in tx and the formula (2) is used instead of formula (1).
If ${\mathbf{usetx}}=\mathit{false}$ formula (1) is used and tx is not referenced.
9:     $\mathrm{tx}\left({\mathbf{nt}}\right)$ – double array
If ${\mathbf{usetx}}=\mathit{true}$ tx must contain the means ${\tau }_{\mathit{i}}^{*}$, for $\mathit{i}=1,2,\dots ,t$.

### Optional Input Parameters

1:     $\mathrm{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: ${\mathbf{nt}}\ge 2$.
2:     $\mathrm{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: ${\mathbf{nc}}\ge 1$.

### Output Parameters

1:     $\mathrm{est}\left({\mathbf{nc}}\right)$ – double array
The estimates of the contrast, ${\Lambda }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathbf{nc}}$.
2:     $\mathrm{tabl}\left(\mathit{ldtabl},:\right)$ – double array
The first dimension of the array tabl will be ${\mathbf{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:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{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.

${\mathbf{ifail}}=1$
 On entry, ${\mathbf{nc}}<1$, or ${\mathbf{nt}}<2$, or $\mathit{ldct}<{\mathbf{nt}}$, or $\mathit{ldtabl}<{\mathbf{nc}}$, or ${\mathbf{rms}}\le 0.0$, or ${\mathbf{rdf}}<1.0$.
W  ${\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.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The computations are stable.

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).

## Example

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));
end
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));
end

```
```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
```