Rev 38402 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{formula}\title{Model Formulae}\usage{formula(x, \dots)as.formula(object, env = parent.frame())}\alias{formula}\alias{formula.default}\alias{formula.formula}\alias{formula.terms}\alias{formula.data.frame}\alias{as.formula}\alias{print.formula}\alias{[.formula}\description{The generic function \code{formula} and its specific methods provide away of extracting formulae which have been included in other objects.\code{as.formula} is almost identical, additionally preservingattributes when \code{object} already inherits from\code{"formula"}. The default value of the \code{env} argument isused only when the formula would otherwise lack an environment.}\arguments{\item{x, object}{\R object.}\item{\dots}{further arguments passed to or from other methods.}\item{env}{the environment to associate with the result.}}\details{The models fit by, e.g., the \code{\link{lm}} and \code{\link{glm}} functionsare specified in a compact symbolic form.The \code{~} operator is basic in the formation of such models.An expression of the form \code{y ~ model} is interpretedas a specification that the response \code{y} is modelledby a linear predictor specified symbolically by \code{model}.Such a model consists of a series of terms separatedby \code{+} operators.The terms themselves consist of variable and factornames separated by \code{:} operators.Such a term is interpreted as the interaction ofall the variables and factors appearing in the term.In addition to \code{+} and \code{:}, a number of other operators areuseful in model formulae. The \code{*} operator denotes factorcrossing: \code{a*b} interpreted as \code{a+b+a:b}. The \code{^}operator indicates crossing to the specified degree. For example\code{(a+b+c)^2} is identical to \code{(a+b+c)*(a+b+c)} which in turnexpands to a formula containing the main effects for \code{a},\code{b} and \code{c} together with their second-order interactions.The \code{\%in\%} operator indicates that the terms on its left arenested within those on the right. For example \code{a + b \%in\% a}expands to the formula \code{a + a:b}. The \code{-} operator removesthe specified terms, so that \code{(a+b+c)^2 - a:b} is identical to\code{a + b + c + b:c + a:c}. It can also used to remove the interceptterm: \code{y ~ x - 1} is a line through the origin. A model with nointercept can be also specified as \code{y ~ x + 0} or \code{y ~ 0 + x}.While formulae usually involve just variable and factornames, they can also involve arithmetic expressions.The formula \code{log(y) ~ a + log(x)} is quite legal.When such arithmetic expressions involveoperators which are also used symbolicallyin model formulae, there can be confusion betweenarithmetic and symbolic operator use.To avoid this confusion, the function \code{\link{I}()}can be used to bracket those portions of a modelformula where the operators are used in theirarithmetic sense. For example, in the formula\code{y ~ a + I(b+c)}, the term \code{b+c} is to beinterpreted as the sum of \code{b} and \code{c}.Variable names can be quoted by backticks \code{`like this`} informulae, although there is no guarantee that all code using formulaewill accept such non-syntactic names.When \code{formula} is called on a fitted model object, either aspecific method is used (such as that for class \code{"nls"}) of thedefault method. The default first looks for a \code{"formula"}component of the object (and evaluates it), then a \code{"terms"}component, then a \code{formula} parameter of the call (and evaluatesits value) and finally a \code{"formula"} attribute.}\section{Environments}{A formula object has an associated environment, andthis environment (rather than the parentenvironment) is used by \code{\link{model.frame}} to evaluate variablesthat are not found in the supplied \code{data} argument.Formulas created with the \code{~} operator use theenvironment in which they were created. Formulas created with\code{as.formula} will use the \code{env} argument for theirenvironment. Pre-existing formulas extracted with\code{as.formula} will only have their environment changed if\code{env} is given explicitly.}\value{All the functions above produce an object of class \code{"formula"}which contains a symbolic model formula.}\references{Chambers, J. M. and Hastie, T. J. (1992)\emph{Statistical models.}Chapter 2 of \emph{Statistical Models in S}eds J. M. Chambers and T. J. Hastie, Wadsworth \& Brooks/Cole.}\seealso{\code{\link{I}}.For formula manipulation: \code{\link{terms}}, and \code{\link{all.vars}};for typical use: \code{\link{lm}}, \code{\link{glm}}, and\code{\link{coplot}}.}\examples{class(fo <- y ~ x1*x2) # "formula"fotypeof(fo)# R internal : "language"terms(fo)environment(fo)environment(as.formula("y ~ x"))environment(as.formula("y ~ x",env=new.env()))## Create a formula for a model with a large number of variables:xnam <- paste("x", 1:25, sep="")(fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+"))))}\keyword{models}