R Unbiasedness

From ECLR
Revision as of 08:27, 6 February 2015 by Rb (talk | contribs) (Created page with "This is R code to demonstrate that OLS parameter estimators are unbiased. # Code to demonstrate unbiasedness of OLS estimator Npop <- 100000 # Population Size N...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is R code to demonstrate that OLS parameter estimators are unbiased.

   # Code to demonstrate unbiasedness of OLS estimator
   Npop <- 100000  # Population Size
   Nsamp1 <- 100    # Sample Size
   Q <- 300  # number of samples taken
   u <- rnorm(Npop)      # true error terms
   beta <- matrix(c(0.5, 1.5),2,1)   # true parameters
   Xpop <- matrix(1,Npop,2) # initialise population X
   Xpop[,2] <- floor(runif(Npop)*12+8) # exp var uniform r.v. in [8,19]
   Ypop <- Xpop %*% beta + u    # %*% is matrix multiplication
   save_beta1 <- matrix(0,Q,1)
   for (i in 1:Q ) {
     sel1 <- ceiling(runif(Nsamp1)*Npop)  # select Nsamp1 observations
     Ysel <- Ypop[sel1,]    
     Xsel <- Xpop[sel1,]
     reg_sel <- lm(Ysel~Xsel[,2])   # estimate regression in sample
     save_beta1[i,1] <- reg_sel$coefficients[2]  # save estimated beta_1
 
   }
   hist(save_beta1,breaks = 15, col = "blue",plot = TRUE)
   # mean(save_beta1)