## Settings for RMarkdown http://yihui.name/knitr/options#chunk_options
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, tidy = FALSE,
echo = TRUE, fig.width = 7, fig.height = 7)
options(width = 116, scipen = 10)
Applied Regression Analysis and Other Multivariable Methods, Chapter 16: Selecting the Best Regression Equation.
Whenever practical, the all possible regression procedure is to be preferred over any other variable selection strategy.
## Load survival package
library(survival)
## Create outcome variable, then survival object
pbc <- within(pbc, {
status.dichotomous <- status > 1
survival.vector <- Surv(time, status.dichotomous)
})
## Create vectors for outcome and predictors
outcome <- c("survival.vector")
predictors <- c("trt", "age", "sex", "ascites","hepato","spiders")
dataset <- pbc
## The lines below should not need modification.
## Create list of models
list.of.models <- lapply(seq_along((predictors)), function(n) {
left.hand.side <- outcome
right.hand.side <- apply(X = combn(predictors, n), MARGIN = 2, paste, collapse = " + ")
paste(left.hand.side, right.hand.side, sep = " ~ ")
})
## Convert to a vector
vector.of.models <- unlist(list.of.models)
## Fit coxph to all models
list.of.fits <- lapply(vector.of.models, function(x) {
formula <- as.formula(x)
fit <- coxph(formula, data = dataset)
result.AIC <- extractAIC(fit)
data.frame(num.predictors = result.AIC[1],
AIC = result.AIC[2],
model = x)
})
## Collapse to a data frame
result <- do.call(rbind, list.of.fits)
## Sort and print
library(doBy)
orderBy(~ AIC, result)
num.predictors AIC model
55 4 1190 survival.vector ~ age + ascites + hepato + spiders
60 5 1191 survival.vector ~ trt + age + ascites + hepato + spiders
62 5 1191 survival.vector ~ age + sex + ascites + hepato + spiders
63 6 1192 survival.vector ~ trt + age + sex + ascites + hepato + spiders
56 4 1196 survival.vector ~ sex + ascites + hepato + spiders
41 3 1197 survival.vector ~ ascites + hepato + spiders
61 5 1198 survival.vector ~ trt + sex + ascites + hepato + spiders
51 4 1199 survival.vector ~ trt + ascites + hepato + spiders
35 3 1199 survival.vector ~ age + ascites + hepato
45 4 1200 survival.vector ~ trt + age + ascites + hepato
52 4 1200 survival.vector ~ age + sex + ascites + hepato
57 5 1202 survival.vector ~ trt + age + sex + ascites + hepato
19 2 1204 survival.vector ~ ascites + hepato
38 3 1204 survival.vector ~ sex + ascites + hepato
36 3 1204 survival.vector ~ age + ascites + spiders
53 4 1205 survival.vector ~ age + sex + ascites + spiders
46 4 1205 survival.vector ~ trt + age + ascites + spiders
29 3 1206 survival.vector ~ trt + ascites + hepato
48 4 1206 survival.vector ~ trt + sex + ascites + hepato
58 5 1206 survival.vector ~ trt + age + sex + ascites + spiders
39 3 1211 survival.vector ~ sex + ascites + spiders
20 2 1213 survival.vector ~ ascites + spiders
49 4 1213 survival.vector ~ trt + sex + ascites + spiders
37 3 1214 survival.vector ~ age + hepato + spiders
54 4 1215 survival.vector ~ age + sex + hepato + spiders
30 3 1215 survival.vector ~ trt + ascites + spiders
47 4 1216 survival.vector ~ trt + age + hepato + spiders
59 5 1217 survival.vector ~ trt + age + sex + hepato + spiders
14 2 1226 survival.vector ~ age + hepato
13 2 1226 survival.vector ~ age + ascites
32 3 1227 survival.vector ~ age + sex + ascites
33 3 1227 survival.vector ~ age + sex + hepato
23 3 1227 survival.vector ~ trt + age + ascites
24 3 1228 survival.vector ~ trt + age + hepato
42 4 1228 survival.vector ~ trt + age + sex + ascites
43 4 1229 survival.vector ~ trt + age + sex + hepato
40 3 1230 survival.vector ~ sex + hepato + spiders
16 2 1232 survival.vector ~ sex + ascites
50 4 1232 survival.vector ~ trt + sex + hepato + spiders
4 1 1232 survival.vector ~ ascites
21 2 1233 survival.vector ~ hepato + spiders
34 3 1233 survival.vector ~ age + sex + spiders
26 3 1234 survival.vector ~ trt + sex + ascites
15 2 1234 survival.vector ~ age + spiders
9 2 1234 survival.vector ~ trt + ascites
31 3 1235 survival.vector ~ trt + hepato + spiders
44 4 1235 survival.vector ~ trt + age + sex + spiders
25 3 1236 survival.vector ~ trt + age + spiders
17 2 1241 survival.vector ~ sex + hepato
5 1 1242 survival.vector ~ hepato
27 3 1243 survival.vector ~ trt + sex + hepato
10 2 1243 survival.vector ~ trt + hepato
18 2 1251 survival.vector ~ sex + spiders
28 3 1253 survival.vector ~ trt + sex + spiders
6 1 1256 survival.vector ~ spiders
11 2 1258 survival.vector ~ trt + spiders
7 2 1263 survival.vector ~ trt + age
22 3 1263 survival.vector ~ trt + age + sex
8 2 1280 survival.vector ~ trt + sex
1 1 1282 survival.vector ~ trt
2 1 1724 survival.vector ~ age
12 2 1724 survival.vector ~ age + sex
3 1 1746 survival.vector ~ sex