The goal of this exercise is to learn how to embed an R Markdown document in a website.
To illustrate in-line code, do simple math: five + five = 5+5
Next, I want to generate some code to illustrate plotting. First, just consider a logistic equation, \[y=log(x)\]
Because the y-value increases more slowly with increasing x, let’s say this represents the law of diminishing returns
x=1:100
y=log(x)
plot(x,y,main='Diminishing returns',xlab='Invested resources',ylab='Return on investment',font.lab=2)
Okay, so the plot looks good now! Let’s add some noise around the theoretical value to make it look more like real-world data
#Let's keep x-values the same
#x=1:100
#Now, update y values to include noise
par(mfrow=c(1,3))
y=log(x) + rnorm(length(x),sd=0.1)
y2=log(x) + rnorm(length(x),sd=0.3)
y3=log(x) + rnorm(length(x),sd=1)
plot(x,y,main='Diminishing returns',xlab='Invested resources',ylab='Return on investment',font.lab=2)
plot(x,y2)
plot(x,y3)
Okay, now let’s try to fit the data using a linear model and the true form of the data to compute the errors.
P.S., stackoverflow is really useful! http://stackoverflow.com/questions/11569068/fitting-logarithmic-curve-to-data-points-in-r
#plot(x,y2,main='Diminishing returns',xlab='Invested resources',ylab='Return on investment',font.lab=2)
pltFits=function(yvar=y2,xvar=x)
{
ymax = max(y,y2,y3)
plot(x,yvar,main='Diminishing returns',xlab='Invested resources',ylab='Return on investment',font.lab=2,ylim=c(0,ymax))
linEst=lm(yvar~xvar)
logEst=lm(yvar~log(xvar))
lines(xvar,predict(linEst),lwd=3,col='red',lty=2)
lines(xvar,predict(logEst),lwd=3,col='blue',lty=2)
#I should plot the actual function in a solid line! Legend: linear/log estimates and true function
resMax = max(abs(linEst$residuals),abs(logEst$residuals))
plot(linEst$residuals,ylim=c(-resMax,resMax),col='red',main='Residuals',xlab='Invested resources',ylab='Residuals',font.lab=2)
points(logEst$residuals,ylim=c(-resMax,resMax),col='blue')
barplot(summary(logEst)$r.squared,summary(linEst)$r.squared)
}
par(mfcol=c(2,3),cex.lab=1.4,cex.main=1.7)
pltFits(y)
pltFits(y2)
pltFits(y3)
Looks like the two models cannot be distinguished in the limit of high noise, but can be in the low noise limit
There we go! Now hopefully on to some more interesting stuff!