Extract formula from model in R

mike picture mike · Mar 14, 2012 · Viewed 13.4k times · Source

I'm building a function for many model types which needs to extract the formula used to make the model. Is there a flexible way to do this? For example:

x <- rnorm(10)
y <- rnorm(10)
z <- rnorm(10)
equation <- z ~ x + y
model <- lm(equation)

I what I need to do is extract the formula object "equation" once being passed the model.

Answer

IRTFM picture IRTFM · Mar 14, 2012

You could get what you wanted by:

  model$call
# lm(formula = formula)

And if you want to see what I did find out then use:

str(model)

Since you passed 'formula' (bad choice of names by the way) from the calling environment you might then need to extract from the object you passed:

 eval(model$call[[2]])
# z ~ x + y

@JPMac offered a more compact method: formula(model). It's also worth looking at the mechanism used by the formula.lm function. The function named formula is generic and you use methods(formula) to see what S3 methods have been defined. Since the formula.lm method has an asterisk at its end, you need to wrap it in `getAnywhere:

> getAnywhere(formula.lm)
A single object matching ‘formula.lm’ was found
It was found in the following places
  registered S3 method for formula from namespace stats
  namespace:stats
with value

function (x, ...) 
{
    form <- x$formula
    if (!is.null(form)) {
        form <- formula(x$terms)
        environment(form) <- environment(x$formula)
        form
    }
    else formula(x$terms)
}
<bytecode: 0x36ff26158>
<environment: namespace:stats>

So it is using "$" to extract the list item named "formula" rather than pulling it from the call. If the $formula item is missing (which it is in your case) then It then replaces that with formula(x$terms) which I suspect is calling formula.default and looking at the operation of that function appears to only be adjusting the environment of the object.