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, ...)
DF2formula(x, env = parent.frame())

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) constructs a formula in which the response is the first column of the data.frame and the other columns are additive predictors (unless the data.frame contains a "terms" attribute, in which case the returned formula comes from that attribute).
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 look up names from the formula in this environment. (Names not found in that environment, or its ancestral environments, are looked up in the environment of the caller of the modelling function.)
Value

formulareturns 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.
as.formulaif the object argument inherits from class formula, then as.formula returns the formula as-is. Otherwise, as.formula returns formula(object).
DF2formula(x)returns a formula formed from the column names of the data.frame x in the same order as that of x, regardless of whether x has a "terms" attribute.
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))
DF2formula(Sdatasets::fuel.frame)
Package stats version 6.0.0-69
Package Index