Skip to content

Instantly share code, notes, and snippets.

@mw55309
Created Jun 3, 2016
Embed
What would you like to do?
# data from https://twitter.com/LukeTurnerEsq/status/738308821207142400
d <- data.frame(min=c(18,30,40,50,60), max=c(29,39,49,59,85), remain=c(73,64,46,45,35), stringsAsFactors=FALSE)
# calc mid-points
mid <- d$min + ((d$max - d$min) / 2)
# fit model and plot
lmfit <- lm(d$remain~mid)
plot(mid, d$remain, pch=16, xlab="Age", ylab="% who support REMAIN in EURef")
abline(lmfit)
# predict
age <- 41
pred <- lmfit$coefficients[1] + lmfit$coefficients[2]*age
# plot prediction
lines(c(age,age), c(0, pred), lty=3)
lines(c(age, 0), c(pred, pred), lty=3)
# annotate
text(41, 36, "Mick's age: 41", pos=4, cex=0.8)
text(30, 55, paste("Predicted ", sprintf("%0.2f", pred), "% remain"), cex=0.8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment