R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>
>
>
> election.table <- read.table('http://www-stat.stanford.edu/~jtaylo/courses/stats191/data/election.table', header=T)
>
> pairs(election.table[,2:ncol(election.table)], cex.labels=3, pch=23,
+ bg='orange', cex=2)
>
>

>


> # This is the library with the leaps function
>
> # install.packages('leaps')
>
> library(leaps)
>
>
> # Leaps takes a design matrix as argument: throw away the intercept
> # column or leaps will complain
>
> X <- model.matrix(lm(V ~ I + D + W +G:I + P + N, election.table))[,-1]
>
> # Look at R^2
>
>
> election.leaps <- leaps(X, election.table$V, nbest=3, method='r2')
> plot(election.leaps$size, election.leaps$r2, pch=23, bg='orange', cex=2)
> best.model.r2 <- election.leaps$which[which((election.leaps$r2 == max(election.leaps$r2))),]
> print(best.model.r2)
1 2 3 4 5 6
TRUE TRUE TRUE TRUE TRUE TRUE
>
>

>


>
> # Look at Adjusted R^2
>
> election.leaps <- leaps(X, election.table$V, nbest=3, method='adjr2')
> plot(election.leaps$size, election.leaps$adjr2, pch=23, bg='orange', cex=2)
> best.model.adjr2 <- election.leaps$which[which((election.leaps$adjr2 == max(election.leaps$adjr2))),]
> print(best.model.adjr2)
1 2 3 4 5 6
TRUE TRUE FALSE FALSE TRUE TRUE
>

>


>
> election.leaps <- leaps(X, election.table$V, nbest=3, method='Cp')
> plot(election.leaps$size, election.leaps$Cp, pch=23, bg='orange', cex=2)
> best.model.Cp <- election.leaps$which[which((election.leaps$Cp == min(election.leaps$Cp))),]
> print(best.model.Cp)
1 2 3 4 5 6
FALSE TRUE FALSE FALSE TRUE TRUE
>
>

>


>
> proc.time()
user system elapsed
0.812 0.020 0.841
R script