# set seed for reproducibility
set.seed(123)
# attach the package `mvtnorm`
# set the number of sampling iterations
N <-
# initialize the vector `beta_hats`
beta_hats <-
# conduct the simulation using `for()`
for(i in 1:N) {
}
# compute the sample mean of the estimates
# set seed for reproducibility
set.seed(123)
# attach the package `mvtnorm`
library(mvtnorm)
# set number of sampling iterations
N <- 1000
# initialize the vector `beta_hats`
beta_hats <- c()
# loop estimation
for (i in 1:N) {
# simulate the dataset and set the column names
d <- data.frame(rmvnorm(1000, c(50, 100), sigma = cbind(c(10, 5), c(5, 10))))
colnames(d) <- c("X", "Y")
# add the measurement error to `X`
d[,1] <- d[,1] + rnorm(100,0,sqrt(10))
# estimate the simple linear regression model
ms_mod <- lm(Y ~ X, data = d)
# save the estimate
beta_hats[i] <- ms_mod$coefficients[2]
}
# compute the sample mean of the estimates
mean(beta_hats)
test_object("beta_hats")
test_output_contains("mean(beta_hats)")
success_msg("Well done! The sample mean should be close to 0.75 but it is about 0.25 due to the downward bias implied by the measurement error in X. Increase the sample size does not alleviate the bias (check this!) because OLS is inconsistent here.")