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. Thanks Thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thomas Yee <yee at wintermute.anu.edu.au> writes:> 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.Hmm. Do we want to change that? For now, something like a <-attr (i, "assign") tt<-terms(~x+f) a <- factor(a,labels=c( if(attr(tt,"intercept")) "(Intercept)", attr(tt,"term.labels"))) split(seq(along=a),a) would seem to do. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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., >As well as the previous suggestions, you will find in library(survival5) a function attrassign() that converts an 'assign' attribute to the new style. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._