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_dummyvars (g04ea)

Purpose

nag_anova_dummyvars (g04ea) computes orthogonal polynomial or dummy variables for a factor or classification variable.

Syntax

[x, rep, ifail] = g04ea(typ, levels, ifact, v, 'n', n)
[x, rep, ifail] = nag_anova_dummyvars(typ, levels, ifact, v, 'n', n)

Description

In the analysis of an experimental design using a general linear model the factors or classification variables that specify the design have to be coded as dummy variables. nag_anova_dummyvars (g04ea) computes dummy variables that can then be used in the fitting of the general linear model using nag_correg_linregm_fit (g02da).
If the factor of length nn has kk levels then the simplest representation is to define kk dummy variables, XjXj such that Xj = 1Xj=1 if the factor is at level jj and 00 otherwise for j = 1,2,,kj=1,2,,k. However, there is usually a mean included in the model and the sum of the dummy variables will be aliased with the mean. To avoid the extra redundant parameter k1k-1 dummy variables can be defined as the contrasts between one level of the factor, the reference level, and the remaining levels. If the reference level is the first level then the dummy variables can be defined as Xj = 1Xj=1 if the factor is at level jj and 00 otherwise, for j = 2,3,,kj=2,3,,k. Alternatively, the last level can be used as the reference level.
A second way of defining the k1k-1 dummy variables is to use a Helmert matrix in which levels 2,3,,k2,3,,k are compared with the average effect of the previous levels. For example if k = 4k=4 then the contrasts would be:
1 1 1 1
2 1 1 1
3 0 2 1
4 0 0 3
1 -1 -1 -1 2 1 -1 -1 3 0 2 -1 4 0 0 3
Thus variable jj, for j = 1,2,,k1j=1,2,,k-1 is given by where rjrj is the number of replicates of level jj
If the factor can be considered as a set of values from an underlying continuous variable then the factor can be represented by a set of k1k-1 orthogonal polynomials representing the linear, quadratic etc. effects of the underlying variable. The orthogonal polynomial is computed using Forsythe's algorithm (Forsythe (1957), see also Cooper (1968)). The values of the underlying continuous variable represented by the factor levels have to be supplied to the function.
The orthogonal polynomials are standardized so that the sum of squares for each dummy variable is one. For the other methods integer ( ± 1±1) representations are retained except that in the Helmert representation the code of level j + 1j+1 in dummy variable jj will be a fraction.

References

Cooper B E (1968) Algorithm AS 10. The use of orthogonal polynomials Appl. Statist. 17 283–287
Forsythe G E (1957) Generation and use of orthogonal polynomials for data fitting with a digital computer J. Soc. Indust. Appl. Math. 5 74–88

Parameters

Compulsory Input Parameters

1:     typ – string (length ≥ 1)
The type of dummy variable to be computed.
  • If typ = 'P'typ='P', an orthogonal Polynomial representation is computed.
  • If typ = 'H'typ='H', a Helmert matrix representation is computed.
  • If typ = 'F'typ='F', the contrasts relative to the First level are computed.
  • If typ = 'L'typ='L', the contrasts relative to the Last level are computed.
  • If typ = 'C'typ='C', a Complete set of dummy variables is computed.
Constraint: typ = 'P'typ='P', 'H''H', 'F''F', 'L''L' or 'C''C'.
2:     levels – int64int32nag_int scalar
kk, the number of levels of the factor.
Constraint: levels2levels2.
3:     ifact(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint nlevelsnlevels.
The nn values of the factor.
Constraint: 1ifact(i)levels1ifactilevels, for i = 1,2,,ni=1,2,,n.
4:     v( : :) – double array
Note: the dimension of the array v must be at least levelslevels if typ = 'P'typ='P', and at least 11 otherwise.
If typ = 'P'typ='P', the kk distinct values of the underlying variable for which the orthogonal polynomial is to be computed.
If typ'P'typ'P', v is not referenced.
Constraint: if typ = 'P'typ='P', the kk values of v must be distinct.

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array ifact.
nn, the number of observations for which the dummy variables are to be computed.
Constraint: nlevelsnlevels.

Input Parameters Omitted from the MATLAB Interface

ldx

Output Parameters

1:     x(ldx, : :) – double array
The first dimension of the array x will be nn
The second dimension of the array will be levels1levels-1 if typ = 'P'typ='P', 'H''H', 'F''F' or 'L''L' and at least levelslevels if typ = 'C'typ='C'
ldxnldxn.
The nn by k*k* matrix of dummy variables, where k* = k1k*=k-1 if typ = 'P'typ='P', 'H''H', 'F''F' or 'L''L' and k* = kk*=k if typ = 'C'typ='C'.
2:     rep(levels) – double array
The number of replications for each level of the factor, riri, for i = 1,2,,ki=1,2,,k.
3:     ifail – int64int32nag_int scalar
ifail = 0ifail=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.

  ifail = 1ifail=1
On entry,levels < 2levels<2,
orn < levelsn<levels,
orldx < nldx<n,
ortyp'P'typ'P', 'H''H', 'F''F', 'L''L' or 'C''C'.
  ifail = 2ifail=2
On entry,a value of ifact is not in the range 1ifact(i)levels1ifactilevels, for i = 1,2,,ni=1,2,,n,
ortyp = 'P'typ='P' and not all values of v are distinct,
ornot all levels are represented in ifact.
W ifail = 3ifail=3
An orthogonal polynomial has all values zero. This will be due to some values of v being very close together. Note this can only occur if typ = 'P'typ='P'.

Accuracy

The computations are stable.

Further Comments

Other functions for fitting polynomials can be found in Chapter E02.

Example

function nag_anova_dummyvars_example
typ = 'C';
levels = int64(4);
ifact = [int64(1);4;2;3;4;2;4;1;3;1;3;2];
v = [0];
[x, rep, ifail] = nag_anova_dummyvars(typ, levels, ifact, v)
 

x =

     1     0     0     0
     0     0     0     1
     0     1     0     0
     0     0     1     0
     0     0     0     1
     0     1     0     0
     0     0     0     1
     1     0     0     0
     0     0     1     0
     1     0     0     0
     0     0     1     0
     0     1     0     0


rep =

     3
     3
     3
     3


ifail =

                    0


function g04ea_example
typ = 'C';
levels = int64(4);
ifact = [int64(1);4;2;3;4;2;4;1;3;1;3;2];
v = [0];
[x, rep, ifail] = g04ea(typ, levels, ifact, v)
 

x =

     1     0     0     0
     0     0     0     1
     0     1     0     0
     0     0     1     0
     0     0     0     1
     0     1     0     0
     0     0     0     1
     1     0     0     0
     0     0     1     0
     1     0     0     0
     0     0     1     0
     0     1     0     0


rep =

     3
     3
     3
     3


ifail =

                    0



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–2013