# inequality R code

Here’s the R code you need for the simulation I discuss in the essay ‘Why inequality is bad’

# You need a library called ‘ineq’ that calculates the Gini index for you
install.packages(“ineq”)
library(ineq)

# Let there be k countries from each of which we sample n inhabitants
# Within each let income be drawn from N(10000, sigma) where sigma varies from country to country
# Let the health outcome be related to income by h=sqrt(x) + E, where E~N(0, 10)
# The following function willproduce a scatterplot of the type you see in The Spirit Level
wilkinson=function(){
k=30
n=100
inequality=seq(500, 5000, length.out=k)
mean.health=NULL
ginis=NULL
for (country in 1: k){
incomes=rnorm(n, mean=10000, sd=inequality[country])
incomes[incomes<=0]=1
health=sqrt(incomes) + rnorm(n, 4)
ginis[country]=ineq(incomes, type=”Gini”)
mean.health[country]=mean(health)
}
mean.health=10+(mean.health-mean(mean.health))/sd(mean.health)
plot(mean.health~ginis, xlab=”Gini coefficient”, ylab=”Average health outcome”, ylim=c(6, 12), xlim=c(0, 0.3),
las=1, pch=19)
abline(lm(mean.health~ginis))
}

# Now call the function
wilkinson()