From Thomas Lumley <thomas@biostat.washington.edu>  Sat Apr 18 17:51:16 1998
From: Thomas Lumley <thomas@biostat.washington.edu> (Thomas Lumley)
Date: Sat, 18 Apr 1998 09:51:16 -0700 (PDT)
Subject: attr(modelmatrix,"assign")
In-Reply-To: <9804178928.AA892838755@nass.usda.gov>
Message-ID: <Pine.SUN.3.95.980417121655.11386D-100000@chekov>


The "assign" attribute for model matrices has mutated in S-PLUS (probably
in 3.3 or 3.4) -- the model code is obviously not subject to
enough evolutionary pressure to keep mutations down.

Now it returns a list with elements named for each model term, giving the
vector of column numbers arising from that term
> y<-1:10
> x<-1:10
> attr(model.matrix(~y+factor(x)),"assign") 
$"(Intercept)":
[1] 1

$y:
[1] 2

$"factor(x)":
[1]  3  4  5  6  7  8  9 10 11

I don't think we necessarily want to change from our attr(,"assign"),
which follows Chambers & Hastie, but we may want to be able to create a
compatible version.
This can be done by the following function, which needs the terms
object as well as the model matrix

attrassign<-function(mmat,tt){
        if (!inherits(tt,"terms"))
                stop("need terms object")
        aa<-attr(mmat,"assign")
        if (is.null(aa))
                stop("argument is not really a model matrix")
        ll<-attr(tt,"term.labels")
        if (attr(tt,"intercept")>0)
                ll<-c("(Intercept)",ll)
        aaa<-factor(aa,labels=ll)
        split(order(aa),aaa)
        }

Perhaps we should add another attribute ("S.assign") to model matrices?

Thomas Lumley
------------------------------------------------------+------
Biostatistics		: "Never attribute to malice what  :
Uni of Washington	:  can be adequately explained by  :
Box 357232		:  incompetence" - Hanlon's Razor  :
Seattle WA 98195-7232	:				   :
------------------------------------------------------------




-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


attr(modelmatrix,"assign")
Thomas Lumley