Anscombe’s Quartet – or playing with R after drinking London Pride.

Reading Data Analysis with Open Source Tools? If you’ve gone past chapter 3, table 3.1 then you have seen it. You have probably seen some plots too – that’s the one of the main things about the dataset – look at your data before breaking out the calculator.

I think the following block is the Anscombe dataset:

x1 y1 x2 y2 x3 y3 x4 y4
10.0	8.04	10.0	9.14	10.0	7.46	8.0	6.58
8.0	6.95	8.0	8.14	8.0	6.77	8.0	5.76
13.0	7.58	13.0	8.74	13.0	12.74	8.0	7.71
9.0	8.81	9.0	8.77	9.0	7.11	8.0	8.84
11.0	8.33	11.0	9.26	11.0	7.81	8.0	8.47
14.0	9.96	14.0	8.10	14.0	8.84	8.0	7.04
6.0	7.24	6.0	6.13	6.0	6.08	8.0	5.25
4.0	4.26	4.0	3.10	4.0	5.39	19.0	12.50
12.0	10.84	12.0	9.13	12.0	8.15	8.0	5.56
7.0	4.82	7.0	7.26	7.0	6.42	8.0	7.91
5.0	5.68	5.0	4.74	5.0	5.73	8.0	6.89

save it in a file called ‘anscombe.txt’. Then copy & paste the following R code into a file and save it as anscombe.R

setwd("path-to-your-working-directory")
data <- read.table("anscombe.txt", h=T)

info <- function(a,b) c( 
	paste("cc:", signif(cor(a,b),2)), 
	paste("var x,y:", signif(var(a),2), ",", signif(var(b),2)), 
	paste("mean x,y:", signif(mean(a),2), ",", signif(mean(b),2)),
	paste("linres", signif(lsfit(a,b)$coefficients[1],1))
	)

par(mfrow=c(2,2))
attach(data)

make_plot <- function(a,b) c(
	plot(a,b, xlim=c(3,20), xlab="x", ylab="y", ylim=c(2,13), main=info(a,b), pch=20),
	abline(lsfit(a,b), col="red") )

make_plot(x1,y1)
make_plot(x2,y2)
make_plot(x3,y3)
make_plot(x4,y4)

This, probably unidiomatic, R blurb should create a grid of plots. Each plot is one of the x,y pairs and above each figure is the correlation coefficient, variance, mean and intercepts.

It should look like this.

There’s actually a bit of precompiled code in R to do all this. Open the R terminal or GUI and type ??anscombe, this is an alias for help.search(“anscombe”). The familiar help dialog should pop-up with a single entry: Topic = Anscombe. Scroll to the bottom of the post and copy & paste the example code into your R terminal/gui. Enjoy the exciting plots.

R + London Pride => ftw!

I’ve been drinking, it’s late (00:50) and my g/f is nagging me to turn the computer off, if there’s a mistake let me know via the comments and I will fix it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s