The R Project SVN R

Rev

Rev 70681 | Rev 83727 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 70681 Rev 76152
Line 1... Line 1...
1
% File src/library/stats/man/model.matrix.Rd
1
% File src/library/stats/man/model.matrix.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2016 R Core Team
3
% Copyright 1995-2019 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{model.matrix}
6
\name{model.matrix}
7
\alias{model.matrix}
7
\alias{model.matrix}
8
\alias{model.matrix.default}
8
\alias{model.matrix.default}
Line 17... Line 17...
17
\arguments{
17
\arguments{
18
  \item{object}{an object of an appropriate class.  For the default
18
  \item{object}{an object of an appropriate class.  For the default
19
    method, a model \link{formula} or a \code{\link{terms}} object.}
19
    method, a model \link{formula} or a \code{\link{terms}} object.}
20
  \item{data}{a data frame created with \code{\link{model.frame}}.  If
20
  \item{data}{a data frame created with \code{\link{model.frame}}.  If
21
    another sort of object, \code{model.frame} is called first.}
21
    another sort of object, \code{model.frame} is called first.}
22
  \item{contrasts.arg}{A list, whose entries are values (numeric
22
  \item{contrasts.arg}{a list, whose entries are values (numeric
23
    matrices or character strings naming functions) to be used
23
    matrices, \code{\link{function}}s or character strings naming
-
 
24
    functions) to be used
24
    as replacement values for the \code{\link{contrasts}}
25
    as replacement values for the \code{\link{contrasts}}
25
    replacement function and whose names are the names of
26
    replacement function and whose names are the names of
26
    columns of \code{data} containing \code{\link{factor}}s.}
27
    columns of \code{data} containing \code{\link{factor}}s.}
27
  \item{xlev}{to be used as argument of \code{\link{model.frame}} if
28
  \item{xlev}{to be used as argument of \code{\link{model.frame}} if
28
    \code{data} is such that \code{model.frame} is called.}
29
    \code{data} is such that \code{model.frame} is called.}
Line 45... Line 46...
45
  formula must be logical, integer, numeric or factor.
46
  formula must be logical, integer, numeric or factor.
46
 
47
 
47
  If \code{contrasts.arg} is specified for a factor it overrides the
48
  If \code{contrasts.arg} is specified for a factor it overrides the
48
  default factor coding for that variable and any \code{"contrasts"}
49
  default factor coding for that variable and any \code{"contrasts"}
49
  attribute set by \code{\link{C}} or \code{\link{contrasts}}.
50
  attribute set by \code{\link{C}} or \code{\link{contrasts}}.
-
 
51
  Whereas invalid \code{contrasts.arg}s have been ignored always, they are
-
 
52
  warned about since \R version 3.6.0.
50
 
53
 
51
  In an interaction term, the variable whose levels vary fastest is the
54
  In an interaction term, the variable whose levels vary fastest is the
52
  first one to appear in the formula (and not in the term), so in
55
  first one to appear in the formula (and not in the term), so in
53
  \code{~ a + b + b:a} the interaction will have \code{a} varying
56
  \code{~ a + b + b:a} the interaction will have \code{a} varying
54
  fastest.
57
  fastest.
Line 92... Line 95...
92
ff <- log(Volume) ~ log(Height) + log(Girth)
95
ff <- log(Volume) ~ log(Height) + log(Girth)
93
utils::str(m <- model.frame(ff, trees))
96
utils::str(m <- model.frame(ff, trees))
94
mat <- model.matrix(ff, m)
97
mat <- model.matrix(ff, m)
95
 
98
 
96
dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way
99
dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way
97
options("contrasts")
100
options("contrasts") # typically 'treatment' (for unordered factors)
98
model.matrix(~ a + b, dd)
101
model.matrix(~ a + b, dd)
99
model.matrix(~ a + b, dd, contrasts = list(a = "contr.sum"))
102
model.matrix(~ a + b, dd, contrasts = list(a = "contr.sum"))
100
model.matrix(~ a + b, dd, contrasts = list(a = "contr.sum", b = "contr.poly"))
103
model.matrix(~ a + b, dd, contrasts = list(a = "contr.sum", b = contr.poly))
101
m.orth <- model.matrix(~a+b, dd, contrasts = list(a = "contr.helmert"))
104
m.orth <- model.matrix(~a+b, dd, contrasts = list(a = "contr.helmert"))
102
crossprod(m.orth) # m.orth is  ALMOST  orthogonal
105
crossprod(m.orth) # m.orth is  ALMOST  orthogonal
-
 
106
# invalid contrasts.. ignored with a warning:
-
 
107
stopifnot(identical(
-
 
108
   model.matrix(~ a + b, dd),
-
 
109
   model.matrix(~ a + b, dd, contrasts.arg = "contr.FOO")))
103
}
110
}
104
\keyword{models}
111
\keyword{models}