I want to use a custom estimator Yhat=(1/(n-4))*sum(Y_i) as an estimator for the mean of Y_i.
How do I draw, say, 20 observations from the distribution N(4,10) and compute the estimate of the sample mean using Yhat, and then repeat this procedure k amount of times in a loop and save the results in a matrix in r?
First, since you want the estimate of the mean, you don't need a matrix, a vector should be fine.
Yhat <- function(x) {
if (length(x) <= 4) {
stop("vector x must be at least 5 elements long")
} else {
sum(x) / (length(x) - 1)
}
}
k <- 100
result <- replicate(k, {
simulations <- rnorm(20, 4, 10)
Yhat(simulations)
})
result