[R] getting the response (dependent) from formula?

Torsten Hothorn Torsten.Hothorn at rzmail.uni-erlangen.de
Wed Jan 16 12:13:59 CET 2002


> I'm trying to build a vector (fitting systems of equations) and I'm having a
> little trouble getting the response variables from a list of formulas...
> 
> bdgmodel   <- lbdg ~ d1sqr + ld1 + lhg
> hgmodel    <- lhg ~ ht2 + lht + whc
> inst       <- ~ d1sqr + ld1 + ht2 + lht + whc
> systemeq  <- list( bdgmodel, hgmodel )

have a look at (e.g.) wilcox.test.formula:

wilcox.test.formula <-
function(formula, data, subset, na.action, ...)
{
    ...

    m <- match.call(expand.dots = FALSE)
    if(is.matrix(eval(m$data, parent.frame())))
        m$data <- as.data.frame(data)
    m[[1]] <- as.name("model.frame")
    m$... <- NULL
    mf <- eval(m, parent.frame())
   
    ...

    response <- attr(attr(mf, "terms"), "response")

    # And now you can get the response by mf[[-response]]

    myresp <- mf[[-response]]

    ...
}

Torsten

> 
> # manually generate the y matrix...
> y <- cbind( matrix( lbdg ),
>             matrix( log(hg) ) )
> 
> I would like to replace the previous line with something like...
> 
> # do the three stage least squares here...
> for(i in 1:length( systemeq ) )
> {
>     bigy <- rbind( bigy, attr(terms(systemeq[[i]]),"variables")[2] )
> }
> 
> ... then go on to fit the system of equations, etc, etc, etc...
> 
> attr(terms(systemeq[[i]]),"variables")[2]
> 
> this statement returns
> "lbdg()" - without the quotes, which I can't figure out how to use....
> 
> is there an equivalent to model.matrix( systemeq[[1]] )?
> 
> Thanks,
> Jeff.
> 
> 
> 
> 
> Jeff D. Hamann
> Hamann, Donald & Associates, Inc.
> PO Box 1421
> Corvallis, Oregon USA 97339-1421
> Bus. 541-753-7333
> Cell. 541-740-5988
> jeff_hamann at hamanndonald.com
> www.hamanndonald.com
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

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