Now that was an embarassment, loops are very inefficient

R’s learning curve can be very steep, and thankfully I am surrounded by intelligent and smart colleagues. The point is that the loop script in the last post is embarrassingly inefficient, and will work horribly if one intends to simulate a large sample.

Here’s a much more elegant solution, for 3.1 of EMT,but this time using the function argument in R.The script is provided by Evan Jo.

ARsample<-function(beta1=1,beta2=.8,n=25,y0=0){
y<-vector(length=n+1);y[1]<-y0
for (i in 2:n+1){
y[i]<-beta1+beta2*y[i-1]+rnorm(1)}

m<-embed(y,2);return(data.frame(y=m[,1],y1=m[,2]))};

meanbeta<-function(samplesize=25,n=100){
m<-data.frame(beta1=NA,beta2=NA);
for(i in 1:n){m[i,]<-coef(lm(y~y1,data=ARsample(n=samplesize)))}
return(colMeans(m));}

In particular look at how efficient this script is compared to the previous one.

And finally, this is why the honours stream is so much more intellectually rewarding to be in.

PS: Don’t worry, I will still be writing about economics, and hopefully when Quantdary takes off we’ll start having more interesting economics/finance related posts.

Advertisements

Simulation Script for ETM Question 3.1(Public Service)

I promised I’ll update my blog, but I have been really really busy. Anyway, this post is not about the KLCI as promised(sorry to disappoint,I hardly had time for myself either), but rather, it is a loop for Q3.1 in Davidson and McKinnon’s Econometric Theory and Methods. For all the poor souls who have just started learning R.

B1<-1
B2<-0.8
y0<-0
B1hat<-numeric(0)
B2hat<-numeric(0)

for(j in 1:100){
residualsj<-numeric(0)
residualsj<-rnorm(200,0,1)
ysimj<-y0
for(i in 1:200){
ynew<-B1+B2*ysimj[i]+residualsj[i]
ysimj<-c(ysimj,ynew)
}
OLSj<-lm(ysimj[2:201]~ysimj[1:200])
beta1j<-coefficients(OLSj)[1]
beta2j<-coefficients(OLSj)[2]
B1hat[j]<-beta1j
B2hat[j]<-beta2j
}

Time permitting I’ll write an explanatory note with the #, although I hardly doubt it with midterms and a lot of other deadlines vying for whatever precious time I have. To change the sample size just change the numbers in the “i” part of the loop. To change the number of runs, just change the numbers in the j part of the loop.

Update:I made some errors in the previous post and those have since be corrected. Note that this script is meant to simulate an OLS drawing from 200 observations 100 times, and feel free to tweak around to adjust the trials and sample size.

This should also make it easier for you to attempt the last 2 questions. For information on how to extract vectors out from a data frame and then transforming it, I’d refer you to Mirza Trokic’s website.
http://www.mirzatrokic.ca/aes.html

He has the most concise and helpful R guide that I have ever seen.