[R] how to use attr?

Thomas Lumley tlumley at u.washington.edu
Tue Jan 8 21:49:50 CET 2002


On Tue, 8 Jan 2002, Jeff D. Hamann wrote:

> I'm trying to build a flexible OLS/SUR/2SLS/3SLS package and I'm having
> trouble getting some information out of a formula. For example....
>
> # set up the system of equations
> demand    <- q ~ p + d
> supply    <- q ~ p + f + a
> inst    <- ~ d + f + a
> systemeq   <- list( demand, supply )
>
> ...blah, blah, blah...
>
> # get the number if instruments...
> # or build the "bigX"...
> #I need to get a print of the variables to build a large matrix...
> print( attr( systemeq[[1]], "variables" ) )
>
> I've tried all kinds of variants...
> names( systemeq[[1]] )
> names( systemeq[[1]]$formula )
>

The complicated answer is that these things are
attributes of a terms object, not a formula. You would need
attr(terms(supply),"variables") and attr(terms(inst),"variables").


The simpler answer is that you want to use model.frame() (and probably
model.matrix() as well.)

model.frame() constructs a dataframe with all the required variables for a
formula, model.matrix takes this dataframe and produces a design matrix.

	-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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list