contrast                package:stats                R Documentation

_C_o_n_t_r_a_s_t _M_a_t_r_i_c_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     Return a matrix of contrasts.

_U_s_a_g_e:

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

_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.

  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' then a
     square indicator matrix is returned.

     '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
     constant.

_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

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

