Build a Model in a Stepwise Fashion - Generic Function

DESCRIPTION:

Performs stepwise model selection.
This function is an S Version 3 generic (see Methods); method functions can be written to handle specific S Version 3 classes of data. Classes that already have methods for this function include gam and glm.

USAGE:

step(object, scope, scale, direction, trace=T, keep, steps)

REQUIRED ARGUMENTS:

object
an object representing a model of an appropriate class. This is used as the initial model in the stepwise search.

OPTIONAL ARGUMENTS:

scope
defines the range of models examined in the stepwise search.
scale
used in the definition of the AIC statistic for selecting the models.
direction
the mode of stepwise search, can be one of "both", "backward", or "forward", with a default of "both". If the scope argument is missing, the default for direction is "backward".
trace
if TRUE, information is printed during the running of step. This is usually a good choice, because step() can take some time for large models.
keep
a filter function whose input is a fitted model object and the associated AIC statistic, and whose output is arbitrary. Typically keep will select a subset of the components of the object and return them. The default is not to keep anything.
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.

VALUE:

the stepwise-selected model is returned, with up to two additional components. There is an "anova" component corresponding to the steps taken in the search, as well as a "keep" component if the keep= argument was supplied in the call.

DETAILS:

A series of models is generated sequentially, where each model differs from its neighbors by a single term. The step methods differ in the way they construct this sequence: both in the way the set of candidates are generated for each step, and in the way the candidates are evaluated for selection. Note: this function should not be used with logistic regression.

Using Frames to Avoid Scoping Problems with step():

S scoping rules require that you take care when wrapping a step function in a function. (See the chapter "Data Management" in the Programmer's Guide or "S-Programming," Venables & Ripley, 2000 for more information.) For example, the following code will not work, even if you were to pass the Data object directly:

  > Step.Funct <- function(){
  >    Data  <- data.frame(matrix(rnorm(1000), ncol=20)); names(Data)[1] <- "Return"
  >    LongRegr <- lm(Return~.,data=Data,na.action=na.exclude,singular.ok=T)
  >    LongStep <- step(LongRegr, direction="both")
  > }
  > Step.Funct()



To make the above Step.Funct work, you can assign the data to frame 1, which lasts for the duration of the top level expression. For example:

  > Step.Funct <- function(){
  >    Data  <- data.frame(matrix(rnorm(1000), ncol=20))
  >    names(Data)[1] <- "Return"
  >    assign("..Step.Funct.Data", Data, frame=1)
  >    LongRegr <- lm(Return~.,data=..Step.Funct.Data,na.action=na.exclude,singular.ok=T)
  >    step(LongRegr, direction="both")
  > }
  > Step.Funct()

(Note: Give it a unique name to avoid multiple functions assigning objects to frame 1 from colliding with each other.)

SEE ALSO:

, , , , , , , , .

EXAMPLES:

# Create a glm object:
glm.object <- glm(skips ~ ., family=poisson, 
        data=solder.balance)

# Step through possible models quadratic interaction models
step(glm.object, scope= ~ . ^2)