sparse.model.matrix          package:Matrix          R Documentation

_C_o_n_s_t_r_u_c_t _S_p_a_r_s_e _D_e_s_i_g_n / _M_o_d_e_l _M_a_t_r_i_c_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     Construct a Model or "Design" Matrix

_U_s_a_g_e:

     sparse.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'.  If another sort of
          object, 'model.frame' is called first.

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.

_V_a_l_u_e:

     a sparse matrix, inheriting from 'CsparseMatrix', typically of
     class  'dgCMatrix'.

_A_u_t_h_o_r(_s):

     Doug Bates and Martin Maechler, with initial suggestions from Tim
     Hesterberg.

_S_e_e _A_l_s_o:

     'model.matrix', ...

_E_x_a_m_p_l_e_s:

     dd <- data.frame(a = gl(3,4), b = gl(4,1,12))# balanced 2-way
     options("contrasts") # the default:  "contr.treatment"
     sparse.model.matrix(~ a + b, dd)
     sparse.model.matrix(~ -1+ a + b, dd)# no intercept --> even sparse
     sparse.model.matrix(~ a + b, dd, contrasts = list(a="contr.sum"))
     sparse.model.matrix(~ a + b, dd, contrasts = list(b="contr.SAS"))

     ## Sparse method is equivalent to the traditional one :
     stopifnot(all(sparse.model.matrix(~ a + b, dd) ==
                   Matrix(model.matrix(~ a + b, dd), sparse=TRUE)),
               all(sparse.model.matrix(~ 0+ a + b, dd) ==
                   Matrix(model.matrix(~ 0+ a + b, dd), sparse=TRUE)))

