hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_anova_hier2 (g04ag)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


nag_anova_hier2 (g04ag) performs an analysis of variance for a two-way hierarchical classification with subgroups of possibly unequal size, and also computes the treatment group and subgroup means. A fixed effects model is assumed.


[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = g04ag(y, lsub, nobs, 'n', n, 'k', k, 'l', l)
[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = nag_anova_hier2(y, lsub, nobs, 'n', n, 'k', k, 'l', l)


In a two-way hierarchical classification, there are k (2) treatment groups, the ith of which is subdivided into li treatment subgroups. The jth subgroup of group i contains nij observations, which may be denoted by
The general observation is denoted by ymij, being the mth observation in subgroup j of group i, for 1ik, 1jli, 1mnij.
The following quantities are computed
(i) The subgroup means
(ii) The group means
y-.i.=j= 1lim= 1nijymij j= 1linij  
(iii) The grand mean
y-=i=1kj=1lim=1nijymij i=1kj=1linij  
(iv) The number of observations in each group
ni.= j= 1 li nij  
(v) Sums of squares
Between groups =SSg = i=1 k ni. y-.i.-y- 2 Between subgroups within groups =SSsg= i=1 k j=1 li nij y.ij-y-.i. 2 Residual (within subgroups) =SSres= i=1 k j=1 li m=1 nij ymij-y-.ij 2 =SStot-SSg-SSsg Corrected total =SStot= i=1 k j=1 li m=1 nij ymij-y- 2  
(vi) Degrees of freedom of variance components
Between groups: k-1
Subgroups within groups: l-k
Residual: n-l
Total: n-1
  • l=i=1kli,
  • n=i=1kni.
(vii) F ratios. These are the ratios of the group and subgroup mean squares to the residual mean square.
Groups F1= Between groups sum of squares/k-1 Residual sum of squares/n-l = SSg/k-1 SSres/n-l
Subgroups F2= Between subgroups (within group) sum of squares/l-k Residual sum of squares/n-l = SSsg/l-k SSres/n-l
If either F ratio exceeds 9999.0, the value 9999.0 is assigned instead.
(viii) f significances. The probability of obtaining a value from the appropriate F-distribution which exceeds the computed mean square ratio.
Groups p 1 = Prob F k - 1 , n - l > F 1  
Subgroups p 2 = Prob F l - k , n - l > F 2  
where Fν1,ν2 denotes the central F-distribution with degrees of freedom ν1 and ν2.
If any Fi=9999.0, then pi is set to zero, i=1,2.


Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Moore P G, Shirley E A and Edwards D E (1972) Standard Statistical Calculations Pitman


Compulsory Input Parameters

1:     yn – double array
The elements of y must contain the observations ymij in the following order:
yn1l11l1,, y1ij,,ynijij,,y1klk,, ynklkklk.  
In words, the ordering is by group, and within each group is by subgroup, the members of each subgroup being in consecutive locations in y.
2:     lsubk int64int32nag_int array
The number of subgroups within group i, li, for i=1,2,,k.
Constraint: lsubi>0, for i=1,2,,k.
3:     nobsl int64int32nag_int array
The numbers of observations in each subgroup, nij, in the following order:
Constraint: n=i=1kj=1linij, that is n=i=1lnobsi and nobsi>0, for i=1,2,,l.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array y.
n, the total number of observations.
2:     k int64int32nag_int scalar
Default: the dimension of the array lsub.
k, the number of groups.
Constraint: k2.
3:     l int64int32nag_int scalar
Default: the dimension of the array nobs.
l, the total number of subgroups.
Constraint: l=i=1klsubi.

Output Parameters

1:     ngpk int64int32nag_int array
The total number of observations in group i, ni., for i=1,2,,k.
2:     gbark – double array
The mean for group i, y-.i., for i=1,2,,k.
3:     sgbarl – double array
The subgroup means, y-.ij, in the following order:
4:     gm – double scalar
The grand mean, y-.
5:     ss4 – double array
Contains the sums of squares for the analysis of variance, as follows;
  • ss1= Between group sum of squares, ssg,
  • ss2= Between subgroup within groups sum of squares, sssg,
  • ss3= Residual sum of squares, ssres,
  • ss4= Corrected total sum of squares, sstot.
6:     idf4 int64int32nag_int array
Contains the degrees of freedom attributable to each sum of squares in the analysis of variance, as follows:
  • idf1= Degrees of freedom for between group sum of squares,
  • idf2= Degrees of freedom for between subgroup within groups sum of squares,
  • idf3= Degrees of freedom for residual sum of squares,
  • idf4= Degrees of freedom for corrected total sum of squares.
7:     f2 – double array
Contains the mean square ratios, F1 and F2, for the between groups variation, and the between subgroups within groups variation, with respect to the residual, respectively.
8:     fp2 – double array
Contains the significances of the mean square ratios, p1 and p2 respectively.
9:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and 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.

On entry,k1.
On entry,lsubi0, for some i=1,2,,k.
On entry,li=1klsubi
On entry,nobsi0, for some i=1,2,,l.
On entry,ni=1lnobsi.
W  ifail=6
The total corrected sum of squares is zero, indicating that all the data values are equal. The means returned are therefore all equal, and the sums of squares are zero. No assignments are made to idf, f, and fp.
W  ifail=7
The residual sum of squares is zero. This arises when either each subgroup contains exactly one observation, or the observations within each subgroup are equal. The means, sums of squares, and degrees of freedom are computed, but no assignments are made to f and fp.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


The computations are believed to be stable.

Further Comments

The time taken by nag_anova_hier2 (g04ag) increases approximately linearly with the total number of observations, n.


This example has two groups, the first of which consists of five subgroups, and the second of three subgroups. The numbers of observations in each subgroup are not equal. The data represent the percentage stretch in the length of samples of sack kraft drawn from consignments (subgroups) received over two years (groups). For details see Moore et al. (1972).
function g04ag_example

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

% Observations in two groups with 5 and 3 sub-groups respectively
y = [2.1     2.4     2       2       2   ...
     2.4     2.1     2.2     ...
     2.4     2.2     2.6     ...
     2.4     2.4     2.5     ...
     1.9     1.7     ...
     2.1     1.5     2       ...
     1.9     1.7     1.9     1.9     1.9  ...
     2       2.1     2.3];
k    = 2;
lsub = [int64(5);                 3];
nobs = [int64(5); 3; 3; 3; 2;     3;5;3];
n = sum(nobs);

fprintf('Data values\n\n Group  Subgroup  Observations\n');
nsub = 0;
nlo  = 1;
for i = 1:k
  for j = 1:lsub(i)
    nsub = nsub + 1;
    nhi = nlo + nobs(nsub) - 1;
    fprintf('%5d%9d   ', i, j);
    nlo = nhi + 1;

% Perform ANOVA
[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = ...
  g04ag(y, lsub, nobs);

% Display results
fprintf('\nSubgroup means\n\n');
fprintf('   Group  Subgroup  Mean\n');
ii = 0;
for i = 1:k
  for j = 1:lsub(i)
    ii = ii + 1;
    fprintf('%6d%8d%10.2f\n', i, j, sgbar(ii));
fprintf('%s%5.2f%s%2d%s\n', '    Group 1 mean =', gbar(1), ...
	'   (', ngp(1), ' observations)');
fprintf('%s%5.2f%s%2d%s\n', '    Group 2 mean =', gbar(2), ...
	'   (', ngp(2), ' observations)');
fprintf('%s%5.2f%s%2d%s\n', '    Grand mean   =', gm, ...
	'   (', n, ' observations)');
fprintf('\nAnalysis of variance table\n\n');
fprintf('   Source                SS    DF  F ratio  Sig\n\n');
fprintf('%s%6.3f%5d%7.2f%8.3f\n', 'Between groups        ', ...
	ss(1), idf(1), f(1), fp(1));
fprintf('%s%6.3f%5d%7.2f%8.3f\n', 'Between subgroups     ', ...
	ss(2), idf(2), f(2), fp(2));
fprintf('%s%6.3f%5d\n', 'Residual              ', ss(3), idf(3));
fprintf('\n%s%6.3f%5d\n', 'Total                 ', ss(4), idf(4));

g04ag example results

Data values

 Group  Subgroup  Observations
    1        1    2.1 2.4 2.0 2.0 2.0
    1        2    2.4 2.1 2.2
    1        3    2.4 2.2 2.6
    1        4    2.4 2.4 2.5
    1        5    1.9 1.7
    2        1    2.1 1.5 2.0
    2        2    1.9 1.7 1.9 1.9 1.9
    2        3    2.0 2.1 2.3

Subgroup means

   Group  Subgroup  Mean
     1       1      2.10
     1       2      2.23
     1       3      2.40
     1       4      2.43
     1       5      1.80
     2       1      1.87
     2       2      1.86
     2       3      2.13

    Group 1 mean = 2.21   (16 observations)
    Group 2 mean = 1.94   (11 observations)
    Grand mean   = 2.10   (27 observations)

Analysis of variance table

   Source                SS    DF  F ratio  Sig

Between groups         0.475    1  16.15   0.001
Between subgroups      0.816    6   4.63   0.005
Residual               0.559   19

Total                  1.850   26

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015