Here I introduce a R function made by myself to calculate several confidence intervals of mean/median including exact CI, basic bootstrap CI, percentile bootstrap CI & studentized bootstrap CI.
R code
Arguments
There are five arguments in this R function bootCI()
:
x
— a numeric vector specifying $x_i$alpha = 0.05
— a numeric specifying $\alpha$ valuealternative = c("t", "l", "g")
— a single character string specifying two-sided, less or greater tailB = 1999
— a integer specifying number of bootstrap samplesquantileAlgorithm = 7
— a integer specifying the argumenttype
passed toquantile()
Examples
We can define a numeric vector $x = [5\, 2\, 3\, 6\, 8\, 19\, 1.5]$ including $N=7$ items as num
:
> num <- c(5, 2, 3, 6, 8, 19, 1.5)
After loading the above function, we can call the function to calculate CIs:
> bootCI(num) Summary of x Min. 1st Qu. Median Mean 3rd Qu. Max. 1.500 2.500 5.000 6.357 7.000 19.000 CIs of mu 2.5% 97.5% $CI.exact 0.7778728 11.936413 $CI.basic 1.7125000 9.714286 $CI.percentile 3.0000000 11.001786 $CI.studentized 2.6785623 17.669412
The above results show that
\begin{aligned} \text{exact CI} & = ( \hat{\mu} + t_{\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} \;,\; \hat{\mu} + t_{1-\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} ) \\ \text{basic CI} & = ( 2\hat{\mu} - \hat{\mu}_{1-\frac{\alpha}{2}}^{*} \;,\; 2\hat{\mu} - \hat{\mu}_{\frac{\alpha}{2}}^{*} ) \\ \text{percentile CI} & = ( \hat{\mu}_{\frac{\alpha}{2}}^* \;,\; \hat{\mu}_{1-\frac{\alpha}{2}}^* ) \\ \text{studentized CI} & = ( \hat{\mu} + t^*_{\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} \;,\; \hat{\mu} + t^*_{1-\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} ) \\ \end{aligned}where $\hat{\mu}$ is mean of $x$, $t_{\frac{\alpha}{2}}$ is lower $\alpha/2$ critical value for the $t$ distribution given $\text{df} = N-1$, $\hat{se}_{\hat{\mu}}$ is the standard error of the mean of $x$, $\hat{\mu}_{\frac{\alpha}{2}}^*$ is $\alpha/2$ percentile of the mean of bootstrapped $x$, and $t^*_{\frac{\alpha}{2}} = \frac{\hat{\mu^*}-\hat{\mu}}{\hat{se}^*_{\hat{\mu^*}}}$ is t-value based on bootstrapped $x$. See
- J. Carpenter and J. Bithell (2000). Bootstrap confidence intervals: when, which, what? A practical guide for medical statisticians. Statist. Med. 19:1141-1164
- Wikipedia contributors, 'Bootstrapping (statistics)', Wikipedia, The Free Encyclopedia, 17 December 2013, 21:28 UTC, <http://en.wikipedia.org/w/index.php?title=Bootstrapping_(statistics)&oldid=586549893> [accessed 31 December 2013]
for more details.
We can assign a different $\alpha$, direction or number of bootstrap samples:
> bootCI(num, alpha=0.01) Summary of x Min. 1st Qu. Median Mean 3rd Qu. Max. 1.500 2.500 5.000 6.357 7.000 19.000 CIs of mu 0.5% 99.5% $CI.exact -2.0962642 14.81055 $CI.basic -0.1428571 10.28571 $CI.percentile 2.4285714 12.85714 $CI.studentized 1.1394952 25.22030 > bootCI(num, alternative="g") Summary of x Min. 1st Qu. Median Mean 3rd Qu. Max. 1.500 2.500 5.000 6.357 7.000 19.000 CIs of mu 5% 100% $CI.exact 1.926445 Inf $CI.basic 2.714286 Inf $CI.percentile 3.285714 Inf $CI.studentized 3.332740 Inf > bootCI(num, B=99) Summary of x Min. 1st Qu. Median Mean 3rd Qu. Max. 1.500 2.500 5.000 6.357 7.000 19.000 CIs of mu 2.5% 97.5% $CI.exact 0.7778728 11.936413 $CI.basic 1.6053571 9.330357 $CI.percentile 3.3839286 11.108929 $CI.studentized 2.5532298 15.532786
The returned list may be helpful for someone:
> myCI <- bootCI(num) > myCI$CI.percentile 2.5% 97.5% 3.00000 10.85714 > str(myCI) List of 8 $ x : num [1:7] 5 2 3 6 8 19 1.5 $ alpha : num 0.05 $ alternative : chr "t" $ B : num 1999 $ CI.exact : Named num [1:2] 0.778 11.936 ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%" $ CI.basic : Named num [1:2] 1.86 9.71 ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%" $ CI.percentile : Named num [1:2] 3 10.9 ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%" $ CI.studentized: Named num [1:2] 2.65 17.73 ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%" - attr(*, "class")= chr "bootCI"
No comments:
Post a Comment