Chapter:5-Resampling method, q9
library(MASS)
summary(Boston)
mean(Boston$medv)
sd(Boston$medv)/sqrt(length(Boston$medv))
library(boot)
set.seed(2)
mean.function = function(data,index){
d = data$medv
return(mean(d[index]))
}
bootstrap = boot(data=Boston,statistic=mean.function,R=1000)
bootstrap
The standard error of the mean using bootstrap is close to standard error estimate in (b)
#95% confidence interval
print("lower bound")
22.533-2*0.419
print("upper bound")
22.533+2*0.419
t.test(Boston$medv)
95% confidence interval calculated by bootstrap method is very close to the confidence interval in the t.test.
median(Boston$medv)
median.function = function(data,index){
d = data$medv
return(median(d[index]))
}
boot(data=Boston,statistic = median.function,R=1000)
The median value is 21.2 and its standard error is 0.379. The standard error is very small compared to the median value
quantile(Boston$medv, 0.1)
quantile.function = function(data,index){
d = data$medv
return(quantile(d[index],0.1))
}
boot(data=Boston,statistic=quantile.function,R=1000)
The 10th percentile is 12.75 with standard error of 0.499. The standard error is very small compared to the 10th percentile.