#-------------------------------------- # Project: Election 2016 #-------------------------------------- mydata = read.table("http://faculty.chicagobooth.edu/nicholas.polson/teaching/41000/election2016.txt",header=T) attach(mydata) head(mydata) # Correlation table cor(cbind(G,P,Z)) ################### # Model 1 ################### model1 = lm(VP~G+P+Z) summary(model1) plot(model1,labels.id=President) ################### # Model 2 ################### # G:I fits the model with G+I and the interaction G*I model2 = lm(VP~G:I+P:I+Z:I+DPER+DUR+I+WAR) summary(model2) # Diagnostics and Model Selection plot(model2,labels.id=President) ################### # Prediction ################### # construct the new X newdata = data.frame(cbind(G=3.03,P=1.33,Z=3)) # predict with the first model (level = 0.95) pred = predict.lm(model1,newdata,interval="prediction",level=0.95)