Write a simulation function (submit an R script) Write your own function, called
ID: 3181881 • Letter: W
Question
Write a simulation function (submit an R script) Write your own function, called bs(). It will produce a 95% bootstrap confidence interval for a mean. The user will provide a vector, and the number of repetitions for the simulation (nrep). You're writing your own function, so do not reference other bootstrap functions that exist in other packages.(e.g., there is a boot function in a bootstrap package - don't use this!) Your function should: Draw a sample the same size as the vector, selecting FROM the vector itself, with replacement. Compute the mean of the vector, and store it in another vector, Repeat the steps above n times. When done, compute the interval as the 2.5% and 97.5% quantiles of the vector of means. Round to 3 digits to the right of the decimal point, Print the interval on the console so that it looks something like this: 95% bootstrap interval for the mean: -0.309 to 0.073 Return the vector of means to the user.Explanation / Answer
# x is a single quantitative sample
# B is the desired number of bootstrap samples to take
boot.mean = function(x,N) {
n = length(x)
boot.samples = matrix( sample(x,size=n*N,replace=TRUE), N, n)
boot.statistics = apply(boot.samples,1,mean)
interval <- quantile(boot.statistics, c(0.025, 0.975))
print(paste( "95% bootstrap interval for the mean: ", interval[1], " to ", interval[2]))
return(boot.statistics)
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.