f=function(x){2*x} # * is multiplication, \ is division x=seq(0,1,0.1); y=f(x) # makes grid 0.0, 0.1, ..., 0.9, 1.0 Finv=function(p){sqrt(p)} # sqrt is square root simulated.data=Finv(runif(100000)) # 100000 random draws from f(x)=2*x hist(simulated.data,freq=F) # make a histogram, freq=F makes it integrate to one lines(x,y) # superimpose the TRUE (usually unknown) density on top mean(simulated.data) # estimate mean through simulation sd(simulated.data) # estimate standard deviation quantile(simulated.data,0.25) # estimate Q1, i.e. x_0.25 from simulation quantile(simulated.data,0.75) # estimate Q3 Finv(0.25) # TRUE x_0.25 Finv(0.75) # TRUE x_0.75