Skip to content

Instantly share code, notes, and snippets.

@mattblackwell
Created May 30, 2012 18:01
Show Gist options
  • Save mattblackwell/2837985 to your computer and use it in GitHub Desktop.
Save mattblackwell/2837985 to your computer and use it in GitHub Desktop.
Dynamic plots for demonstrating attenuation bias
### Create plots for MO talk
library(tikzDevice)
x <- rnorm(200,0,.4)
y <- x + rnorm(200, 0, .4)
night <- rgb(26,31,30, max=255)
beach <- rgb(227,223,186, max=255)
red60 <- rgb(1,.4,.4)
tangyblue <- rgb(108,189,181, max=255)
purp <- rgb(181.5,145.5, 141.5, max=255)
tikz("atten.tex", width = 5*(4/3), height = 5)
par(mar=c(1,1,1,1)+.1, bg=night, fg=beach, col=beach, col.axis=beach, col.lab=beach)
plot(x,y,xlim=c(-2,2), ylim=c(-2,2), xlab="", ylab="", axes=FALSE, cex=.8, pch=19)
abline(v=0, lwd=3)
abline(h=0, lwd=3)
text(x=2,y=0, "\\LARGE $x^*$", pos=3)
text(x=0,y=2, "\\LARGE $y$", pos=4)
abline(lm(y~x), col = tangyblue, lwd=6)
text(x=2,y=-2, "\\LARGE $\\sigma^2_u = 0$", pos=2, col=red60)
text(x=2,y=2, "\\LARGE truth", col=tangyblue, pos=2)
dev.off()
rho <- c(.1,.2,.3,.4,.6,.8, 1, 2)
for (i in 1:length(rho)) {
x.err <- x + rnorm(200,0,rho[i])
tikz(paste("atten",i,".tex",sep=""), width = 5*(4/3), height = 5)
par(mar=c(1,1,1,1)+.1, bg=night, fg=beach, col=beach, col.axis=beach, col.lab=beach)
plot(x.err,y,xlim=c(-2,2), ylim=c(-2,2), xlab="", ylab="", axes=FALSE, cex=.8, pch=19)
abline(v=0, lwd=3)
abline(h=0, lwd=3)
text(x=2,y=0, "\\LARGE $x$", pos=3)
text(x=0,y=2, "\\LARGE $y$", pos=4)
abline(lm(y~x.err), lwd=6, col=red60)
abline(lm(y~x), col= tangyblue, lwd=6)
text(x=2,y=-2, paste("\\LARGE $\\sigma^2_u = $",rho[i],sep=""), pos=2, col=red60)
text(x=2,y=2, "\\LARGE truth", col=tangyblue, pos=2)
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment