# verify that the alternative estimator is unbiased
n <- 100
w <- c(rep((1+0.5)/n, n/2), rep((1-0.5)/n, n/2))
# define the alternative estimator mu_tilde
# compute repeatedly estimates for both estimators and store the results in est_bar and est_tilde
set.seed(123)
# compute the sample variances for est_bar and est_tilde
# verify that the alternative estimator is unbiased
n <- 100
w <- c(rep((1+0.5)/n, n/2), rep((1-0.5)/n, n/2))
sum(w)
# define the alternative estimator mu_tilde
mu_tilde <- function(x){sum(w*x)}
# compute repeatedly estimates for both estimators and store the results in est_bar and est_tilde
set.seed(123)
est_bar <- replicate(expr = mean(rnorm(100, 5, 10)), n = 10000)
est_tilde <- replicate(expr = mu_tilde(rnorm(100, 5, 10)), n = 10000)
# compute the sample variances for est_bar and est_tilde
var(est_bar)
var(est_tilde)
test_function_result("sum")
test_function_definition("mu_tilde",
function_test = {
test_expression_result("mu_tilde(1:100)")
test_expression_result("mu_tilde(2:101)")
})
test_object("est_bar")
test_object("est_tilde")
test_function_result("var", index = 1)
test_function_result("var", index = 2)
success_msg("Correct! The sample mean is more efficient (that is, has a lower variance) than the alternative estimator.")