In: Statistics and Probability
In R, how do you create a bootstrap distribution for the 5% trimmed mean by taking 1000 resamples (with replacement), with a given dataset named 'data'? I am just looking for the correct R code that will allow me to plug in my data to find the 5% trimmed mean by taking 1000 resamples. The dataset is composed of salaries of a random sample, and I am not providing it because the only question I have is how to correctly format this bootstrapping code in R. This is all I need.
R code with comments (all statements starting with # are comments)
#set the random seed
set.seed(123)
# get the data,
#data<-read....
#assuming that the data is in the dataset named data
# get the sample size (That is number of observations in
data)
# assuming that data has 1 column, else use data$salary everywhere,
if data is a dataframe
n<-nrow(data)
#set the bootstrap resamples
B<-1000
#initialize variable to hold the trimmed mean of resamples
mustar<-numeric(B)
for (i in 1:B){
#sample with replacement using sample(), assuming data
has 1 column, else use data$salary
s<-sample(data[,1],size=n,replace=TRUE)
#calculate the 5% trimmed mean
mustar[i]<-mean(s,0.05)
}
#Do all the things that you want to do with this, for
example
#plot the distribution of mean
hist(mustar,xlab="average Salary",freq=FALSE, main="Bootstrap
distribution of mean Salary")
#98% confidence interval using percentile
# get the value of alpha
alpha<-1-98/100
ci<-quantile(mustar,c(alpha/2,1-alpha/2))
sprintf("98% confidence interval for the mean Salary is
[%.4f,%.4f]",ci[1],ci[2])