\Header{formula}{Model Formulae}
\alias{as.formula}{formula}
\methalias{formula.data.frame}{formula}
\methalias{formula.default}{formula}
\methalias{formula.formula}{formula}
\methalias{formula.terms}{formula}
\alias{I}{formula}
\alias{print.formula}{formula}
\alias{$\,\tilde{\,}$}{formula}
\keyword{models}{formula}
\begin{Description}\relax
The generic function \code{formula} and its specific methods provide a
way of extracting formulae which have been included in other objects.

\code{as.formula} is almost identical, additionally preserving
attributes when \code{object} already inherits from \code{"formula"}.\end{Description}
\begin{Usage}
\begin{verbatim}
y ~ model
formula(object)
formula.default(anything)
formula.formula(formula.obj)
formula.terms(terms.obj)
formula.data.frame(df)
as.formula(object)
I(name)
\end{verbatim}
\end{Usage}
\begin{Details}\relax
The models fit by, e.g., the \code{\Link{lm}} and \code{\Link{glm}} functions
are specified in a compact symbolic form.
The \code{$\,\tilde{\,}$} operator is basic in the formation of such models.
An expression of the form \code{y $\,\tilde{\,}$ model} is interpreted
as a specification that the response \code{y} is modelled
by a linear predictor specified symbolically by \code{model}.
Such a model consists of a series of terms separated
by \code{+} operators.
The terms themselves consist of variable and factor
names separated by \code{:} operators.
Such a term is interpreted as the interaction of
all the variables and factors appearing in the term.

In addition to \code{+} and \code{:}, a number of other operators are
useful in model formulae.  The \code{*} operator denotes factor
crossing: \code{a*b} interpreted as \code{a+b+a:b}.  The \code{$\,\hat{\,}$}
operator indicates crossing to the specified degree.  For example
\code{(a+b+c)$\,\hat{\,}$2} is identical to \code{(a+b+c)*(a+b+c)} which in turn
expands 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 are
nested within those on the right.  For example \code{a+b\%in\%a}
expands to the formula \code{a+a:b}.  The \code{-} operator removes
the specified terms, so that \code{(a+b+c)$\,\hat{\,}$2 - a:b} is identical to
\code{a + b + c + b:c + a:c}. It can also used to remove the intercept
term: \code{y$\,\tilde{\,}$x - 1} is a line through the origin. A model with no
intercept can be also specified as \code{y$\,\tilde{\,}$x + 0} or \code{0 + y$\,\tilde{\,}$x}.

While formulae usually involve just variable and factor
names, they can also involve arithmetic expressions.
The formula \code{log(y) $\,\tilde{\,}$ a + log(x)} is quite legal.
When such arithmetic expressions involve
operators which are also used symbolically
in model formulae, there can be confusion between
arithmetic and symbolic operator use.

To avoid this confusion, the function \code{I()}
can be used to bracket those portions of a model
formula where the operators are used in their
arithmetic sense.  For example, in the formula
\code{y $\,\tilde{\,}$ a + I(b+c)}, the term \code{b+c} is to be
interpreted as the sum of \code{b} and \code{c}.\end{Details}
\begin{Value}
All the functions above produce an object
of class \code{formula} which contains a symbolic model formula.\end{Value}
\begin{SeeAlso}\relax
\code{\Link{lm}}, \code{\Link{glm}}, \code{\Link{terms}}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
class(fo <- y ~ x1*x2) # "formula"
fo
typeof(fo)# R internal : "language"
terms(fo)

## 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= "+"))))
\end{ExampleCode}
\end{Examples}

