Bootstraping Scripts

Now that we have wrapped up Chapter 4 in class, we’re all required to perform bootstrap simulations for the problem set. Econometrics is a class that requires a lot of commitment, not only on grasping the materials and going through the painful algebra, but also in terms of learning how to programme in R.

In the spirit of the last few posts, here then, is the script of loops that I had written to complete the bootstrap simulations, both parametric and non-parametric.

P/S: To anyone who is following this blog faithfully, I swear I will be get back to writing when Winter break begins.

#Simulating DGP under the Null
lmnull<-lm(cs$V2~cs$V3)
#Use the estimate under the null to begin bootstrap.  
Loops for Parametric Bootstrapping
#For Bootstrap Size 99
BST99<-numeric(0)
for(i in 1:99){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients[“cs$V4″,”t value”]
BST99[i]<-B3simT}
#Notice that I applied the absolute function here. We are doing a 2-tailed test,and thus it is important that we apply the absolute operator on the entire
vector. We’d want extreme values that are too large or too small, and then be able to discriminate statistically. 
BST99<-abs(BST99)
#To get R to calculate the frequency of X greater or equal to a specified values while implicitly using a logical argument.A very useful trick.
PBST99<-mean(BST99>=2.349)
#For Bootstrap Size 999
BST999<-numeric(0)
for(i in 1:999){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients[“cs$V4″,”t value”]
BST999[i]<-B3simT}
BST999<-abs(BST999)
PBST999<-mean(BST999>=2.349)
#For Bootstrap Size 9999
BST9999<-numeric(0)
for(i in 1:9999){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients[“cs$V4″,”t value”]
BST9999[i]<-B3simT}
BST9999<-abs(BST9999)
PBST9999<-mean(BST9999>=2.349)
Loops for Non-Parametric Bootstrapping 
# Extracting residuals from the OLS regression under the null, and then scaling them by (n/n-k)^(1/2) where k is the number of restrictions under the null.
nullres<-residuals(lmnull)
scalednullres<-(sqrt(50/48))*nullres
#For Non-Parametric Bootstrap Size 99
BSTNP99<-numeric(0)
for(i in 1:99){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients[“cs$V4″,”t value”]
BSTNP99[i]<-B3NPSimT}
BSTNP99<-abs(BSTNP99)
PBSTNP99<-mean(BSTNP99>=2.349)
#For Non-Parametric Bootstrap Size 999
BSTNP999<-numeric(0)
for(i in 1:999){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients[“cs$V4″,”t value”]
BSTNP999[i]<-B3NPSimT}
BSTNP999<-abs(BSTNP999)
PBSTNP999<-mean(BSTNP999>=2.349)
#For Non-Parametric Bootstrap Size 9999
BSTNP9999<-numeric(0)
for(i in 1:9999){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients[“cs$V4″,”t value”]
BSTNP9999[i]<-B3NPSimT}
BSTNP9999<-abs(BSTNP9999)
PBSTNP9999<-mean(BSTNP9999>=2.349)
Advertisements