Home‎ > ‎Schedule‎ > ‎

Demo: log transform

# an exponentially growing function
num.amoebas = function(x) { 2^x }
num.amoebas(3)

# by default, "plot" plots points, and "lines" plots lines.
# also, "plot" starts a new panel, and "lines" draws on the existing panel
xvalues = 1:10
plot(xvalues,num.amoebas(xvalues))
lines(xvalues,num.amoebas(xvalues))

# a more detailed look at how num.amoebas grows
xvalues = seq(1,10,.1)
plot(xvalues,num.amoebas(xvalues))
lines(xvalues,num.amoebas(xvalues))

# plotting with type = "n" means that nothing is plotted
# so we only see the line
plot(xvalues,num.amoebas(xvalues),type="n")
lines(xvalues,num.amoebas(xvalues))

# The log of this exponentially growing function is a straight line
plot(xvalues,log(num.amoebas(xvalues)))

# a function that doesn't grow quite as fast
num.amoebas3 = function(x) { 2^(x/3) }

# If we look at num.amoebas and num.amoebas3 this way,
# it looks as if num.amoebas3 didn't grow at all
# Note the us of "pch", which changes the character to be plotted.
plot(xvalues,num.amoebas(xvalues),xlim=c(0,10),ylim=c(0,1100))
points(xvalues,num.amoebas3(xvalues),pch=2)

# but in the log transform we can see that they both grow.
plot(xvalues,log(num.amoebas(xvalues)),xlim=c(0,10),ylim=c(0,10))
points(xvalues,log(num.amoebas3(xvalues)),pch=2)

# It doesn't matter much if we use log with basis e or log with basis 2
plot(xvalues,log(num.amoebas(xvalues),2),xlim=c(0,10),ylim=c(0,10))
points(xvalues,log(num.amoebas3(xvalues),2),pch=2)


####
# population growth over the centuries
pop = data.frame(year=c(1,400,800,1200,1400,1600,1800,1850,1900,1950,1975,2000), pop=c(170,190,220,360,350,545,900,1200,1625,2500,3900,6080))
# plotting two panels at the same time
par(mfrow = c(1,2))
# Again, we see more in the log transform
plot(pop$year,pop$pop)
plot(pop$year,log(pop$pop))


Comments