O'Reilly logo

R Statistical Application Development by Example Beginner's Guide by Prabhanjan Narayanachar Tattar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – model selection using the backward, forward, and AIC criteria

For the forward and backward selection procedure under the stepwise procedures of the model selection problem, we first define two functions: backwardlm and forwardlm. However, for the criteria-based model selection, say AIC, we use the step function, which can be performed on the fitted linear models.

  1. Create a function pvalueslm which extracts the p-values related to the covariates of an lm object:
    pvalueslm <- function(lm) {summary(lm)$coefficients[-1,4]}
  2. Create a backwardlm function defined as follows:
    backwardlm <- function(lm,criticalalpha) { lm2=lm while(max(pvalueslm(lm2))>criticalalpha) { lm2=update(lm2,paste(".~.-",attr(lm2$terms, "term.labels")[(which(pvalueslm(lm2)==max(pvalueslm(lm2))))],sep="")) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required