Extract a formula from a model in R

I am creating a function for many types of models that need to extract the formula used to create 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 need to do to extract the equation equation when the model is passed.

+8
r modeling
source share
2 answers

You can get what you want:

  model$call # lm(formula = formula) 

And if you want to see what I found out, use:

 str(model) 

Since you passed the "formula" (an unsuccessful choice of names by the way) from the calling environment, which then you need to extract from the object you passed:

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

@JPMac offers a more compact method: formula(model) . It's also worth looking at the mechanism used by the formula.lm function. A function called formula is generic, and you use methods(formula) to find out which S3 methods are defined. Since the formula.lm method has an asterisk at the 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> 

Thus, it uses "$" to retrieve a list item named "formula" instead of pulling it out of the call. If the $ formula element is missing (which is your case), then it replaces it with formula(x$terms) , which, I suspect, calls formula.default , and looking at how this function works, only the environment correction appears object.

+12
source share

As already noted, model$call will give you the call that created the lm object, but if this call contains the object itself as a model formula, you will get the name of the object, not the formula.

The evaluated object, i.e. the formula itself can be obtained in model$terms (along with a bunch of supporting information about how it was processed). This should work regardless of the details of the lm call.

+3
source share

All Articles