## Eryk Wdowiak ## 21 October 2012 ## R script to demonstrate that "our estimate of beta is an estimate" ## OLS gives us an unbiased estimate of beta ## but the true value of beta remains unknown ## suppose that the TRUE data generating process is: ## yy = alpha + beta*xx + rnorm() ## let's generate lots of samples and estimate beta for each one ## here are our parameters nn <- 50 alphatrue <- 0 betatrue <- 2 ## now generate some data giving both "xx" and "uu" unit variance, ## so that we can expect the std. error of "betahat" to be sqrt(1/nn) betahat <- NA for (i in 1:100) { xx <- rnorm(nn) ; mnx <- mean(xx) uu <- rnorm(nn) yy <- alphatrue + betatrue*xx + uu ols <- lm( yy ~ xx ) betahat[i] <- coef(ols)["xx"] } ## now let's look at the distribution of "betahat" hist( betahat ) Results <- c(" beta true: ",betatrue,".000, se(beta true): ",round(sqrt(1/nn),3),"\n") Results <- c(Results,"mean(beta hat): ",round(mean(betahat),3),", sd(beta hat): ",round(sd(betahat),3),"\n") cat("\n",Results,"\n",sep="")