Rで何かをしたり、読書をするブログ

政府統計の総合窓口のデータや、OECDやUCIやのデータを使って、Rの練習をしています。ときどき、読書記録も載せています。

OECD Threatened species data analysis 5 - Bootstrap for Cinfidence Interval

 This blog is following of

www.crosshyou.info

 In this blog, I will show you how to get confidence interval with bootstrap method.

for BIRD, 95% confidence interval is 18.1 ~ 25.3 by parametric calculation.

average ± qt(0.975, d.f.)*S.E.

f:id:cross_hyou:20210523080014p:plain

We will calculate it with bootstrap method.

1. make a vector for BIRD.

f:id:cross_hyou:20210523080449p:plain

2. decide how many times calculate average

f:id:cross_hyou:20210523080630p:plain

I set it 100,000 times.

3. make a vector to stroe averages

f:id:cross_hyou:20210523080909p:plain

4. make a function to draw random samples and calculate averages.

f:id:cross_hyou:20210523081415p:plain

let's check if the function works well.

f:id:cross_hyou:20210523081705p:plain

we see 1st, 2nd and 3rd are different value, it means the function works fine.

5. use for() function to makes averages.

f:id:cross_hyou:20210523082150p:plain

6. use quantile() to get confidence interval

f:id:cross_hyou:20210523082433p:plain

That's it.

Let's make a histogram for results and vertical lines for confidence intervals.

f:id:cross_hyou:20210523083209p:plain

f:id:cross_hyou:20210523083250p:plain

The red vertical line is average, 

the blue vertical lines are confidence interval by parametric and 

the green vertical lines are confidence interval by bootstrap.

Thank you.

 

To read the 1st blog,

 

www.crosshyou.info