(This article was first published on

**mickeymousemodels**, and kindly contributed to R-bloggers)A few days ago I heard a talk about Simpson’s paradox, and I decided to write a little example in R:

library(MASS) # For multivariate normals

# List of (vectors of) means

mu <- list(c(5, 175),

c(6.25, 110))

# List of covariance matrices

sigma <- list(rbind(c(0.75, 25), c(25, 1000)),

rbind(c(0.80, 10), c(10, 500)))

# Vector of colors

cols <- c("darkolivegreen", "midnightblue")

if (any(sapply(sigma, det) <= 0)) {

warning("One of your sigmas is not positive-definite")

}

vars <- c("height", "weight")

CreateDataFrame <- function(i, n=500) {

# Create data frame containing n observations from the ith group

df <- data.frame(type=rep(cols[i], n))

df[ , vars] <- mvrnorm(n, mu[[i]], sigma[[i]])

return(df)

}

df <- do.call(rbind, lapply(1:2, CreateDataFrame))

str2rgb <- function(str, alpha=255) {

# Convert a vector of strings to a vector of color codes,

# eg "darkblue" -> "#00008B96" (a semi-transparent darkblue)

# Is there a better way to do this?

rgb.matrix <- col2rgb(str)

return(rgb(rgb.matrix[1, ],

rgb.matrix[2, ],

rgb.matrix[3, ],

alpha,

maxColorValue=255))

}

# Sort data frame by vars[1] (for plotting)

df <- df[order(df[ , vars[1]]), ]

dev.new(height=8, width=10)

plot(df[ , vars],

main="Height and Weight in Two Populations",

col=str2rgb(as.character(df$type), alpha=128),

pch=as.integer(df$type),

xlim=(range(df$height) + c(-1.5, 1.5)),

ylim=(range(df$weight) + c(-20, 20)))

mtext("An illustration of Simpson's paradox")

# Vector of model formulas

formulas <- c("weight ~ height",

"weight ~ height + type",

"weight ~ height * type")

# List of fitted models

models <- lapply(formulas, glm, data=df)

# Plot model 1

lines(df$height, fitted.values(models[[1]]),

col="black", lwd=2, lty=2)

# Plot models 2:3

for (i in 2:3) {

for (col in cols) {

lines(df$height[df$type == col],

fitted.values(models[[i]])[df$type == col],

col="black", lwd=2, lty=(i + 1))

}

}

legend("topleft", formulas, bty="n", lwd=2, lty=2:4)

legend("topright", sprintf("group %s", 1:2), bty="n",

col=str2rgb(cols),

pch=as.integer(factor(cols)))

savePlot("simpson_height_weight.png")

The code plots heights and weights in two hypothetical populations; the paradox is that the variables are positively correlated within each group, but negatively correlated in the aggregate data.

Imagine you’ve just seen that plot, and now someone comes along and asks you whether height and weight are positively correlated. What would you answer? I’d say, “Yes, conditional on type,” but it’s just as correct to say “No.” Things could get pretty confusing when you start looking at larger, non-hypothetical datasets.

In short, amalgamation can reverse correlations that hold in every subgroup. Beware of aggregates!

To

**leave a comment**for the author, please follow the link and comment on his blog:**mickeymousemodels**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...