ブートストラップ入門

Rで学ぶサンプリング

Richie Cotton

Data Evangelist at DataCamp

有りか無しか

非復元抽出

カジノのテーブル上のトランプ。

復元抽出(「再標本化」)

4つのサイコロ。

Rで学ぶサンプリング

非復元の単純無作為抽出

母集団

行と列に並んだコーヒー豆。

標本

行と列に並んだコーヒー豆。多くがグレーアウト。

Rで学ぶサンプリング

復元の単純無作為抽出

母集団

行と列に並んだコーヒー豆。

標本

重複を含む無作為抽出のコーヒー豆。

Rで学ぶサンプリング

なぜ復元抽出か?

  • coffee_ratings は全コーヒー母集団からの標本と考える。
  • 標本内の各コーヒーは、母集団にいる未観測の多くのコーヒーを代表すると考える。
  • 復元抽出は、これらのグループの異なるメンバーを標本に含める代替手段。
Rで学ぶサンプリング

コーヒーデータの準備

coffee_focus <- coffee_ratings %>%
  select(variety, country_of_origin, flavor) %>%
  rowid_to_column()
glimpse(coffee_focus)
Rows: 1,338
Columns: 4
$ rowid             <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ...
$ variety           <chr> NA, "Other", "Bourbon", NA, "Other", NA, "Other", N...
$ country_of_origin <chr> "Ethiopia", "Ethiopia", "Guatemala", "Ethiopia", "E...
$ flavor            <dbl> 8.83, 8.67, 8.50, 8.58, 8.50, 8.42, 8.50, 8.33, 8.6...
Rで学ぶサンプリング

slice_sample() による再標本化

coffee_resamp <- coffee_focus %>%
  slice_sample(prop = 1, replace = TRUE)
# A tibble: 1,338 x 4
   rowid variety country_of_origin flavor
   <int> <chr>   <chr>              <dbl>
 1  1253 Bourbon Guatemala           6.92
 2   186 Caturra Colombia            7.58
 3  1185 Bourbon Guatemala           7.42
 4  1273 NA      Philippines         6.5 
 5  1042 Caturra Honduras            7.33
 6   195 Caturra Guatemala           7.75
 7  1219 Typica  Mexico              7   
 8   952 Caturra Honduras            7.5 
 9    41 Caturra Thailand            8.33
10   460 Caturra Honduras            7.67
# ... with 1,328 more rows
Rで学ぶサンプリング

重複したコーヒー

coffee_resamp %>% 
  count(rowid, sort = TRUE)
# A tibble: 844 x 2
   rowid     n
   <int> <int>
 1   704     5
 2   913     5
 3  1070     5
 4    16     4
 5   180     4
 6   230     4
 7   234     4
 8   342     4
 9   354     4
10   423     4
# ... with 834 more rows
Rで学ぶサンプリング

含まれないコーヒー

coffee_resamp %>% 
  summarize(
    coffees_included = n_distinct(rowid),
    coffees_not_included = n() - coffees_included
  )
# A tibble: 1 x 2
  coffees_included coffees_not_included
             <int>                <int>
1              844                  494
Rで学ぶサンプリング

ブートストラップ

母集団からのサンプリングの逆。

・サンプリング:母集団から小さい標本へ。

・ブートストラップ:標本から理論上の母集団を構築。

ブートストラップの用途

  • 単一標本からサンプリングばらつきを理解する。

カウボーイブーツ。

Rで学ぶサンプリング

ブートストラップの手順

  1. 元の標本と同じサイズで再標本化する。
  2. そのブートストラップ標本で関心の統計量を計算する。
  3. 1と2を多数回繰り返す。

得られた統計量は「ブートストラップ統計量」、そのばらつきを見る分布は「ブートストラップ分布」。

Rで学ぶサンプリング

コーヒーの平均風味のブートストラップ

# Step 3. Repeat many times
mean_flavors_1000 <- replicate(
  n = 1000,
  expr = {
    coffee_focus %>%
      # Step 1. Resample
      slice_sample(prop = 1, replace = TRUE) %>%
      # Step 2. Calculate statistic
      summarize(mean_flavor = mean(flavor, na.rm = TRUE)) %>% 
      pull(mean_flavor)
  })
Rで学ぶサンプリング

ブートストラップ分布のヒストグラム

bootstrap_distn <- tibble(
  resample_mean = mean_flavors_1000
)
ggplot(bootstrap_distn, aes(resample_mean)) +
  geom_histogram(binwidth = 0.0025)

標本平均のブートストラップ分布のヒストグラム。

Rで学ぶサンプリング

練習しましょう!

Rで学ぶサンプリング

Preparing Video For Download...