### Week 4: CLRM Problems ### Code from class ## analyze the clasic Anscombe dataset (anscombe <- read.csv(url("http://www.kenbenoit.net/courses/quant2/anscombe.csv"))) round(apply(anscombe, 2, mean),2) # same means for x, y attach(anscombe) round(coef(lm(y1~x1)), 2) # same b0, b1 round(coef(lm(y2~x2)), 2) round(coef(lm(y3~x3)), 2) round(coef(lm(y4~x4)), 2) round(summary(lm(y1~x1))$r.squared, 2) # same R^2 round(summary(lm(y2~x2))$r.squared, 2) round(summary(lm(y3~x3))$r.squared, 2) round(summary(lm(y4~x4))$r.squared, 2) # plot the four x-y pairs par(mfrow=c(2,2), mar=c(4, 4, 1, 1)+0.1) # 4 plots in one graphic window plot(x1,y1) abline(lm(y1~x1), col="red", lty="dashed") plot(x2,y2) abline(lm(y2~x2), col="red", lty="dashed") plot(x3,y3) abline(lm(y3~x3), col="red", lty="dashed") plot(x4,y4) abline(lm(y4~x4), col="red", lty="dashed") detach(anscombe) ## run the Marsh and Benoit 2008 regression and produce diagnostic plots library(foreign) dail <- read.dta("dail2002.dta") m1 <- lm(votes1st ~ spend_total*incumb, data=dail) par(mar=c(4, 4, 1, 1)+0.1) plot(m1, which=1:6) ## Residual v. Fitted plots # restrict the cases that are missing or cannot be logged dail <- dail[complete.cases(dail$spend_total, dail$incumb, dail$votes1st) & is.finite(log(dail$spend_total)),] # residual v. fitted plot for spending: logged spending plot(lm(votes1st ~ log(spend_total)*incumb, data=dail), which=1) # residual v. fitted plot for spending: logged votes plot(lm(log(votes1st) ~ spend_total*incumb, data=dail), which=1) # residual v. fitted plot for spending: logged votes and spending plot(lm(log(votes1st) ~ log(spend_total)*incumb, data=dail), which=1) ## other graphs are created by one-line code from slides