nag_anova_factorial (g04cac) (PDF version)
g04 Chapter Contents
g04 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_anova_factorial (g04cac)

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

#include <nag.h>
#include <nagg04.h>
void  nag_anova_factorial (Integer n, const double y[], Integer nfac, const Integer lfac[], Integer nblock, Integer inter, Integer irdf, Integer *mterm, double **table, double **tmean, Integer *maxt, double **e, Integer **imean, double **semean, double **bmean, double r[], NagError *fail)

3  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.,
T 1 P 1 T 2 P 1 T 3 P 1 ,  
T 1 P 2 T 2 P 2 T 3 P 2  
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
y ijk = μ + t i + p j + γ ij + e ijk  
where μ  is the overall mean, t i  is the main effect of T , p j  is the main effect of P , γ 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 parameter estimates. For the example here these are:
i=1 3 t ^ i = 0 ,   j=1 2 p ^ j = 0 ,   i=1 3 γ ^ ij = 0   for ​ j = 1 , 2   and   j=1 2 γ ^ ij = 0   for ​ i = 1 , 2 , 3 ,  
where ^  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 (g04cac) 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 (g04cac) has to be in standard order given by the order of the factors. Let there be k  factors, f 1 , f 2 , , f k  in that order with levels l 1 , l 2 , , l k  respectively. Standard order requires the levels of factor f 1  are in order 1 , 2 , , l 1  and within each level of f 1  the levels of f 2  are in order 1 , 2 , , 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 , , l 1  and within each level of f 1  the levels of f 2  are in order 1 , 2 , , 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:
f 1 f 2 f 3 ,  
f 1 f 2 f 4 ,  
f 1 f 2 f 5 ,  
f 1 f 3 f 4 ,  
f 1 f 3 f 5 ,  
f 1 f 4 f 5 ,  
f 2 f 3 f 4 ,  
f 2 f 3 f 5 ,  
f 2 f 4 f 5 ,  
f 3 f 4 f 5  

4  References

John J A and Quenouille M H (1977) Experiments: Design and Analysis Griffin

5  Arguments

1:     n IntegerInput
On entry: the number of observations.
Constraints:
  • n4 .
  • n must be a multiple of nblock if nblock>1 .
  • n must be a multiple of the number of treatment combinations, that is a multiple of i=1 k lfac[i-1] .
2:     y[n] const doubleInput
On entry: the number of observations in standard order, see Section 3.
3:     nfac IntegerInput
On entry: the number of factors, k .
Constraint: nfac1 .
4:     lfac[nfac] const IntegerInput
On entry: lfac[i-1]  must contain the number of levels for the i th factor, for i=1,2,,k .
Constraint: lfac[i-1] 2 , for i=1,2,,k.
5:     nblock IntegerInput
On entry: the number of blocks. If there are no blocks, set nblock=0  or 1.
Constraint: nblock0 .
If nblock2 , n/nblock must be a multiple of the number of treatment combinations, that is a multiple of i=1 k lfac[i-1] .
6:     inter IntegerInput
On entry: the maximum number of factors in an interaction term. If no interaction terms are to be computed, set inter=0  or 1.
Constraint: 0 inter nfac .
7:     irdf IntegerInput
On entry: the adjustment to the residual and total degrees of freedom. The total degrees of freedom are set to n-irdf  and the residual degrees of freedom adjusted accordingly. For examples of the use of irdf see Section 9.
Constraint: irdf0 .
8:     mterm Integer *Output
On exit: the number of terms in the analysis of variance table, see Section 9.
The number of treatment effects is mterm-3 .
9:     table double **Output
On exit: a pointer which points to mterm×5  memory locations, allocated internally. Viewing this memory as a two-dimensional mterm×5  array, the first mterm 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 mtermth row corresponds to the total sum of squares for y and the mterm-1 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 Section 3.
10:   tmean double **Output
On exit: a pointer pointing to maxt memory locations, allocated internally. It contains 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 Section 3.
11:   maxt Integer *Output
On exit: the number of treatment means that have been computed, see Section 9.
12:   e double **Output
On exit: a pointer pointing to maxt memory locations, allocated internally. It contains the estimated effects in the same order as for the means in tmean.
13:   imean Integer **Output
On exit: a pointer pointing to mterm memory locations, allocated internally. It 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 tmean[0]  up to tmean[ imean[0] - 1 ] . Other effect means corresponding to the i th treatment effect, for i=2,3,,mterm - 3, are stored in tmean[ imean[i-2] ]  up to tmean[ imean[i-1] - 1 ] .
14:   semean double **Output
On exit: a pointer pointing to mterm memory locations, allocated internally. It contains the standard error of the difference between means corresponding to the i th treatment effect in the ANOVA table.
15:   bmean double **Output
On exit: a pointer pointing to nblock+1  memory locations, allocated internally. bmean[0]  contains the grand mean, if nblock>1 , bmean[1]  up to bmean[nblock]  contain the block means.
16:   r[n] doubleOutput
On exit: the residuals.
17:   fail NagError *Input/Output
The NAG error argument (see Section 2.7 in How to Use the NAG Library and its Documentation).

6  Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, inter=value  while nfac=value . These arguments must satisfy internfac .
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_ARRAY_CONSTANT
On entry, the elements of the array y are constant.
NE_G04CA_RES_DF
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.
NE_INT_2
On entry, nblock=value , n=value .
Constraint: n must be a multiple of nblock, when nblock>1 .
NE_INT_ARG_LT
On entry, inter=value.
Constraint: inter0.
On entry, irdf=value.
Constraint: irdf0.
On entry, n=value.
Constraint: n4.
On entry, nblock=value.
Constraint: nblock0.
On entry, nfac=value.
Constraint: nfac1.
NE_INTARR
On entry, lfac[value] = value.
Constraint: lfac[i-1] 2 , for i=1,2,,nfac.
NE_PLOT_TREAT
The number of plots per block is not a multiple of the number of treatment combinations.

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

8  Parallelism and Performance

nag_anova_factorial (g04cac) is not threaded in any implementation.

9  Further Comments

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,,k, and let t  be the maximum number of terms in an interaction, then the number of rows in the ANOVA table is
i=1 t 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_regress_confid_interval (g02cbc) or nag_regsn_mult_linear (g02dac). 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 (g04bbc) or a prior call to nag_anova_factorial (g04cac) if the data is reordered between calls. The residuals from the preliminary analysis are then input to nag_anova_factorial (g04cac). 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.
If nag_anova_factorial (g04cac) is to be called repeatedly then the memory allocated to table, tmean, e, imean, semean, and bmean must be freed between calls. You are advised to call nag_anova_factorial_free (g04czc) to achieve this.

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

10.1  Program Text

Program Text (g04cace.c)

10.2  Program Data

Program Data (g04cace.d)

10.3  Program Results

Program Results (g04cace.r)


nag_anova_factorial (g04cac) (PDF version)
g04 Chapter Contents
g04 Chapter Introduction
NAG Library Manual

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