require(zoo) data <- read.csv('silver_driving_2011.csv') data$date <- as.Date(data$date) # A replication of Silver's analysis, based on his description # of the model in the original article. m1 <- lm(miles ~ unemp + gasprice + date + month, data=data) # Add the effect of gas prices 12 months earlier. This variable was discussed # by Silver, but apparently didn't make it into his model. # Also add stock prices and house prices, to account for wealth effects. m2 <- lm(miles ~ unemp + gasprice + gasprice_lag12 + date + stocks + housing + month, data=data) par(mar=c(2,2,1,1)) # Create 12-month rolling averages of both the actual data and the predictions, # to make the graph easier to read. date <- data$date[-c(1:11)] miles12 <- rollapply(data$miles,12,mean) pred.silver <- rollapply(predict(m1),12,mean) pred.frase <- rollapply(predict(m2),12,mean) plot(date,miles12,type="l",col="blue",lwd=3,xaxs="i") points(date,pred.silver,type="l",col="darkgreen",lwd=2) points(date[-c(1:12)],pred.frase,type="l",col="red",lwd=2) legend("topleft", c("Actual","Reconstructed Nate Silver model","Model with lagged gas prices,\n housing and stock prices"), lty=c(1,1,1),col=c("blue","darkgreen","red"),cex=0.8,lwd=2) text(as.Date('2004-01-01'),210, "Seasonally-adjusted Vehicle Miles Traveled, billions\n(12 month moving average)")