contrast               package:Matrix               R Documentation

_C_o_n_t_r_a_s_t _M_a_t_r_i_c_e_s, _O_p_t_i_o_n_a_l_l_y _S_p_a_r_s_e

_D_e_s_c_r_i_p_t_i_o_n:

     Return a matrix of contrasts.

     This help file for package 'Matrix' is only needed for R versions
     < 2.10.0.

_U_s_a_g_e:

     contr.helmert(n, contrasts = TRUE, sparse = FALSE)
     contr.poly(n, scores = 1:n, contrasts = TRUE, sparse = FALSE)
     contr.sum(n, contrasts = TRUE, sparse = FALSE)
     contr.treatment(n, base = 1, contrasts = TRUE, sparse = FALSE)
     contr.SAS(n, contrasts = TRUE, sparse = FALSE)

_A_r_g_u_m_e_n_t_s:

       n: a vector of levels for a factor, or the number of levels.

contrasts: a logical indicating whether contrasts should be computed.

  sparse: logical indicating if the result should be sparse, using
          'Matrix' from package 'Matrix'.

  scores: the set of values over which orthogonal polynomials are to be
          computed.

    base: an integer specifying which group is considered the baseline
          group. Ignored if 'contrasts' is 'FALSE'.

_D_e_t_a_i_l_s:

     These functions are used for creating contrast matrices for use in
     fitting analysis of variance and regression models.  The columns
     of the resulting matrices contain contrasts which can be used for
     coding a factor with 'n' levels.  The returned value contains the
     computed contrasts.  If the argument 'contrasts' is 'FALSE' a
     square indicator matrix (the dummy coding) is returned *except*
     for 'contr.poly' (which include the 0-degree, i.e. constant,
     polynomial when 'contrasts = FALSE').

     'cont.helmert' returns Helmert contrasts, which contrast the
     second level with the first, the third with the average of the
     first two, and so on.  'contr.poly' returns contrasts based on
     orthogonal polynomials. 'contr.sum' uses 'sum to zero contrasts'.

     'contr.treatment' contrasts each level with the baseline level
     (specified by 'base'): the baseline level is omitted.  Note that
     this does not produce 'contrasts' as defined in the standard
     theory for linear models as they are not orthogonal to the
     intercept.

     'contr.SAS' is a wrapper for 'contr.treatment' that sets the base
     level to be the last level of the factor.  The coefficients
     produced when using these contrasts should be equivalent to those
     produced by many (but not all) SAS procedures.

     For consistency, 'sparse' is an argument to all contrast
     functions, however 'sparse = TRUE' for 'contr.poly' is nonsense
     (and hence an error), and also is rarely sensical for
     'contr.helmert'.

_V_a_l_u_e:

     A matrix with 'n' rows and 'k' columns, with 'k=n-1' if
     'contrasts' is 'TRUE' and 'k=n' if 'contrasts' is 'FALSE'.

_R_e_f_e_r_e_n_c_e_s:

     Chambers, J. M. and Hastie, T. J. (1992) _Statistical models._
     Chapter 2 of _Statistical Models in S_ eds J. M. Chambers and T.
     J. Hastie, Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'contrasts', 'C', and 'aov', 'glm', 'lm'.

_E_x_a_m_p_l_e_s:

     (cH <- contr.helmert(4))
     apply(cH, 2,sum) # column sums are 0!
     crossprod(cH) # diagonal -- columns are orthogonal
     contr.helmert(4, contrasts = FALSE) # just the 4 x 4 identity matrix

     (cT <- contr.treatment(5))
     all(crossprod(cT) == diag(4)) # TRUE: even orthonormal

     cT. <- contr.SAS(5)
     all(crossprod(cT.) == diag(4))# TRUE
     (cS. <- contr.SAS(5, sparse = TRUE))
     all(cT. == cS.)

     (cP <- contr.poly(3)) # Linear and Quadratic
     zapsmall(crossprod(cP), digits=15) # orthonormal up to fuzz

