Mastering R: From Function Creation to Practical Applications
1. Introduction to R’s Plotting and Function Capabilities
R offers a wide range of functions for data visualization and analysis. For example, the polygon
function can be used to approximate the range of 100 Brownian motions and a 90% confidence band. Here is the code to achieve this:
par(mar=c(2,2,2,2))
x=matrix(0,ncol=100,nrow=10^4)
for (t in 2:10^4)
x[t,]=x[t-1,]+rnorm(100)*10^(-2)
plot(seq(0,1,le=10^4),x[,1],ty="n",
ylim=range(x),xlab="",ylab="")
polygon(c(1:10^4,10^4:1)/10^4,c(apply(x,1,max),
rev(apply(x,1,min))),col="gold",bor=F)
polygon(c(1:10^4,10^4:1)/10^4,c(apply(x,1,quantile,.95),
rev(apply(x,1,quantile,.05))),col="brown",b