require(graphics)
# Example from Burnham and Anderson (2002), page 100:
data(Cement)
fm1 <- lm(y ~ ., data = Cement)
ms1 <- dredge(fm1)
confset.95p <- get.models(ms1, subset=cumsum(weight) <= .95)
avgm <- model.avg(confset.95p)
nseq <- function(x, len=length(x)) seq(min(x, na.rm=TRUE),
max(x, na.rm=TRUE), length=len)
# New predictors: X1 along the range of original data, other
# variables held constant at their means
newdata <- as.data.frame(lapply(lapply(Cement[1:5], mean), rep, 25))
newdata$X1 <- nseq(Cement$X1, nrow(newdata))
# Predictions from each of the models in a set:
pred <- sapply(confset.95p, predict, newdata=newdata)
pred <- cbind(pred,
averaged.subset = predict(avgm, newdata),
averaged.full = predict(avgm, newdata, full = TRUE))
matplot(x=newdata$X1, y=pred, type="l", lwd=c(rep(1,ncol(pred)-2), 2, 2),
xlab="X1", ylab="y", col=c(3:7, 1,2), lty=c(1:5,1,1))
legend("topleft",
legend=c(lapply(confset.95p, formula),
paste(c("subset", "full"), "averaged")),
col=c(3:7, 1,2), lty=c(1:5,1,1), lwd=c(rep(1,ncol(pred)-2), 2, 2), cex = .75)
Run the code above in your browser using DataLab