PDF version (NAG web site
, 64bit version, 64bit version)
NAG Toolbox: nag_anova_random (g04bb)
Purpose
nag_anova_random (g04bb) computes the analysis of variance and treatment means and standard errors for a randomized block or completely randomized design.
Syntax
[
gmean,
bmean,
tmean,
tabl,
c,
irep,
r,
ef,
ifail] = g04bb(
y,
iblock,
nt,
it,
tol,
irdf, 'n',
n)
[
gmean,
bmean,
tmean,
tabl,
c,
irep,
r,
ef,
ifail] = nag_anova_random(
y,
iblock,
nt,
it,
tol,
irdf, 'n',
n)
Description
In a completely randomized design, 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$t$ treatments and
n_{t}${n}_{t}$ plots per treatment, the linear model is
where
y_{ij}${y}_{ij}$ is the
i$i$th observation for the
j$j$th treatment,
μ$\mu $ is the overall mean,
τ_{j}${\tau}_{j}$ is the effect of the
j$j$th treatment and
e_{ij}${e}_{ij}$ is the random error term. For a randomized block design, with
t$t$ treatments and
b$b$ blocks of
k$k$ plots, the linear model is
where
β_{i}${\beta}_{i}$ is the effect of the
i$i$th block and the
ij(l)$ij\left(l\right)$ notation indicates that the
l$l$th treatment is applied to the
j$j$th plot in the
i$i$th block.
The completely randomized design gives rise to a oneway 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, μ̂$\hat{\mu}$, is computed and subtracted from the observations to give y_{ij}^{ ′ } = y_{ij} − μ̂${y}_{ij}^{\prime}={y}_{ij}\hat{\mu}$. The estimated treatment effects, τ̂_{j}${\hat{\tau}}_{j}$ are then computed as the treatment means of the mean adjusted observations, y_{ij}^{ ′ }${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}^{ ′ }${y}_{ij}^{\prime}$ divided by the number of observations per treatment total, n_{j}${n}_{j}$. The final residuals are computed as r_{ij} = y_{ij}^{ ′ } − τ̂_{j}${r}_{ij}={y}_{ij}^{\prime}{\hat{\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(l)}^{ ′ } = y_{ij(l)} − μ̂${y}_{ij\left(l\right)}^{\prime}={y}_{ij\left(l\right)}\hat{\mu}$. The estimated block effects, ignoring treatment effects, β̂_{i}${\hat{\beta}}_{i}$, are then computed using the block means of the y_{ij(l)}^{ ′ }${y}_{ij\left(l\right)}^{\prime}$ and the unadjusted sum of squares computed as the sum of squared block totals for the y_{ij(l)}^{ ′ }${y}_{ij\left(l\right)}^{\prime}$ divided by number of plots per block, k$k$. The block adjusted observations are then computed as y_{ij(l)}^{ ′ ′ } = y_{i}^{ ′ }j_{(l)} = β̂_{i}${y}_{ij\left(l\right)}^{\prime \prime}={{y}_{i}^{\prime}j}_{\left(l\right)}={\hat{\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(l)}^{ ′ ′ }${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(l)}^{ ′ ′ }${y}_{ij\left(l\right)}^{\prime \prime}$ divided by the number of replicates to the treatments, r = bk / t$r=bk/t$. Finally the residuals, and hence the residual sum of squares, are given by r_{ij(l)} = y_{ij(l)}^{ ′ ′ } − τ̂_{l}${r}_{ij\left(l\right)}={y}_{ij\left(l\right)}^{\prime \prime}{\hat{\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
where
q$q$ is the vector of the treatment totals for block adjusted observations,
y_{ij(l)}^{ ′ ′ }${y}_{ij\left(l\right)}^{\prime \prime}$,
R$R$ is a diagonal matrix with
R_{ll}${R}_{ll}$ equal to the number of times the
l$l$th treatment is replicated, and
n${\mathbf{n}}$ is the
t$t$ by
b$b$ incidence matrix, with
N_{lj}${N}_{lj}$ equal to the number of times treatment
l$l$ occurs in block
j$j$. The solution to the equations can be written as
where
Ω$\Omega $ is a generalized inverse of
(R − NN^{T} / k)$(RN{N}^{\mathrm{T}}/k)$. The solution is found from the eigenvalue decomposition of
(R − NN^{T} / k)$(RN{N}^{\mathrm{T}}/k)$. The residuals are first calculated by subtracting the estimated treatment effects from the block adjusted observations to give
r_{ij(l)}^{ ′ } = y_{ij(l)}^{ ′ ′ } − τ̂_{l}${r}_{ij\left(l\right)}^{\prime}={y}_{ij\left(l\right)}^{\prime \prime}{\hat{\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(l)}^{ ′ }${r}_{ij\left(l\right)}^{\prime}$ have to be subtracted to give the correct residuals,
r_{ij(l)}${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$b1$, for the completely randomized and the complete block designs the degrees of freedom for the treatments is
t − 1$t1$. In the general case the degrees of freedom for treatments is the rank of the matrix
Ω$\Omega $. The
F$F$statistic given by the ratio of the treatment mean square to the residual mean square tests the hypothesis
The standard errors for the difference in treatment effects, or treatment means, for the completely randomized or the complete block designs, are given by:
where
s^{2}${s}^{2}$ is the residual mean square and
n_{j} = n_{j * } = b${n}_{j}={n}_{j*}=b$ in the complete block design. In the general case the variances of the treatment effects are given by
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 nonorthogonality 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
(R − NN^{T} / k)$(RN{N}^{\mathrm{T}}/k)$, 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.
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
Parameters
Compulsory Input Parameters
 1:
y(n) – double array
n, the dimension of the array, must satisfy the constraint
n ≥ 2${\mathbf{n}}\ge 2$ and if
abs(iblock) ≥ 2$\mathrm{abs}\left({\mathbf{iblock}}\right)\ge 2$,
n must be a multiple of
abs(iblock)$\mathrm{abs}\left({\mathbf{iblock}}\right)$.
The observations in the order as described by
iblock and
nt.
 2:
iblock – int64int32nag_int scalar
Indicates the block structure.
 abs(iblock) ≤ 1$\mathrm{abs}\left({\mathbf{iblock}}\right)\le 1$
 There are no blocks, i.e., it is a completely randomized design.
 iblock ≥ 2${\mathbf{iblock}}\ge 2$
 There are iblock blocks and the data should be input by blocks, i.e., y must contain the observations for block 1$1$ followed by the observations for block 2$2$, etc.
 iblock ≤ 2${\mathbf{iblock}}\le 2$
 There are abs(iblock)$\mathrm{abs}\left({\mathbf{iblock}}\right)$ blocks and the data is input in parallel with respect to blocks, i.e., y(1)${\mathbf{y}}\left(1\right)$ must contain the first observation for block 1$1$, y(2)${\mathbf{y}}\left(2\right)$ must contain the first observation for block 2 ⋯ y(abs(iblock))$2\cdots {\mathbf{y}}\left(\mathrm{abs}\left({\mathbf{iblock}}\right)\right)$ must contain the first observation for block abs(iblock),y(abs(iblock + 1))$\mathrm{abs}\left({\mathbf{iblock}}\right),{\mathbf{y}}\left(\mathrm{abs}({\mathbf{iblock}}+1)\right)$ must contain the second observation for block 1$1$, etc.
Constraint:
iblock = 1${\mathbf{iblock}}=1$,
2$2$ or
2$2$.
 3:
nt – int64int32nag_int scalar
The number of treatments. If only blocks are required in the analysis then set
nt = 1${\mathbf{nt}}=1$.
Constraints:
 if abs(iblock) ≥ 2$\mathrm{abs}\left({\mathbf{iblock}}\right)\ge 2$, nt ≥ 1${\mathbf{nt}}\ge 1$;
 otherwise nt ≥ 2${\mathbf{nt}}\ge 2$.
 4:
it( : $:$) – int64int32nag_int array

Note: the dimension of the array
it
must be at least
n${\mathbf{n}}$ if
nt ≥ 2${\mathbf{nt}}\ge 2$, and at least
1$1$ otherwise.
it(i)${\mathbf{it}}\left(\mathit{i}\right)$ indicates which of the
nt treatments plot
i$\mathit{i}$ received, for
i = 1,2, … ,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
If
nt = 1${\mathbf{nt}}=1$,
it is not referenced.
Constraint:
1 ≤ it(i) ≤ nt$1\le {\mathbf{it}}\left(\mathit{i}\right)\le {\mathbf{nt}}$, for
i = 1,2, … ,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
 5:
tol – double scalar
The tolerance value used to check for zero eigenvalues of the matrix
Ω$\Omega $. If
tol = 0.0${\mathbf{tol}}=0.0$ a default value of
10^{ − 5}${10}^{5}$ is used.
Constraint:
tol ≥ 0.0${\mathbf{tol}}\ge 0.0$.
 6:
irdf – int64int32nag_int scalar
An adjustment to the degrees of freedom for the residual and total.
 irdf ≥ 1${\mathbf{irdf}}\ge 1$
 The degrees of freedom for the total is set to n − irdf${\mathbf{n}}{\mathbf{irdf}}$ and the residual degrees of freedom adjusted accordingly.
 irdf = 0${\mathbf{irdf}}=0$
 The total degrees of freedom for the total is set to n − 1${\mathbf{n}}1$, as usual.
Constraint:
irdf ≥ 0${\mathbf{irdf}}\ge 0$.
Optional Input Parameters
 1:
n – int64int32nag_int scalar
Default:
The dimension of the array
y.
The number of observations.
Constraint:
n ≥ 2${\mathbf{n}}\ge 2$ and if
abs(iblock) ≥ 2$\mathrm{abs}\left({\mathbf{iblock}}\right)\ge 2$,
n must be a multiple of
abs(iblock)$\mathrm{abs}\left({\mathbf{iblock}}\right)$.
Input Parameters Omitted from the MATLAB Interface
 ldtabl ldc wk
Output Parameters
 1:
gmean – double scalar
The grand mean, μ̂$\hat{\mu}$.
 2:
bmean(abs(iblock)$\mathrm{abs}\left({\mathbf{iblock}}\right)$) – double array
If
abs(iblock) ≥ 2$\mathrm{abs}\left({\mathbf{iblock}}\right)\ge 2$,
bmean(j)${\mathbf{bmean}}\left(\mathit{j}\right)$ contains the mean for the
j$\mathit{j}$th block,
β̂_{j}${\hat{\beta}}_{\mathit{j}}$, for
j = 1,2, … ,b$\mathit{j}=1,2,\dots ,b$.
 3:
tmean(nt) – double array
If
nt ≥ 2${\mathbf{nt}}\ge 2$,
tmean(l)${\mathbf{tmean}}\left(\mathit{l}\right)$ contains the (adjusted) mean for the
l$\mathit{l}$th treatment,
μ̂^{*} + τ̂_{l}${\hat{\mu}}^{*}+{\hat{\tau}}_{\mathit{l}}$, for
l = 1,2, … ,t$\mathit{l}=1,2,\dots ,t$, where
μ̂^{*}${\hat{\mu}}^{*}$ is the mean of the treatment adjusted observations,
y_{ij(l)} − τ̂_{l}${y}_{ij\left(l\right)}{\hat{\tau}}_{l}$.
 4:
tabl(ldtabl,5$5$) – double array
ldtabl ≥ 4$\mathit{ldtabl}\ge 4$.
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$F$statistic and column 5 the significance level of the F$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$F$statistics and significance are computed for Treatments and Blocks, if present. Any unfilled cells are set to zero.
 5:
c(ldc,nt) – double array
ldc ≥ nt$\mathit{ldc}\ge {\mathbf{nt}}$.
If
nt ≥ 2${\mathbf{nt}}\ge 2$, the upper triangular part of
c contains the variancecovariance matrix of the treatment effects, the strictly lower triangular part contains the standard errors of the difference between two treatment effects (means), i.e.,
c(i,j)${\mathbf{c}}\left(\mathit{i},\mathit{j}\right)$ contains the covariance of treatment
i$\mathit{i}$ and
j$\mathit{j}$ if
j ≥ i$\mathit{j}\ge \mathit{i}$ and the standard error of the difference between treatment
i$\mathit{i}$ and
j$\mathit{j}$ if
j < i$\mathit{j}<\mathit{i}$, for
i = 1,2, … ,t$\mathit{i}=1,2,\dots ,t$ and
j = 1,2, … ,t$\mathit{j}=1,2,\dots ,t$.
 6:
irep(nt) – int64int32nag_int array
If
nt ≥ 2${\mathbf{nt}}\ge 2$, the treatment replications,
R_{ll}${R}_{\mathit{l}\mathit{l}}$, for
l = 1,2, … ,nt$\mathit{l}=1,2,\dots ,{\mathbf{nt}}$.
 7:
r(n) – double array
The residuals,
r_{i}${r}_{\mathit{i}}$, for
i = 1,2, … ,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
 8:
ef(nt) – double array
If
nt ≥ 2${\mathbf{nt}}\ge 2$, the canonical efficiency factors.
 9:
ifail – int64int32nag_int scalar
ifail = 0${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see
[Error Indicators and Warnings]).
Error Indicators and Warnings
Note: nag_anova_random (g04bb) 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.
 ifail = 1${\mathbf{ifail}}=1$
On entry,  n < 2${\mathbf{n}}<2$, 
or  nt ≤ 0${\mathbf{nt}}\le 0$, 
or  nt = 1${\mathbf{nt}}=1$ and abs(iblock) ≤ 1$\mathrm{abs}\left({\mathbf{iblock}}\right)\le 1$, 
or  ldtabl < 4$\mathit{ldtabl}<4$, 
or  ldc < nt$\mathit{ldc}<{\mathbf{nt}}$, 
or  tol < 0.0${\mathbf{tol}}<0.0$, 
or  irdf < 0${\mathbf{irdf}}<0$. 
 ifail = 2${\mathbf{ifail}}=2$
On entry,  abs(iblock) ≥ 2$\mathrm{abs}\left({\mathbf{iblock}}\right)\ge 2$ and n is not a multiple of abs(iblock)$\mathrm{abs}\left({\mathbf{iblock}}\right)$. 
 ifail = 3${\mathbf{ifail}}=3$
On entry,  it(i) < 1${\mathbf{it}}\left(i\right)<1$ or it(i) > nt${\mathbf{it}}\left(i\right)>{\mathbf{nt}}$ for some i$i$ when nt ≥ 2${\mathbf{nt}}\ge 2$, 
or  no value of it = j${\mathbf{it}}=j$ for some j = 1,2, … ,nt$j=1,2,\dots ,{\mathbf{nt}}$, when nt ≥ 2${\mathbf{nt}}\ge 2$. 
 ifail = 4${\mathbf{ifail}}=4$

On entry,  the values of y are constant. 
 ifail = 5${\mathbf{ifail}}=5$
A computed standard error is zero due to rounding errors, or the eigenvalue computation failed to converge. Both are unlikely error exits.
 W ifail = 6${\mathbf{ifail}}=6$
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.
 W ifail = 7${\mathbf{ifail}}=7$
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$s$ or
s^{2}${s}^{2}$.
 W ifail = 8${\mathbf{ifail}}=8$
The design is disconnected; the standard errors may not be valid. The design may be nested.
Accuracy
The algorithm used by
nag_anova_random (g04bb), described in
Section [Description], achieves greater accuracy than the traditional algorithms based on the subtraction of sums of squares.
Further Comments
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
μ̂$\hat{\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 Graeco–Latin squares one or more of the blocking factors has to be removed in a preliminary analysis before the final analysis using calls to
nag_anova_random (g04bb) or
nag_anova_rowcol (g04bc). The residuals from the preliminary analysis are then input to
nag_anova_random (g04bb). 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_correg_linregs_noconst (g02cb) or
nag_correg_linregm_fit (g02da). 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.
Example
Open in the MATLAB editor:
nag_anova_random_example
function nag_anova_random_example
y = [1;
5;
4;
5;
10;
6;
2;
9;
3;
4;
8;
6;
2;
4;
7;
6;
7;
5;
5;
7;
2;
7;
2;
4;
8;
4;
2;
10;
8;
7];
iblock = int64(10);
nt = int64(6);
it = [int64(1);2;3;1;2;4;1;3;5;1;4;6;1;5;6;2;3;6;2;4;5;2;5;6;3;4;5;3;4;6];
tol = 5e06;
irdf = int64(0);
[gmean, bmean, tmean, table, c, irep, r, ef, ifail] = ...
nag_anova_random(y, iblock, nt, it, tol, irdf)
gmean =
5.3333
bmean =
3.3333
7.0000
4.6667
6.0000
4.3333
6.0000
4.6667
4.3333
4.6667
8.3333
tmean =
2.5000
7.2500
8.0833
5.9167
2.9167
5.3333
table =
9.0000 60.0000 6.6667 4.7872 0.0039
5.0000 101.7778 20.3556 14.6170 0.0000
15.0000 20.8889 1.3926 0 0
29.0000 182.6667 0 0 0
c =
0.2901 0.0580 0.0580 0.0580 0.0580 0.0580
0.8344 0.2901 0.0580 0.0580 0.0580 0.0580
0.8344 0.8344 0.2901 0.0580 0.0580 0.0580
0.8344 0.8344 0.8344 0.2901 0.0580 0.0580
0.8344 0.8344 0.8344 0.8344 0.2901 0.0580
0.8344 0.8344 0.8344 0.8344 0.8344 0.2901
irep =
5
5
5
5
5
5
r =
1.1111
0.3611
1.4722
0.7222
0.9722
1.6944
0.6667
0.7500
0.0833
0.0833
0.6667
0.7500
1.2500
0.3333
0.9167
0.3611
0.1944
0.5556
1.5556
1.7778
0.2222
0.5833
0.0833
0.5000
0.8889
0.9444
0.0556
0.0278
0.1944
0.2222
ef =
0
0.8000
0.8000
0.8000
0.8000
0.8000
ifail =
0
Open in the MATLAB editor:
g04bb_example
function g04bb_example
y = [1;
5;
4;
5;
10;
6;
2;
9;
3;
4;
8;
6;
2;
4;
7;
6;
7;
5;
5;
7;
2;
7;
2;
4;
8;
4;
2;
10;
8;
7];
iblock = int64(10);
nt = int64(6);
it = [int64(1);2;3;1;2;4;1;3;5;1;4;6;1;5;6;2;3;6;2;4;5;2;5;6;3;4;5;3;4;6];
tol = 5e06;
irdf = int64(0);
[gmean, bmean, tmean, table, c, irep, r, ef, ifail] = g04bb(y, iblock, nt, it, tol, irdf)
gmean =
5.3333
bmean =
3.3333
7.0000
4.6667
6.0000
4.3333
6.0000
4.6667
4.3333
4.6667
8.3333
tmean =
2.5000
7.2500
8.0833
5.9167
2.9167
5.3333
table =
9.0000 60.0000 6.6667 4.7872 0.0039
5.0000 101.7778 20.3556 14.6170 0.0000
15.0000 20.8889 1.3926 0 0
29.0000 182.6667 0 0 0
c =
0.2901 0.0580 0.0580 0.0580 0.0580 0.0580
0.8344 0.2901 0.0580 0.0580 0.0580 0.0580
0.8344 0.8344 0.2901 0.0580 0.0580 0.0580
0.8344 0.8344 0.8344 0.2901 0.0580 0.0580
0.8344 0.8344 0.8344 0.8344 0.2901 0.0580
0.8344 0.8344 0.8344 0.8344 0.8344 0.2901
irep =
5
5
5
5
5
5
r =
1.1111
0.3611
1.4722
0.7222
0.9722
1.6944
0.6667
0.7500
0.0833
0.0833
0.6667
0.7500
1.2500
0.3333
0.9167
0.3611
0.1944
0.5556
1.5556
1.7778
0.2222
0.5833
0.0833
0.5000
0.8889
0.9444
0.0556
0.0278
0.1944
0.2222
ef =
0
0.8000
0.8000
0.8000
0.8000
0.8000
ifail =
0
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013