computer science

profilebbbzzz
OLS-Sim.txt

# Set the random seed set.seed(1234567) # set sample size and number of simulations n<-1000; r<-10000 # set true parameters: betas and sd of u b0<-1; b1<-0.5; su<-2 # initialize b0hat and b1hat to store results later: b0hat <- numeric(r) b1hat <- numeric(r) # Draw a sample of x, fixed over replications: x <- rnorm(n,4,1) # repeat r times: for(j in 1:r) { # Draw a sample of y: u <- rnorm(n,0,su) y <- b0 + b1*x + u # estimate parameters by OLS and store them in the vectors bhat <- coefficients( lm(y~x) ) b0hat[j] <- bhat["(Intercept)"] b1hat[j] <- bhat["x"] }