contrasts               package:stats               R Documentation

_G_e_t _a_n_d _S_e_t _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:

     Set and view the contrasts associated with a factor.

_U_s_a_g_e:

     contrasts(x, contrasts = TRUE)
     contrasts(x, how.many) <- value

_A_r_g_u_m_e_n_t_s:

       x: a factor or a logical variable.

contrasts: logical.  See 'Details'.

how.many: How many contrasts should be made. Defaults to one less than
          the number of levels of 'x'.  This need not be the same as
          the number of columns of 'value'.

   value: either a numeric matrix whose columns give coefficients for
          contrasts in the levels of 'x', or the (quoted) name of a
          function which computes such matrices.

_D_e_t_a_i_l_s:

     If contrasts are not set for a factor the default functions from
     'options("contrasts")' are used.

     A logical vector 'x' is converted into a two-level factor with
     levels 'c(FALSE, TRUE)' (regardless of which levels occur in the
     variable).

     The argument 'contrasts' is ignored if 'x' has a matrix
     'contrasts' attribute set.  Otherwise if 'contrasts = TRUE' it is
     passed to a contrasts function such as 'contr.treatment' and if
     'contrasts = FALSE' an identity matrix is returned.  Suitable
     functions have a first argument which is the character vector of
     levels and a named argument 'contrasts' (always called with
     'contrasts = TRUE').

     If 'value' supplies more than 'how.many' contrasts, the first
     'how.many' are used.  If too few are supplied, a suitable contrast
     matrix is created by extending 'value' after ensuring its columns
     are contrasts (orthogonal to the constant term) and not collinear.

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

     'C', 'contr.helmert', 'contr.poly', 'contr.sum',
     'contr.treatment'; 'glm', 'aov', 'lm'.

_E_x_a_m_p_l_e_s:

     utils::example(factor)
     fff <- ff[, drop=TRUE]  # reduce to 5 levels.
     contrasts(fff) # treatment contrasts by default
     contrasts(C(fff, sum))
     contrasts(fff, contrasts = FALSE) # the 5x5 identity matrix

     contrasts(fff) <- contr.sum(5); contrasts(fff)  # set sum contrasts
     contrasts(fff, 2) <- contr.sum(5); contrasts(fff)  # set 2 contrasts
     # supply 2 contrasts, compute 2 more to make full set of 4.
     contrasts(fff) <- contr.sum(5)[,1:2]; contrasts(fff)

