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_factorial (g04ca)

## Purpose

nag_anova_factorial (g04ca) computes an analysis of variance table and treatment means for a complete factorial design.

## Syntax

[table, itotal, tmean, e, imean, semean, bmean, r, ifail] = g04ca(y, lfac, nblock, inter, irdf, mterm, maxt, 'n', n, 'nfac', nfac)
[table, itotal, tmean, e, imean, semean, bmean, r, ifail] = nag_anova_factorial(y, lfac, nblock, inter, irdf, mterm, maxt, 'n', n, 'nfac', nfac)

## Description

An experiment consists of a collection of units, or plots, to which a number of treatments are applied. In a factorial experiment the effects of several different sets of conditions are compared, e.g., three different temperatures, ${T}_{1}$, ${T}_{2}$ and ${T}_{3}$, and two different pressures, ${P}_{1}$ and ${P}_{2}$. The conditions are known as factors and the different values the conditions take are known as levels. In a factorial experiment the experimental treatments are the combinations of all the different levels of all factors, e.g.,
 $T1P1, T2P1, T3P1 T1P2, T2P2, T3P2$
The effect of a factor averaged over all other factors is known as a main effect, and the effect of a combination of some of the factors averaged over all other factors is known as an interaction. This can be represented by a linear model. In the above example if the response was ${y}_{ijk}$ for the $k$th replicate of the $i$th level of $T$ and the $j$th level of $P$ the linear model would be
 $yijk = μ+ ti+ pj+ γij+ eijk$
where $\mu$ is the overall mean, ${t}_{i}$ is the main effect of $T$, ${p}_{j}$ is the main effect of $P$, ${\gamma }_{ij}$ is the $T×P$ interaction and ${e}_{ijk}$ is the random error term. In order to find unique estimates constraints are placed on the arguments estimates. For the example here these are:
 $∑i=13t^i=0, ∑j=12p^j=0, ∑ i=1 3 γ^ij = 0 , for ​j=1,2​ and ∑ j=1 2 γ^ ij = 0 , for ​ i=1,2,3 ,$
where $\stackrel{^}{\text{​ ​}}$ denotes the estimate.
If there is variation in the experimental conditions (e.g., in an experiment on the production of a material different batches of raw material may be used, or the experiment may be carried out on different days), then plots that are similar are grouped together into blocks. For a balanced complete factorial experiment all the treatment combinations occur the same number of times in each block.
nag_anova_factorial (g04ca) computes the analysis of variance (ANOVA) table by sequentially computing the totals and means for an effect from the residuals computed when previous effects have been removed. The effect sum of squares is the sum of squared totals divided by the number of observations per total. The means are then subtracted from the residuals to compute a new set of residuals. At the same time the means for the original data are computed. When all effects are removed the residual sum of squares is computed from the residuals. Given the sums of squares an ANOVA table is then computed along with standard errors for the difference in treatment means.
The data for nag_anova_factorial (g04ca) has to be in standard order given by the order of the factors. Let there be $k$ factors, ${f}_{1},{f}_{2},\dots ,{f}_{k}$ in that order with levels ${l}_{1},{l}_{2},\dots ,{l}_{k}$ respectively. Standard order requires the levels of factor ${f}_{1}$ are in order $1,2,\dots ,{l}_{1}$ and within each level of ${f}_{1}$ the levels of ${f}_{2}$ are in order $1,2,\dots ,{l}_{2}$ and so on.
For an experiment with blocks the data is for block $1$ then for block $2$, etc. Within each block the data must be arranged so that the levels of factor ${f}_{1}$ are in order $1,2,\dots ,{l}_{1}$ and within each level of ${f}_{1}$ the levels of ${f}_{2}$ are in order $1,2,\dots ,{l}_{2}$ and so on. Any within block replication of treatment combinations must occur within the levels of ${f}_{k}$.
The ANOVA table is given in the following order. For a complete factorial experiment the first row is for blocks, if present, then the main effects of the factors in their order, e.g., ${f}_{1}$ followed by ${f}_{2}$, etc. These are then followed by all the two factor interactions then all the three factor interactions, etc., the last two rows being for the residual and total sums of squares. The interactions are arranged in lexical order for the given factor order. For example, for the three factor interactions for a five factor experiment the $10$ interactions would be in the following order:
 $f1f2f3 f1f2f4 f1f2f5 f1f3f4 f1f3f5 f1f4f5 f2f3f4 f2f3f5 f2f4f5 f3f4f5$

## References

Cochran W G and Cox G M (1957) Experimental Designs Wiley
Davis O L (1978) The Design and Analysis of Industrial Experiments Longman
John J A and Quenouille M H (1977) Experiments: Design and Analysis Griffin

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The observations in standard order, see Description.
2:     $\mathrm{lfac}\left({\mathbf{nfac}}\right)$int64int32nag_int array
${\mathbf{lfac}}\left(\mathit{i}\right)$ must contain the number of levels for the $\mathit{i}$th factor, for $\mathit{i}=1,2,\dots ,k$.
Constraint: ${\mathbf{lfac}}\left(\mathit{i}\right)\ge 2$, for $\mathit{i}=1,2,\dots ,k$.
3:     $\mathrm{nblock}$int64int32nag_int scalar
The number of blocks. If there are no blocks, set ${\mathbf{nblock}}=0$ or $1$.
Constraints:
• ${\mathbf{nblock}}\ge 0$;
• if ${\mathbf{nblock}}\ge 2$, ${\mathbf{n}}/{\mathbf{nblock}}$ must be a multiple of the number of treatment combinations, that is a multiple of $\prod _{i=1}^{k}{\mathbf{lfac}}\left(i\right)$.
4:     $\mathrm{inter}$int64int32nag_int scalar
The maximum number of factors in an interaction term. If no interaction terms are to be computed, set ${\mathbf{inter}}=0$ or $1$.
Constraint: $0\le {\mathbf{inter}}\le {\mathbf{nfac}}$.
5:     $\mathrm{irdf}$int64int32nag_int scalar
The adjustment to the residual and total degrees of freedom. The total degrees of freedom are set to ${\mathbf{n}}-{\mathbf{irdf}}$ and the residual degrees of freedom adjusted accordingly. For examples of the use of irdf see Further Comments.
Constraint: ${\mathbf{irdf}}\ge 0$.
6:     $\mathrm{mterm}$int64int32nag_int scalar
The maximum number of terms in the analysis of variance table, see Further Comments.
Constraint: ${\mathbf{mterm}}$ must be large enough to contain the terms specified by nfac, inter and nblock. If the function exits with ${\mathbf{ifail}}\ge {\mathbf{2}}$, the required minimum value of mterm is returned in itotal.
7:     $\mathrm{maxt}$int64int32nag_int scalar
The maximum number of treatment means to be computed, see Further Comments. If the value of maxt is too small for the required analysis then the minimum number is returned in ${\mathbf{imean}}\left(1\right)$.
Constraint: ${\mathbf{maxt}}$ must be large enough for the number of means specified by lfac and inter; if ${\mathbf{inter}}={\mathbf{nfac}}$ then ${\mathbf{maxt}}\ge \prod _{i=1}^{k}\left({\mathbf{lfac}}\left(i\right)+1\right)-1$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array y.
The number of observations.
Constraints:
• ${\mathbf{n}}\ge 4$;
• if ${\mathbf{nblock}}>1$, n must be a multiple of nblock;
• n must be a multiple of the number of treatment combinations, that is a multiple of $\prod _{i=1}^{k}{\mathbf{lfac}}\left(i\right)$.
2:     $\mathrm{nfac}$int64int32nag_int scalar
Default: the dimension of the array lfac.
$k$, the number of factors.
Constraint: ${\mathbf{nfac}}\ge 1$.

### Output Parameters

1:     $\mathrm{table}\left({\mathbf{mterm}},5\right)$ – double array
The first itotal rows of table contain the analysis of variance table. The first column contains the degrees of freedom, the second column contains the sum of squares, the third column (except for the row corresponding to the total sum of squares) contains the mean squares, i.e., the sums of squares divided by the degrees of freedom, and the fourth and fifth columns contain the $F$ ratio and significance level, respectively (except for rows corresponding to the total sum of squares, and the residual sum of squares). All other cells of the table are set to zero.
The first row corresponds to the blocks and is set to zero if there are no blocks. The itotalth row corresponds to the total sum of squares for y and the $\left({\mathbf{itotal}}-1\right)$th row corresponds to the residual sum of squares. The central rows of the table correspond to the main effects followed by the interaction if specified by inter. The main effects are in the order specified by lfac and the interactions are in lexical order, see Description.
2:     $\mathrm{itotal}$int64int32nag_int scalar
The row in table corresponding to the total sum of squares. The number of treatment effects is ${\mathbf{itotal}}-3$.
3:     $\mathrm{tmean}\left({\mathbf{maxt}}\right)$ – double array
The treatment means. The position of the means for an effect is given by the index in imean. For a given effect the means are in standard order, see Description.
4:     $\mathrm{e}\left({\mathbf{maxt}}\right)$ – double array
The estimated effects in the same order as for the means in tmean.
5:     $\mathrm{imean}\left({\mathbf{mterm}}\right)$int64int32nag_int array
Indicates the position of the effect means in tmean. The effect means corresponding to the first treatment effect in the ANOVA table are stored in ${\mathbf{tmean}}\left(1\right)$ up to ${\mathbf{tmean}}\left({\mathbf{imean}}\left(1\right)\right)$. Other effect means corresponding to the $i$th treatment effect, $i=1,2,\dots ,{\mathbf{itotal}}-3$, are stored in ${\mathbf{tmean}}\left({\mathbf{imean}}\left(i-1\right)+1\right)$ up to ${\mathbf{tmean}}\left({\mathbf{imean}}\left(i\right)\right)$.
6:     $\mathrm{semean}\left({\mathbf{mterm}}\right)$ – double array
The standard error of the difference between means corresponding to the $i$th treatment effect in the ANOVA table.
7:     $\mathrm{bmean}\left({\mathbf{nblock}}+1\right)$ – double array
${\mathbf{bmean}}\left(1\right)$ contains the grand mean, if ${\mathbf{nblock}}>1$, ${\mathbf{bmean}}\left(2\right)$ up to ${\mathbf{bmean}}\left({\mathbf{nblock}}+1\right)$ contain the block means.
8:     $\mathrm{r}\left({\mathbf{n}}\right)$ – double array
The residuals.
9:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<4$, or ${\mathbf{nfac}}<1$, or ${\mathbf{nblock}}<0$, or ${\mathbf{inter}}<0$, or ${\mathbf{inter}}>{\mathbf{nfac}}$, or ${\mathbf{irdf}}<0$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{lfac}}\left(i\right)\le 1$, for some $i=1,2,\dots ,{\mathbf{nfac}}$, or the value of maxt is too small, or the value of mterm is too small, or ${\mathbf{nblock}}>1$ and n is not a multiple of nblock, or the number of plots per block is not a multiple of the number of treatment combinations.
${\mathbf{ifail}}=3$
 On entry, the values of y are constant.
${\mathbf{ifail}}=4$
There are no degrees of freedom for the residual or the residual sum of squares is zero. In either case the standard errors and $F$-statistics cannot be computed.
${\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 block and treatment sums of squares are computed from the block and treatment residual totals. The residuals are updated as each effect is computed and the residual sum of squares computed directly from the residuals. This avoids any loss of accuracy in subtracting sums of squares.

The number of rows in the ANOVA table and the number of treatment means are given by the following formulae.
Let there be $k$ factors with levels ${l}_{i}$ for $i=1,2,\dots ,k$, and let $t$ be the maximum number of terms in an interaction then the number of rows in the ANOVA tables is:
 $∑i=1t k i +3.$
The number of treatment means is:
 $∑ i =1 t ∏ j ∈ S i l j ,$
where ${S}_{i}$ is the set of all combinations of the $k$ factors $i$ at a time.
To estimate missing values the Healy and Westmacott procedure or its derivatives may be used, see John and Quenouille (1977). This is an iterative procedure in which estimates of the missing values are adjusted by subtracting the corresponding values of the residuals. The new estimates are then used in the analysis of variance. This process is repeated until convergence. A suitable initial value may be the grand mean. When using this procedure irdf should be set to the number of missing values plus one to obtain the correct degrees of freedom for the residual sum of squares.
For analysis of covariance the residuals are obtained from an analysis of variance of both the response variable and the covariates. The residuals from the response variable are then regressed on the residuals from the covariates using, say, nag_correg_linregs_noconst (g02cb) or nag_correg_linregm_fit (g02da). The coefficients obtained from the regression can be examined for significance and used to produce an adjusted dependent variable using the original response variable and covariate. An approximate adjusted analysis of variance table can then be produced by using the adjusted dependent variable. In this case irdf should be set to one plus the number of fitted covariates.
For designs such as Latin squares one more of the blocking factors has to be removed in a preliminary analysis before the final analysis. This preliminary analysis can be performed using nag_anova_random (g04bb) or a prior call to nag_anova_factorial (g04ca) if the data is reordered between calls. The residuals from the preliminary analysis are then input to nag_anova_factorial (g04ca). In these cases irdf should be set to the difference between n and the residual degrees of freedom from preliminary analysis. Care should be taken when using this approach as there is no check on the orthogonality of the two analyses.

## Example

The data, given by John and Quenouille (1977), is for the yield of turnips for a factorial experiment with two factors, the amount of phosphate with $6$ levels and the amount of liming with $3$ levels. The design was replicated in $3$ blocks. The data is input and the analysis of variance computed. The analysis of variance table and tables of means with their standard errors are printed.
```function g04ca_example

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

y = [274; 361; 253; 325; 317; 339; 326; 402; 336; 379;
345; 361; 352; 334; 318; 339; 393; 358; 350; 340;
203; 397; 356; 298; 382; 376; 355; 418; 387; 379;
432; 339; 293; 322; 417; 342;  82; 297; 133; 306;
352; 361; 220; 333; 270; 388; 379; 274; 336; 307;
266; 389; 333; 353];

% Sizes
nblock = int64(3);
inter  = int64(2);

% Levels for the 2 factors
lfac   = [int64(6); 3];
irdf   = int64(0);

% Get routine to calculate maxt and mterm
ws = warning;
warning('Off');
warn_state = nag_issue_warnings();
nag_issue_warnings(true);

mterm  = int64(1);
maxt   = int64(0);
[table, itotal, tmean, e, imean, semean, bmean, r, ifail] = ...
g04ca( ...
y, lfac, nblock, inter, irdf, mterm, maxt);

nag_issue_warnings(warn_state);
warning(ws);

mterm = itotal;
maxt = imean(1);

% Calculate ANOVA table
[table, itotal, tmean, e, imean, semean, bmean, r, ifail] = ...
g04ca( ...
y, lfac, nblock, inter, irdf, mterm, maxt);

% Display results
fprintf('ANOVA table\n\n');
fprintf(' Source      df         SS          MS          F        Prob\n\n');
fmt6 = '%s%2d%5.0f%12.0f%12.0f%12.3f%11.4f\n';
fmt5 = '%s%5.0f%12.0f%12.0f%12.3f%11.4f\n';
fmt3 = '%s%5.0f%12.0f%12.0f\n';
fmt2 = '%s%5.0f%12.0f\n';
k = 0;
if nblock>1
k = k + 1;
fprintf(fmt5, 'Blocks    ', table(1,1:5));
end
ntreat = itotal - 2 - k;
for i = 1:ntreat
fprintf(fmt6, 'Effect  ', i, table(k+i,1:5));
end
fprintf(fmt3, 'Residual  ', table(itotal-1,1:3));
fprintf(fmt2, 'Total     ', table(itotal,1:2));
fprintf('\nTreatment Means and Standard Errors\n\n');
k = 1;
for i = 1:ntreat
l = imean(i);
fprintf('Effect %5d\n\n', i);
for j = k:6:l
fprintf('%10.2f', tmean(j:min(j+5,l)));
fprintf('\n');
end
fprintf('\nSE of difference in means  = %10.2f\n\n', semean(i));
k = l + 1;
end

```
```g04ca example results

ANOVA table

Source      df         SS          MS          F        Prob

Blocks        2       30119       15059       7.685     0.0018
Effect   1    5       73008       14602       7.451     0.0001
Effect   2    2       21596       10798       5.510     0.0085
Effect   3   10       31192        3119       1.592     0.1513
Residual     34       66628        1960
Total        53      222543

Treatment Means and Standard Errors

Effect     1

254.78    339.00    333.33    367.78    330.78    360.67

SE of difference in means  =      20.87

Effect     2

334.28    353.78    305.11

SE of difference in means  =      14.76

Effect     3

235.33    332.67    196.33    342.67    341.67    332.67
309.33    370.33    320.33    395.00    370.33    338.00
373.33    326.67    292.33    350.00    381.00    351.00

SE of difference in means  =      36.14

```