How to find bootstrap confidence interval in R?

R ProgrammingServer Side ProgrammingProgramming

The bootstrap confidence interval can be found by using the boot function. The bootstrapping is a method of finding inferential statistics with the help of sample data. It is done by drawing a large number of samples with replacement from the same values. Check out the Examples given below to understand how we can create a bootstrap confidence interval.

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(50,2,0.25)
x1

The following dataframe is created

[1] 2.022942 2.057100 1.659310 1.697694 2.160157 1.864619 1.984351 2.031657
[9] 1.660948 1.943491 1.891821 2.296657 1.308199 1.985485 1.884744 2.409612
[17] 2.232219 1.818459 2.062774 1.747950 2.023910 2.481306 1.640285 1.774226
[25] 1.785806 1.649945 2.362140 2.054361 1.913593 1.841699 2.289838 2.386582
[33] 1.569098 2.336435 2.365340 1.630608 2.147830 2.185923 2.026811 2.055732
[41] 2.106160 1.833184 2.334308 1.796523 1.734141 2.118171 1.932267 1.558501
[49] 1.936617 2.244815

To find the bootstrap confidence interval in R on the above created data frame, add the following code to the above snippet −

x1<-rnorm(50,2,0.25)
b1<-boot(x1,function(u,i) mean(u[i]),R=1000)
boot.ci(b1,type=c("norm","basic","perc"))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL :
boot.ci(boot.out = b1, type = c("norm", "basic", "perc"))

Intervals :
Level     Normal           Basic         Percentile
95%  ( 1.902, 2.049 ) ( 1.901, 2.048 ) ( 1.906, 2.052 )
Calculations and Intervals on Original Scale

Example 2

Following snippet creates a sample data frame −

x2<-rpois(200,10)
x2

The following dataframe is created

[1] 12 7 15 13 11 8 7 9 11 10 3 10 14 12 12 6 4 13 17 8 6 9 12 10 15
[26] 9 8 18 11 10 5 12 8 7 11 11 5 13 17 8 17 9 5 5 10 6 11 10 9
14
[51] 16 14 7 10 8 9 6 11 15 7 12 12 12 12 10 12 10 8 8 11 11 11 9 12
12
[76] 3 12 6 7 19 12 12 15 6 13 11 10 6 12 11 9 13 9 13 10 6 11 11 15
8
[101] 10 6 11 10 7 18 11 15 9 6 12 4 9 10 12 13 13 14 13 9 9 5 12 10
19
[126] 9 11 12 11 7 12 7 8 14 10 14 13 12 5 10 17 11 9 8 8 10 15 14 11
13
[151] 7 5 10 12 8 11 16 6 9 16 10 11 13 10 8 17 14 10 10 4 10 7 15 11
9
[176] 16 9 9 4 11 10 11 5 4 9 16 12 15 15 9 10 10 11 9 12 6 15 9 8
11

To find the bootstrap confidence interval in R on the above created data frame, add the following code to the above snippet −

x2<-rpois(200,10)
b2<-boot(x2,function(u,i) mean(u[i]),R=1000)
boot.ci(b2,type=c("basic","perc"))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL :
boot.ci(boot.out = b2, type = c("basic", "perc"))

Intervals :
Level      Basic        Percentile
95%  ( 9.91, 10.87 ) ( 9.94, 10.90 )
Calculations and Intervals on Original Scale

Example 3

Following snippet creates a sample data frame −

x3<-sample(0:9,200,replace=TRUE)
x3

The following dataframe is created

[1] 7 4 3 5 9 4 8 8 1 8 4 8 8 0 4 9 4 5 1 8 2 7 4 8 0 9 5 6 6 3 6 8 7 9 4 3 0
[38] 3 6 9 9 4 4 3 2 2 5 7 7 7 9 1 7 3 8 5 8 2 8 7 2 5 2 2 1 7 1 6 1 3 6 7 5 6
[75] 6 9 1 0 4 0 7 8 2 7 0 3 3 6 5 9 6 6 9 1 4 3 2 9 7 3 4 3 2 5 4 4 0 0 0 4 6
[112] 3 3 3 4 2 4 1 3 4 8 4 3 3 9 1 2 4 1 0 2 3 0 9 4 3 7 9 6 5 1 7 2 8 9 1 4 6
[149] 1 0 9 8 5 0 3 7 5 8 0 3 5 6 9 1 4 7 6 8 9 2 1 4 4 0 7 0 1 2 4 5 6 5 3 0 9
[186] 3 4 9 3 7 9 6 8 6 0 1 8 9 3 6

To find the bootstrap confidence interval in R on the above created data frame, add the following code to the above snippet −

x3<-sample(0:9,200,replace=TRUE)
b3<-boot(x3,function(u,i) mean(u[i]),R=1000)
boot.ci(b3,type=c("norm","basic","perc"))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL :
boot.ci(boot.out = b3, type = c("norm", "basic", "perc"))

Intervals :
Level     Normal            Basic         Percentile
95%  ( 4.187, 4.964 ) ( 4.190, 4.980 ) ( 4.160, 4.950 )
Calculations and Intervals on Original Scale

Example 4

Following snippet creates a sample data frame −

x4<-sample(10:99,200,replace=TRUE)
x4

The following dataframe is created

[1] 52 64 59 25 55 75 25 29 37 84 80 21 64 48 15 93 49 66 10 90 60 38 28 25 12
[26] 19 78 63 81 50 19 58 63 11 86 70 26 36 75 95 54 49 37 69 90 28 18 96 41
92
[51] 10 98 93 69 64 75 38 60 85 34 90 62 58 11 53 54 27 52 95 34 37 78 44 24
82
[76] 31 43 95 86 36 86 98 49 52 90 43 41 45 89 24 74 94 75 32 39 59 56 83 44
12
[101] 73 87 31 27 39 19 41 44 17 81 17 15 23 49 47 77 46 35 23 12 75 72 80 13
38
[126] 44 45 34 94 41 47 91 76 79 66 23 15 35 38 38 60 91 17 11 21 49 97 38 64
25
[151] 83 91 12 14 81 34 58 45 32 82 58 47 41 78 96 17 59 23 50 91 61 18 16 12
56
[176] 88 99 16 99 21 47 50 45 18 14 73 44 97 28 71 34 79 93 64 28 44 87 60 70
93

To find the bootstrap confidence interval in R on the above created data frame, add the following code to the above snippet −

x4<-sample(10:99,200,replace=TRUE)
b4<-boot(x4,function(u,i) sd(u[i]),R=1000)
boot.ci(b4,type=c("norm","basic","perc"))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL :
boot.ci(boot.out = b4, type = c("norm", "basic", "perc"))

Intervals :
Level    Normal           Basic        Percentile
95%  (24.94, 28.27 ) (24.99, 28.37 ) (24.67, 28.06 )
Calculations and Intervals on Original Scale

Example 5

Following snippet creates a sample data frame −

x5<-sample(101:999,200)
x5

The following dataframe is created

[1]   983 360 354 222 143 630 172 932 736 302 600 408 805 364 660 634 500 299
[19]  944 645 433 551 780 818 524 301 686 309 974 998 133 696 421 769 194 935
[37]  358 515 549 741 862 316 589 411 674 561 777 971 329 684 854 774 477 821
[55]  590 274 277 315 300 988 443 570 214 525 961 594 826 184 435 933 410 853
[73]  190 792 322 940 285 544 756 822 384 902 924 244 401 154 187 723 664 975
[91]  673 623 955 533 351 832 216 581 295 450 682 478 294 706 937 905 815 254
[109] 758 748 828 996 174 157 993 912 900 797 406 574 632 248 743 102 156 121
[127] 226 710 681 768 296 568 665 234 896 388 995 693 958 677 656 291 655 899
[145] 739 980 889 610 637 556 502 814 855 136 967 880 970 647 365 499 856 196
[163] 459 334 189 341 186 965 580 417 890 418 142 692 566 936 622 569 403 667
[181] 247 112 183 150 376 901 413 192 466 123 552 816 875 338 437 148 448 261
[199] 669 373

To find the bootstrap confidence interval in R on the above created data frame, add the following code to the above snippet −

x5<-sample(101:999,200)
x5
b5<-boot(x5,function(u,i) median(u[i]),R=1000)
boot.ci(b5,type=c("norm","basic","perc"))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL :
boot.ci(boot.out = b5, type = c("norm", "basic", "perc"))

Intervals :
Level    Normal           Basic        Percentile
95%  (508.7, 625.8 ) (507.0, 629.0 ) (515.0, 637.0 )
Calculations and Intervals on Original Scale
raja
Updated on 03-Nov-2021 06:03:48

Advertisements