Skip to content

Instantly share code, notes, and snippets.

@phewson
Created October 8, 2013 19:12
Show Gist options
  • Save phewson/6889912 to your computer and use it in GitHub Desktop.
Save phewson/6889912 to your computer and use it in GitHub Desktop.
This is an animation using the manipulate package in R-studio. It uses the Anscombe data - you get to choose which pair. Then you slide through the datasets and the line is computed omitting that data point, and the leave one out residual is illustrated. I would like to tidy up a few details on this, but it works as a first step.
require(MASS)
require(manipulate)
data(anscombe)
plotit <- function(whichpair, i){
eval(parse(text = paste("sortit <- order(anscombe$x", whichpair, ")", sep = "")))
eval(parse(text = paste("data.df <- data.frame(x=anscombe$x", whichpair, "[sortit], y=anscombe$y", whichpair, "[sortit])", sep = "")))
ylims <- c(min(data.df$y)*0.8, max(data.df$y)*1.2)
xlims <- c(min(data.df$x)*0.8, max(data.df$x)*1.2)
ytext1 <- ylims[1] + 0.8 * (ylims[2]-ylims[1])
ytext2 <- ylims[1] + 0.7 * (ylims[2]-ylims[1])
xtext <- ylims[1] + 0.2 * (xlims[2]-xlims[1])
#i <- 4
cols = rep("green", 11)
cols[i] <- "red"
pchs <- rep(16, 11)
pchs[i] <- 4
par(bty = "n", xpd = NA, las = 1)
plot(data.df$y~data.df$x, pch = pchs, col = cols, ylim = ylims, xlab = "x", ylab = "y", main = "Illustration of \n leave one out residuals", xlim = xlims)
text(data.df$x, rep(4,11), round(studres(lm(data.df$y~data.df$x)),2), cex = 0.4)
m1 <- lm(data.df$y[-i] ~ data.df$x[-i])
sigma2 <- summary(m1)$sigma^2
t1 <- bquote(hat(sigma)^2 == .(round(sigma2,6)))
text(xtext, ytext2, t1)
t3 <- bquote(paste(hat(beta)[1]==.(round(coef(m1)[1],2))," ", hat(beta)[2]==.(round(coef(m1)[2],2))))
text(xtext, ytext1, t3)
abline(m1, col = "blue")
##??pred.df <- data.frame(x=data.df$x[i])
##??yhat <- predict(m1, newdata=pred.df)
yhat <- coef(m1)[1] + coef(m1)[2] * data.df$x[i]
posit <- sign(yhat) + 2
t2 <- bquote(e[-i] == .(round(data.df$y[i]-yhat, 2)))
text(data.df$x[i], data.df$y[i], t2, col = "red", pos = posit, offset = 2)
arrows(data.df$x[i], yhat, data.df$x[i], data.df$y[i], length = 0.01, col = "red")
##Sys.sleep(2)
}
manipulate(
plotit(whichpair, thepoint),
whichpair = picker("One"="1", "Two"="2", "Three"="3", "Four"= "4", label = "Which Anscombe Data Set"),
thepoint = slider(1, 11, step = 1, label = "Which point") )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment