Learning to display R-code online 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 investm ent',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 investm ent',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-todata-points-in-r (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 invest ment',font.lab=2) linEst=lm(y2~x) logEst=lm(y2~log(x)) lines(x,predict(linEst)) lines(x,predict(logEst))

There we go! Now hopefully on to some more interesting stuff!

R-code online.pdf

There we go! Now hopefully on to some more interesting stuff! Page 3 of 3. R-code online.pdf. R-code online.pdf. Open. Extract. Open with. Sign In. Main menu.

304KB Sizes 4 Downloads 102 Views

Recommend Documents

No documents