NAG FL Interface
g04caf (factorial)

1 Purpose

g04caf computes an analysis of variance table and treatment means for a complete factorial design.

2 Specification

Fortran Interface
Subroutine g04caf ( n, y, nfac, lfac, nblock, inter, irdf, mterm, table, itotal, tmean, maxt, e, imean, semean, bmean, r, iwk, ifail)
Integer, Intent (In) :: n, nfac, lfac(nfac), nblock, inter, irdf, mterm, maxt
Integer, Intent (Inout) :: ifail
Integer, Intent (Out) :: itotal, imean(mterm), iwk(n+3*nfac)
Real (Kind=nag_wp), Intent (In) :: y(n)
Real (Kind=nag_wp), Intent (Out) :: table(mterm,5), tmean(maxt), e(maxt), semean(mterm), bmean(nblock+1), r(n)
C Header Interface
#include <nag.h>
void  g04caf_ (const Integer *n, const double y[], const Integer *nfac, const Integer lfac[], const Integer *nblock, const Integer *inter, const Integer *irdf, const Integer *mterm, double table[], Integer *itotal, double tmean[], const Integer *maxt, double e[], Integer imean[], double semean[], double bmean[], double r[], Integer iwk[], Integer *ifail)
The routine may be called by the names g04caf or nagf_anova_factorial.

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, T1, T2 and T3, and two different pressures, P1 and P2. 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 yijk for the kth replicate of the ith level of T and the jth level of P the linear model would be
yijk = μ+ ti+ pj+ γij+ eijk  
where μ is the overall mean, ti is the main effect of T, pj is the main effect of P, γij is the T×P interaction and eijk is the random error term. In order to find unique estimates constraints are placed on the parameters 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 ​ ​^ 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.
g04caf 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 g04caf has to be in standard order given by the order of the factors. Let there be k factors, f1,f2,,fk in that order with levels l1,l2,,lk respectively. Standard order requires the levels of factor f1 are in order 1,2,,l1 and within each level of f1 the levels of f2 are in order 1,2,,l2 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 f1 are in order 1,2,,l1 and within each level of f1 the levels of f2 are in order 1,2,,l2 and so on. Any within block replication of treatment combinations must occur within the levels of fk.
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., f1 followed by f2, 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  

4 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

5 Arguments

1: n Integer Input
On entry: the number of observations.
Constraints:
  • n4;
  • if 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 i=1klfaci.
2: yn Real (Kind=nag_wp) array Input
On entry: the observations in standard order, see Section 3.
3: nfac Integer Input
On entry: k, the number of factors.
Constraint: nfac1.
4: lfacnfac Integer array Input
On entry: lfaci must contain the number of levels for the ith factor, for i=1,2,,k.
Constraint: lfaci2, for i=1,2,,k.
5: nblock Integer Input
On entry: the number of blocks. If there are no blocks, set nblock=0 or 1.
Constraints:
  • nblock0;
  • if nblock2, n/nblock must be a multiple of the number of treatment combinations, that is a multiple of i=1klfaci.
6: inter Integer Input
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: 0internfac.
7: irdf Integer Input
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 Input
On entry: the maximum number of terms in the analysis of variance table, see Section 9.
Constraint: mterm must be large enough to contain the terms specified by nfac, inter and nblock. If the routine exits with ifail2, the required minimum value of mterm is returned in itotal.
9: tablemterm5 Real (Kind=nag_wp) array Output
On exit: 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 itotal-1th 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: itotal Integer Output
On exit: the row in table corresponding to the total sum of squares. The number of treatment effects is itotal-3.
11: tmeanmaxt Real (Kind=nag_wp) array Output
On exit: 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.
12: maxt Integer Input
On entry: the maximum number of treatment means to be computed, see Section 9. If the value of maxt is too small for the required analysis then the minimum number is returned in imean1.
Constraint: maxt must be large enough for the number of means specified by lfac and inter; if inter=nfac then maxti=1klfaci+1-1.
13: emaxt Real (Kind=nag_wp) array Output
On exit: the estimated effects in the same order as for the means in tmean.
14: imeanmterm Integer array Output
On exit: 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 tmean1 up to tmeanimean1. Other effect means corresponding to the ith treatment effect, i=1,2,,itotal-3, are stored in tmeanimeani-1+1 up to tmeanimeani.
15: semeanmterm Real (Kind=nag_wp) array Output
On exit: the standard error of the difference between means corresponding to the ith treatment effect in the ANOVA table.
16: bmeannblock+1 Real (Kind=nag_wp) array Output
On exit: bmean1 contains the grand mean, if nblock>1, bmean2 up to bmeannblock+1 contain the block means.
17: rn Real (Kind=nag_wp) array Output
On exit: the residuals.
18: iwkn+3×nfac Integer array Workspace
19: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of -1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value -1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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).

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).
Errors or warnings detected by the routine:
ifail=1
On entry, inter=value.
Constraint: inter0.
On entry, inter=value and nfac=value.
Constraint: internfac.
On entry, irdf=value.
Constraint: irdf0.
On entry, n=value.
Constraint: n4.
On entry, nblock=value.
Constraint: nblock0.
On entry, nfac=value.
Constraint: nfac1.
ifail=2
On entry, i=value and lfaci=value
Constraint: lfaci2.
On entry, maxt=value
Constraint: maxtvalue.
On entry, mterm=value
Constraint: mtermvalue.
On entry, n=value and nblock=value.
Constraint: when nblock>1 , n must be a multiple of nblock.
On entry, the number of plots per block is incompatible with the number of plot factors.
ifail=3
On entry, the values of y are constant.
ifail=4
There are no degrees of freedom for the residual sum of squares. The standard errors and F-statistics cannot be computed.
The residual sum of squares is zero. The standard errors and F-statistics cannot be computed.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

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

g04caf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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 li 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 tables is:
i=1t k i +3.  
The number of treatment means is:
i =1 t j S i l j ,  
where Si 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, g02cbf or g02daf. 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 g04bbf or a prior call to g04caf if the data is reordered between calls. The residuals from the preliminary analysis are then input to g04caf. 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.

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 (g04cafe.f90)

10.2 Program Data

Program Data (g04cafe.d)

10.3 Program Results

Program Results (g04cafe.r)