Q:

Automating nonsignificant term removal from linear models

Im working on a data set that includes categorical and continuous independent variables and want to find out what the minimum adequate model is.
This is the starting model:
mod1 <- lm(Richness ~ Distances*Flower*Veg*Canopy*factor(Vines), data = Data)
anova(mod1)

I am then removing nonsignificant interaction terms and variables by a manual process like so:
mod2 <- update(mod1, ~.-Canopy:factor(Vines))
anova(mod2)

Is there a way of automating this process?
Thank you for your help.

A:

Following what @nongkrong suggested about the step function I think I found a solution:
model.null = lm(Abundance ~ 1,
            data=data) #define the null model#
model.full = lm(Abundance ~ Distances*Flower*Veg*Canopy*factor(Vines), 
            data = data) #define the max model#

step(model.full, #start at the full model#
 scope = list(lower = model.null), # the lower limit of model it can produce#
 direction = "backward", #work by deleting terms until you get to the minimum model #
 data = data
)

It seems to work so far, if anyone sees a problem with this code please comment.

