set.seed(1)
DGP_OLS <- function() {
X <- runif(100,2,10)
Y <- 2*X + rnorm(100, sd = sqrt(X))
return(
c("beta_1_hat" = sum(X*Y)/sum(X^2))
)
}
estimates <- replicate(1000, DGP_OLS())
est_var_OLS <- var(estimates)
set.seed(1)
# define the function `DGP_WLS()`
# estimate the variance, assign the value to `var_est_WLS`
# compare the estimated variances
set.seed(1)
# define the function `DGP_WLS()`
DGP_WLS <- function() {
X <- runif(100,2,10)
Y <- 2*X + rnorm(100, sd = sqrt(X))
w <- 1/sqrt(X)
return(
c("beta_1_hat" = sum(w^2*X*Y)/sum((w*X)^2))
)
}
# estimate the variance, assign the value to `var_est_WLS`
est_var_WLS <- var(replicate(1000, DGP_WLS()))
# compare the estimated variances
est_var_WLS < est_var_OLS
test_predefined_objects(c("DGP_OLS","est_var_OLS"))
test_function_definition("DGP_WLS",
function_test =
test_expression_result("DGP_OLS()")
)
test_object("est_var_WLS")
test_or({
test_student_typed("est_var_WLS < est_var_OLS")
},{
test_student_typed("est_var_WLS > est_var_OLS")
},{
test_student_typed("est_var_OLS > est_var_WLS")
},{
test_student_typed("est_var_OLS < est_var_WLS")
})
success_msg("Nice! This indicates that the WLS estimator indeed has lower variance than OLS under heteroskedasticity.")