formula
Define or Extract a Model Formula

Description

Defines or extracts a formula object.
This function is an S Version 3 generic (see Methods). A generic function adapts its action to match the class of an object by calling a method function specified for that class. You can create a method function to handle specific S Version 3 classes of data. Method functions for the classes data.frame, formula, lm, nls, and terms are included.

Usage

formula(x, ...) 
as.formula(object, env = parent.frame())
## Default S3 method:
formula(x, env = parent.frame(), ...)
## S3 method for class 'call':
formula(x, env = parent.frame(), ...)
## S3 method for class 'character':
formula(x, env = parent.frame(), ...)
## S3 method for class 'data.frame':
formula(x, ..., env = parent.frame())
## S3 method for class 'formula':
formula(x, ...)
## S3 method for class 'lm':
formula(x, ...)
## S3 method for class 'nls':
formula(x, ...)
## S3 method for class 'terms':
formula(x, ...)

Arguments

x either a formula expression--that is, a call to the tilde (~) operator or a character string that could be parsed to make such a call--or an object that contains a previously constructed formula, such as a fitted model or a terms object. As a special case, formula(data.frame will construct a formula in which the response is the first column of the data.frame and the other columns are additive predictors.
object For historical reasons as.formula calls its first argument object but it is treated the same as x in formula.
env The environment to assign to the formula. The modelling functions will look up names from the formula in this environment. (Names not found in that environment, or its ancestral environments, will be looked up in the environment of the caller of the modelling function.)
Value
formula returns an object of class "formula", essentially the call to the tilde (~) operator along with an attribute, ".Environment", that gives the environment in which the formula was constructed.
The return value for as.formula differs in that if its object argument inherits from class formula it returns it, as-is. Otherwise it returns formula(object).
See Also
For a description of formula syntax, see formula.object.
Examples
Fuel ~ Weight + Disp.
formula(lm(Fuel ~ Weight + Type * Disp., data=Sdatasets::fuel.frame))
formula(Sdatasets::fuel.frame)
Package stats version 4.0.0-28
Package Index