\Header{step}{Choose a model by AIC in a Stepwise Algorithm}
\keyword{models}{step}
\begin{Usage}
\begin{verbatim}
step(object, scope, scale=0, direction=c("both", "backward", "forward"), 
	trace=1, keep=NULL, steps=1000, k=2, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\texttt{object}] an object representing a model of an appropriate class.
This is used as the initial model in the stepwise search.
\item[\texttt{scope}] defines the range of models examined in the stepwise search.
\item[\texttt{scale}] used in the definition of the AIC statistic for selecting the models,
currently only for \texttt{\Link{lm}}, \texttt{\Link{aov}} and
\texttt{\Link{glm}} models.
\item[\texttt{direction}] the mode of stepwise search, can be one of \texttt{"both"}, \texttt{"backward"}, 
or \texttt{"forward"}, with a default of \texttt{"both"}. 
If the \texttt{scope} argument is missing, 
the default for \texttt{direction} is \texttt{"backward"}.
\item[\texttt{trace}] if positive, information is printed during the running of \texttt{step}.
\item[\texttt{keep}] a filter function whose input is a fitted model object and the 
associated \texttt{AIC} statistic, and whose output is arbitrary. 
Typically \texttt{keep} will select a subset of the components of 
the object and return them. The default is not to keep anything.
\item[\texttt{steps}] the maximum number of steps to be considered.  The default is 1000
(essentially as many as required).  It is typically used to stop the
process early.
\item[\texttt{k}] the multiple of the number of degrees of freedom used for the penalty.
Only \texttt{k=2} gives the genuine AIC: \texttt{k = log(n)} is sometimes
referred to as BIC or SBC.
\item[\texttt{...}] any additional arguments to \texttt{\Link{extractAIC}}.
\end{ldescription}
\end{Arguments}
\begin{Description}
\texttt{step} uses \texttt{\Link{add1}} and \texttt{\Link{drop1}}
repeatedly; it will work for any method for which they work, and that
is determined by having a valid method for \texttt{\Link{extractAIC}}.
When the additive constant can be chosen so that AIC is equal to
Mallows' Cp, this is done and the tables are labelled appropriately.

There is a potential problem in using \texttt{\Link{glm}} fits with a variable
\texttt{scale}, as in that case the deviance is not simply related to the
maximized log-likelihood. The function \texttt{\Link{extractAIC.glm}} makes the
appropriate adjustment for a \texttt{gaussian} family, but may need to be
amended for other cases. (The \texttt{binomial} and \texttt{poisson}
families have fixed \texttt{scale} by default and do not correspond
to a particular maximum-likelihood problem for variable \texttt{scale}.)\end{Description}
\begin{Value}
the stepwise-selected model is returned, with up to two additional
components.  There is an \texttt{"anova"} component corresponding to the
steps taken in the search, as well as a \texttt{"keep"} component if the
\texttt{keep=} argument was supplied in the call. The
\texttt{"Resid. Dev"} column of the analysis of deviance table refers
to a constant minus twice the maximized log likelihood: it will be a
deviance only in cases where a saturated model is well-defined
(thus excluding \texttt{lm}, \texttt{aov} and \texttt{survreg} fits, for example).\end{Value}
\begin{Note}
This function differs considerably from the function in S, which uses a
number of approximations and does not compute the correct AIC.\end{Note}
\begin{Author}
B.D. Ripley\end{Author}
\begin{SeeAlso}
\texttt{\Link{add1}}, \texttt{\Link{drop1}}\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
example(lm)
step(lm.D9)  

data(swiss)
summary(lm1 <- lm(Fertility ~ ., data = swiss))
slm1 <- step(lm1)
summary(slm1)
slm1 $ anova
\end{ExampleCode}
\end{Examples}

