[R] model.matrix()
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Mon Aug 30 11:00:13 CEST 1999
On Mon, 30 Aug 1999, Thomas Yee wrote:
> Hi,
>
> In R I get the following:
>
> > x <- 1:9
> > f <- as.factor(1:9)
> > i <- model.matrix(~x+f)
> >
> > attr(i, "assign")
> [1] 0 1 2 2 2 2 2 2 2 2
>
> whereas in S I get the following:
>
> > attr(i, "assign")
> $"(Intercept)":
> [1] 1
>
> $x:
> [1] 2
>
> $f:
> [1] 3 4 5 6 7 8 9 10
>
> Has anybody written a model.matrix function to get the same
> result as S? It's just that S's output is what I want, viz.,
>
> 1. the names "(Intercept)","x","f", and
>
> 2. the vector values.
Look in the function summary.aov, then. You need to be very careful with
singular matrices, as R and S differ even more there.
Something like
my.model.matrix <- function(formula, data = sys.frame(sys.parent()), ...)
{
mf <- model.frame(formula, data)
terms <- attr(mf, "terms")
tl <- attr(terms, "term.labels")
intr <- attr(terms, "intercept")
if(intr > 0) tl <- c("(Intercept)", tl)
m <- model.matrix(formula, mf, ...)
asgn <- attr(m, "assign")
s <- split(seq(along=asgn), asgn)
names(s) <- tl
attr(m, "my.assign") <- s
m
}
may do want you want. Beware of what happens when you fit a singular
model, though,
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list