Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract formula from model in R

Tags:

r

modeling

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.

like image 806
mike Avatar asked Mar 14 '12 00:03

mike


2 Answers

As noted, model$call will get you the call that created the lm object, but if that call contains an object itself as the model formula, you get the object name, not the formula.

The evaluated object, ie the formula itself, can be accessed in model$terms (along with a bunch of auxiliary information on how it was treated). This should work regardless of the details of the call to lm.

like image 90
Hong Ooi Avatar answered Sep 22 '22 19:09

Hong Ooi


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.

like image 37
IRTFM Avatar answered Sep 23 '22 19:09

IRTFM