model.matrix              package:stats              R Documentation

_C_o_n_s_t_r_u_c_t _D_e_s_i_g_n _M_a_t_r_i_c_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     'model.matrix' creates a design matrix.

_U_s_a_g_e:

     model.matrix(object, ...)

     ## Default S3 method:
     model.matrix(object, data = environment(object),
                  contrasts.arg = NULL, xlev = NULL, ...)

_A_r_g_u_m_e_n_t_s:

  object: an object of an appropriate class. For the default method, a
          model formula or terms object.

    data: a data frame created with 'model.frame'.

contrasts.arg: A list, whose entries are contrasts suitable for input
          to the 'contrasts' replacement function and whose names are
          the names of columns of 'data' containing 'factor's.

    xlev: to be used as argument of 'model.frame' if 'data' has no
          '"terms"' attribute.

     ...: further arguments passed to or from other methods.

_D_e_t_a_i_l_s:

     'model.matrix' creates a design matrix from the description given
     in 'terms(formula)', using the data in 'data' which must contain
     columns with the same names as would be created by a call to
     'model.frame(formula)' or, more precisely, by evaluating
     'attr(terms(formula), "variables")'.  There may be other columns
     and the order is not important.  If 'contrasts' is specified it
     overrides the default factor coding for that variable.

     In interactions, the variable whose levels vary fastest is the
     first one to appear in the formula (and not in the term), so in '~
     a + b + b:a' the interaction will have 'a' varying fastest.

     By convention, if the response variable also appears on the
     right-hand side of the formula it is dropped (with a warning),
     although interactions involving the term are retained.

_V_a_l_u_e:

     The design matrix for a regression model with the specified
     formula and data.

_R_e_f_e_r_e_n_c_e_s:

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

_S_e_e _A_l_s_o:

     'model.frame', 'model.extract', 'terms'

_E_x_a_m_p_l_e_s:

     data(trees)
     ff <- log(Volume) ~ log(Height) + log(Girth)
     str(m <- model.frame(ff, trees))
     mat <- model.matrix(ff, m)

     dd <- data.frame(a = gl(3,4), b = gl(4,1,12))# balanced 2-way
     options("contrasts")
     model.matrix(~ a + b, dd)
     model.matrix(~ a + b, dd, contrasts = list(a="contr.sum"))
     model.matrix(~ a + b, dd, contrasts = list(a="contr.sum", b="contr.poly"))
     m.orth <- model.matrix(~a+b, dd, contrasts = list(a="contr.helmert"))
     crossprod(m.orth)# m.orth is  ALMOST  orthogonal

