R code for linear regression

Share this page

To perform a linear regression as mentioned in the main article, simply copy and paste the script below into your R console once you have started up the program, and then modify it to analyse your own data.

rank<-seq(1,50,1)
freq<-c(22038615, 12545825, 10741073, 10343885, 10144200, 6996437, 6332195, 4303955, 3978265, 3872477, 3856916, 3430996, 3281454, 3081151, 2909254, 2683014, 2573587, 2485306, 1915138, 1885366, 1865580, 1820935, 1801708, 1776767, 1767638, 1712406, 1638830, 1635914, 1619007, 1490548, 1481869, 1379320, 1296879, 1181023, 1151045, 1083029, 1022775, 1018283, 992596, 969591, 933542, 925515, 919821, 892535, 892102, 874406, 857168, 829018, 824568, 795534)
plot(rank,freq,log="xy",xlab="Rank",ylab="Frequency")
xl<-log(rank)
yl<-log(freq)
est<-lm(yl~xl)
summary(est)
p<-predict(est)
px<-exp(p)
points(rank,px,type="l")

Back to the main article.

  • Want facts and want them fast? Our Maths in a minute series explores key mathematical concepts in just a few words.

  • As COP28, the 2023 United Nations Climate Change Conference, kicks off we look at how maths can help understand the climate crisis.

  • How do you create dramatic film out of mathematics? We find out with writer and director Timothy Lanzone.

  • Mathematics plays a central role in understanding how infectious diseases spread. This collection of articles looks at some basic concepts in epidemiology to help you understand this fascinating and important field, and set you up for further study.

  • Find out why the formula we use to work out conditional probabilities is true!

  • We talk about a play that explores the fascinating mathematical collaboration between the mathematicians GH Hardy and Srinivasa Ramanujan.