#
#
########## Variable selection based on bootstrapped sampling and calculation of stepwise AIC ##########
#
#
# This script assumes you have worked through all the previous notes from
# the web page and you have downloaded, installed, and updated all available
# R packages.
# Load the following libraries if you have not already.
library(foreign)
library(Rcmdr)
# The BootTesting.sav data file contains 1000 cases with one outcome (y) and 11 continuous
# predictors (x1, x2, x3 ... x11).
# Import the data using the 'foreign' libary and naming it 'boottesting'.
boottesting <- read.spss("http://researchsupport.unt.edu/class/Jon/R_SC/Module9/BootTesting.sav",
use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
summary(boottesting)
cor(boottesting)
reg1 <- lm(y~x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11,boottesting)
summary(reg1)
# Standardized coefficients (using QuantPsyc library & lm.beta function).
library(QuantPsyc)
lm.beta(reg1)
#########################################################################################################
# The following method is not model based and instead, relies on the Akaike Information Criterion (AIC)
# or the Bayesian Information Criterion (BIC) to select or indicate the best variables to construct
# a specified model; be that model an LM (OLS regression), aov (ANOVA), GLM, negbin, polr, survreg, or coxph.
###### Automated variable selection through bootstrapping the 'stepAIC' function of library(MASS). ######
# Using 'boot.stepAIC' to run bootstraps of the Stepwise Algorithm of stepAIC() for choosing variables
# to include in a model by AIC (or BIC). Library 'MASS' is also required, although it loads with Rcmdr.
library(bootStepAIC)
# Run the boot.stepAIC function. Note; k = 2 refers to true AIC, while k = log(n) refers to BIC.
# The default 'direction' is "both" (meaning both forward and backward).
# Note: this function can take a few minutes.
backward.AIC <- boot.stepAIC(reg1, data = boottesting, B = 1000, alpha = 0.05, direction = "backward", k = 2, verbose = TRUE)
backward.AIC
# More information on each function:
help(stepAIC)
help(boot.stepAIC)