g04 Chapter Contents
g04 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_anova_random (g04bbc)

## 1  Purpose

nag_anova_random (g04bbc) computes the analysis of variance and treatment means and standard errors for a randomized block or completely randomized design.

## 2  Specification

 #include #include
 void nag_anova_random (Integer n, const double y[], Nag_Blocks blocks, Integer iblock, Integer nt, const Integer it[], double *gmean, double bmean[], double tmean[], double table[], double c[], Integer tdc, Integer irep[], double r[], double ef[], double tol, Integer irdf, NagError *fail)

## 3  Description

In a completely randomized design the experimental material is divided into a number of units, or plots, to which a treatment can be applied. In a randomized block design the units are grouped into blocks so that the variation within blocks is less than the variation between blocks. If every treatment is applied to one plot in each block it is a complete block design. If there are fewer plots per block than treatments then the design will be an incomplete block design and may be balanced or partially balanced.
For a completely randomized design, with $t$ treatments and ${n}_{t}$ plots per treatment, the linear model is
 $y ij = μ + τ j + e ij , j = 1 , 2 , … , t ; ​ i = 1 , 2 , … , n j ,$
where ${y}_{ij}$ is the $i$th observation for the $j$th treatment, $\mu$ is the overall mean, ${\tau }_{j}$ is the effect of the $j$th treatment and ${e}_{ij}$ is the random error term. For a randomized block design, with $t$ treatments and $b$ blocks of $k$ plots, the linear model is
 $y ij l = μ + β i + τ l + e ij , i = 1 , 2 , … , b ; ​ j = 1 , 2 , … , k ; ​ l = 1 , 2 , … , t ,$
where ${\beta }_{i}$ is the effect of the $i$th block and the $ij\left(l\right)$ notation indicates that the $l$th treatment is applied to the $i$th plot in the $j$th block.
The completely randomized design gives rise to a one-way analysis of variance. The treatments do not have to be equally replicated, i.e., do not have to occur the same number of times. First the overall mean, $\stackrel{^}{\mu }$, is computed and subtracted from the observations to give ${y}_{ij}^{\prime }={y}_{ij}-\stackrel{^}{\mu }$. The estimated treatment effects, ${\stackrel{^}{\tau }}_{j}$ are then computed as the treatment means of the mean adjusted observations, ${y}_{ij}^{\prime }$, and the treatment sum of squares can be computed from the sum of squares of the treatment totals of the ${y}_{ij}^{\prime }$ divided by the number of observations per treatment total, ${n}_{j}$. The final residuals are computed as ${r}_{ij}={y}_{ij}^{\prime }-{\stackrel{^}{\tau }}_{j}$, and, from the residuals, the residual sum of squares is calculated.
For the randomized block design the mean is computed and subtracted from the observations to give ${y}_{ij\left(l\right)}^{\prime }={y}_{ij\left(l\right)}-\stackrel{^}{\mu }$. The estimated block effects, ignoring treatment effects, ${\stackrel{^}{\beta }}_{i}$, are then computed using the block means of the ${y}_{ij\left(l\right)}^{\prime }$ and the unadjusted sum of squares computed as the sum of squared block totals for the ${y}_{ij\left(l\right)}^{\prime }$ divided by number of plots per block, $k$. The block adjusted observations are then computed as ${y}_{ij\left(l\right)}^{\prime \prime }={y}_{ij\left(l\right)}^{\prime }={\stackrel{^}{\beta }}_{i}$. In the case of the complete block design, with the same replication for each treatment within each block, the blocks and treatments are orthogonal, and so the treatment effects are estimated as the treatment means of the block adjusted observations, ${y}_{ij\left(l\right)}^{\prime \prime }$. The treatment sum of squares is computed as the sum of squared treatment totals of the ${y}_{ij\left(l\right)}^{\prime \prime }$ divided by the number of replicates to the treatments, $r=bk/t$. Finally the residuals, and hence the residual sum of squares, are given by ${r}_{ij\left(l\right)}={y}_{ij\left(l\right)}^{\prime \prime }-{\stackrel{^}{\tau }}_{l}$.
For a design without the same replication for each treatment within each block the treatments and the blocks will not be orthogonal, so the treatments adjusted for blocks need to be computed. The adjusted treatment effects are found as the solution to the equations
 $R - N N T / k τ ^ = q$
where $q$ is the vector of the treatment totals for block adjusted observations, ${y}_{ij\left(l\right)}^{\prime \prime }$, $R$ is a diagonal matrix with ${R}_{ll}$ equal to the number of times the $l$th treatment is replicated, and $N$ is the $t$ by $b$ incidence matrix, with ${N}_{lj}$ equal to the number of times treatment $l$ occurs in block $j$. The solution to the equations can be written as
 $τ ^ = Ω q$
where $\Omega$ is a generalized inverse of $\left({R-NN}^{\mathrm{T}}/k\right)$. The solution is found from the eigenvalue decomposition of $\left({R-NN}^{\mathrm{T}}/k\right)$. The residuals are first calculated by subtracting the estimated treatment effects from the block adjusted observations to give ${r}_{ij\left(l\right)}^{\prime }={y}_{ij\left(l\right)}^{\prime \prime }-{\stackrel{^}{\tau }}_{l}$. However, since only the unadjusted block effects have been removed and blocks and treatments are not orthogonal, the block means of the ${r}_{ij\left(l\right)}^{\prime }$ have to be subtracted to give the correct residuals, ${r}_{ij\left(l\right)}$ and residual sum of squares.
The mean squares are computed as the sum of squares divided by the degrees of freedom. The degrees of freedom for the unadjusted blocks is $b-1$, for the completely randomized and the complete block designs the degrees of freedom for the treatments is $t-1$. In the general case the degrees of freedom for treatments is the rank of the matrix $\Omega$. The $F$-statistic given by the ratio of the treatment mean square to the residual mean square tests the hypothesis
 $H 0 : τ 1 = τ 2 = ⋯ = τ t = 0 .$
The standard errors for the difference in treatment effects, or treatment means, for the completely randomized or the complete block designs, are given by
 $se τ j - τ j* = 1 n j + 1 n j* s 2$
where ${s}^{2}$ is the residual mean square and ${n}_{j}={n}_{j*}=b$ in the complete block design. In the general case the variances of the treatment effects are given by
 $varτ = Ω s 2$
from which the appropriate standard errors of the difference between treatment effects or the difference between adjusted means can be calculated.
In the complete block design all the information on the treatment effects is given by the within block analysis. In other designs there may be a loss of information due to the non-orthogonality of treatments and blocks. The efficiency of the within block analysis in these cases is given by the (canonical) efficiency factors, these are the nonzero eigenvalues of the matrix $\left({R-NN}^{\mathrm{T}}/k\right)$, divided by the number of replicates in the case of equal replication, or by the mean of the number of replicates in the unequally replicated case, see John (1987). If more than one eigenvalue is zero then the design is said to be disconnected and some treatments can only be compared using a between block analysis.

## 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 (1987) Cyclic Designs Chapman and Hall
John J A and Quenouille M H (1977) Experiments: Design and Analysis Griffin
Searle S R (1971) Linear Models Wiley

## 5  Arguments

1:     nIntegerInput
On entry: the number of observations, $n$.
Constraints:
• ${\mathbf{n}}\ge 2$;
• if ${\mathbf{iblock}}\ge 2$, n must be a multiple of iblock.
2:     y[n]const doubleInput
On entry: the observations in the order as described by blocks and nt.
3:     blocksNag_BlocksInput
On entry: blocks indicates the block structure.
${\mathbf{blocks}}=\mathrm{Nag_NoBlocks}$
There are no blocks, i.e., it is a completely randomized design.
${\mathbf{blocks}}=\mathrm{Nag_SerialBlocks}$
The data should be input by blocks, i.e., y must contain the observations for block 1 followed by the observations for block 2, etc.
${\mathbf{blocks}}=\mathrm{Nag_ParallelBlocks}$
The data is input in parallel with respect to blocks, i.e., ${\mathbf{y}}\left[0\right]$ must contain the first observation for block 1, ${\mathbf{y}}\left[1\right]$ must contain the first observation for block $2\dots {\mathbf{y}}\left[{\mathbf{iblock}}-1\right]$ must contain the first observation for block iblock, ${\mathbf{y}}\left[{\mathbf{iblock}}\right]$ must contain the second observation for block 1, etc.
Constraint: ${\mathbf{blocks}}=\mathrm{Nag_NoBlocks}$, $\mathrm{Nag_SerialBlocks}$ or $\mathrm{Nag_ParallelBlocks}$.
4:     iblockIntegerInput
On entry: iblock indicates the number of blocks, $b$.
Constraint: if ${\mathbf{blocks}}=\mathrm{Nag_SerialBlocks}$ or ${\mathbf{blocks}}=\mathrm{Nag_ParallelBlocks}$ then ${\mathbf{iblock}}\ge 2$; it is not referenced otherwise.
5:     ntIntegerInput
On entry: the number of treatments, $t$. If only blocks are required in the analysis then set ${\mathbf{nt}}=1$.
Constraints:
• if ${\mathbf{iblock}}\ge 2$, ${\mathbf{nt}}\ge 1$;
• otherwise ${\mathbf{nt}}\ge 2$.
6:     it[n]const IntegerInput
On entry: ${\mathbf{it}}\left[\mathit{i}-1\right]$ indicates which of the nt treatments plot $\mathit{i}$ received, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$. If ${\mathbf{nt}}=1$, it is not referenced.
Constraint: $1\le {\mathbf{it}}\left[\mathit{i}-1\right]\le {\mathbf{nt}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
7:     gmeandouble *Output
On exit: the grand mean, $\stackrel{^}{\mu }$.
8:     bmean[$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{iblock}}\right)$]doubleOutput
On exit: if ${\mathbf{blocks}}=\mathrm{Nag_SerialBlocks}$ or $\mathrm{Nag_ParallelBlocks}$, ${\mathbf{bmean}}\left[j-1\right]$ contains the mean for the $j$th block, ${\stackrel{^}{\beta }}_{j}$ for $j=1,2,\dots ,b$. It is not referenced otherwise.
9:     tmean[nt]doubleOutput
On exit: if ${\mathbf{nt}}\ge 2$, ${\mathbf{tmean}}\left[\mathit{l}-1\right]$ contains the (adjusted) mean for the $\mathit{l}$th treatment, ${\stackrel{^}{\mu }}^{*}+{\stackrel{^}{\tau }}_{\mathit{l}}$, for $\mathit{l}=1,2,\dots ,t$, where ${\stackrel{^}{\mu }}^{*}$ is the mean of the treatment adjusted observations, ${y}_{ij\left(l\right)}-{\stackrel{^}{\tau }}_{l}$.
10:   table[$4×5$]doubleOutput
Note: the $\left(i,j\right)$th element of the matrix is stored in ${\mathbf{table}}\left[\left(i-1\right)×5+j-1\right]$.
On exit: the analysis of variance table. Column 1 contains the degrees of freedom, column 2 the sum of squares, and where appropriate, column 3 the mean squares, column 4 the $F$-statistic and column 5 the significance level of the $F$-statistic. Row 1 is for Blocks, row 2 for Treatments, row 3 for Residual and row 4 for Total. Mean squares are computed for all but the Total row; $F$-statistics and significance are computed for Treatments and Blocks, if present. Any unfilled cells are set to zero.
11:   c[${\mathbf{nt}}×{\mathbf{tdc}}$]doubleOutput
On exit: if ${\mathbf{nt}}\ge 2$, the upper triangular part of c contains the variance-covariance matrix of the treatment effects, the strictly lower triangular part contains the standard errors of the difference between two treatment effects (means), i.e., ${\mathbf{c}}\left[\left(i-1\right)×{\mathbf{tdc}}+j-1\right]$ contains the covariance of treatment $i$ and $j$ if $j\ge i$ and the standard error of the difference between treatment $i$ and $j$ if $j for $i=1,2,\dots ,t$ and $j=1,2,\dots ,t$.
12:   tdcIntegerInput
On entry: the stride separating matrix column elements in the array c.
Constraint: ${\mathbf{tdc}}\ge {\mathbf{nt}}$.
13:   irep[nt]IntegerOutput
On exit: if ${\mathbf{nt}}\ge 2$, the treatment replications, ${R}_{\mathit{l}\mathit{l}}$, for $\mathit{l}=1,2,\dots ,{\mathbf{nt}}$.
14:   r[n]doubleOutput
On exit: the residuals, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
15:   ef[nt]doubleOutput
On exit: if ${\mathbf{nt}}\ge 2$, the canonical efficiency factors.
16:   toldoubleInput
On entry: the tolerance value used to check for zero eigenvalues of the matrix $\Omega$. If ${\mathbf{tol}}=0.0$ a default value of ${10}^{-5}$ is used.
Constraint: ${\mathbf{tol}}\ge 0.0$.
17:   irdfIntegerInput
On entry: an adjustment to the degrees of freedom for the residual and total.
${\mathbf{irdf}}\ge 1$
The degrees of freedom for the total is set to ${\mathbf{n}}-{\mathbf{irdf}}$ and the residual degrees of freedom adjusted accordingly.
${\mathbf{irdf}}=0$
The total degrees of freedom for the total is set to ${\mathbf{n}}-1$, as usual.
Constraint: ${\mathbf{irdf}}\ge 0$.
18:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{tdc}}=〈\mathit{\text{value}}〉$ while ${\mathbf{nt}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdc}}\ge {\mathbf{nt}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_ARRAY_CONSTANT
On entry, the elements of the array y are constant.
On entry, argument blocks had an illegal value.
NE_G04BB_CONV
The eigenvalue computation has failed to converge.
This is an unlikely error exit.
NE_G04BB_DESIGN
The design is disconnected; the standard errors may not be valid. The design may be nested.
NE_G04BB_RES_DF
The residual degrees of freedom or the residual sum of squares are zero, columns 3, 4 and 5 of the analysis of variance table will not be computed and the matrix of standard errors and covariances, C, will not be scaled by $s$ or the square of $s$.
NE_G04BB_STDERR
A computed standard error is zero due to rounding errors.
This is an unlikely error exit.
NE_G04BB_TREAT
The treatments are totally confounded with blocks, so the treatment sum of squares and degrees of freedom are zero. The analysis of variance table is not computed, except for block and total sums of squares and degrees of freedom.
NE_INT
On entry, ${\mathbf{iblock}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{iblock}}\ge 2$ when ${\mathbf{blocks}}=\mathrm{Nag_NoBlocks}$.
On entry, ${\mathbf{nt}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nt}}\ge 2$ when ${\mathbf{blocks}}=\mathrm{Nag_NoBlocks}$.
NE_INT_2
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$, ${\mathbf{iblock}}=〈\mathit{\text{value}}〉$.
Constraint: when ${\mathbf{iblock}}\ge 2$, n must be a multiple of iblock.
NE_INT_ARG_LT
On entry, ${\mathbf{irdf}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{irdf}}\ge 0$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{nt}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nt}}\ge 1$.
NE_INTARR
On entry, ${\mathbf{it}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{it}}\left[\mathit{i}-1\right]\le {\mathbf{nt}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
NE_IT_ARRAY
No value of ${\mathbf{it}}\left[j-1\right]=j$ for some $j=1,2,\dots ,{\mathbf{nt}}$.
NE_REAL_ARG_LT
On entry, tol must not be less than 0.0: ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.

## 7  Accuracy

The algorithm used by nag_anova_random (g04bbc), described in Section 3, achieves greater accuracy than the traditional algorithms based on the subtraction of sums of squares.

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 $\stackrel{^}{\mu }$. 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 designs such as Latin squares one more of the blocking factors has to be removed in a preliminary analysis before the final analysis using extra calls to nag_anova_random (g04bbc). The residuals from the preliminary analysis are then input to nag_anova_random (g04bbc). 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.
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 results from those functions can be used to test for the significance of the covariates. To test the significance of the treatment effects after fitting the covariate, the residual sum of squares from the regression should be compared with the residual sum of squares obtained from the equivalent regression but using the residuals from fitting blocks only.

## 9  Example

The data, given by John and Quenouille (1977), is for a balanced incomplete block design with 10 blocks and 6 treatments and with 3 plots per block. The observations are the degree of pain experienced and the treatments are penicillin of different potency. The data is input and the analysis of variance table and treatment means are printed.

### 9.1  Program Text

Program Text (g04bbce.c)

### 9.2  Program Data

Program Data (g04bbce.d)

### 9.3  Program Results

Program Results (g04bbce.r)