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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: