仮説検定とzスコア

Rによる仮説検定

Richie Cotton

Data Evangelist at DataCamp

A/Bテスト

  • Electronic Arts(EA)はゲーム会社。
  • 2013年にSimCity 5を発売。
  • 目標は予約購入の増加。
  • 広告パターンをA/Bテストで検証。
  • これはユーザーを「対照群」と「処置群」に分ける方法。

Electronic Artsの建物

1 画像クレジット: "Electronic Arts" by majaX1 CC BY-NC-SA 2.0
Rによる仮説検定

小売WebページのA/Bテスト

対照群

「予約購入で次回$20割引」と書かれたバナー付きのSimCityのWebページ

処置群

バナーのないSimCityのWebページ

Rによる仮説検定

A/Bテストの結果

  • 処置群(広告なし)は、対照群(広告あり)より購入が43.4%多かった。
  • 「広告を出せば売上が上がる」という直感は誤りだった。
  • この結果は統計的に有意か、偶然か。
  • 判定にはEAのデータが必要。
  • 「Sampling in R」と本講座の手法で検証可能。
Rによる仮説検定

Stack Overflow 開発者調査 2020

library(dplyr)
glimpse(stack_overflow)
Rows: 2,261
Columns: 8
$ respondent         <dbl> 36, 47, 69, 125, 147, 152, 166, 170, 187, 196, 221,…
$ age_first_code_cut <chr> "adult", "child", "child", "adult", "adult", "adult…
$ converted_comp     <dbl> 77556, 74970, 594539, 2000000, 37816, 121980, 48644…
$ job_sat            <fct> Slightly satisfied, Very satisfied, Very satisfied,…
$ purple_link        <chr> "Hello, old friend", "Hello, old friend", "Hello, o…
$ age_cat            <chr> "At least 30", "At least 30", "Under 30", "At least…
$ age                <dbl> 34, 53, 25, 41, 28, 30, 28, 26, 43, 23, 24, 35, 37,…
$ hobbyist           <chr> "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Ye…
Rによる仮説検定

平均に関する仮説設定

仮説:

データサイエンティスト集団の平均年収は$110,000である。

点推定(標本統計量):

mean_comp_samp <- mean(stack_overflow$converted_comp)
mean_comp_samp <- stack_overflow %>% 
  summarize(mean_compensation = mean(converted_comp)) %>% 
  pull(mean_compensation)
119574.7
Rによる仮説検定

ブートストラップ分布の生成

# 手順3: 手順1と2を多数回繰り返す
so_boot_distn <- replicate(
  n = 5000,
  expr = {
    # 手順1: 再標本化
    stack_overflow %>%
      slice_sample(prop = 1, replace = TRUE) %>%
      # 手順2: 点推定を計算
      summarize(mean_compensation = mean(converted_comp)) %>% 
      pull(mean_compensation)
  }
)
1 ブートストラップ分布は「Sampling in R」第4章で扱います
Rによる仮説検定

ブートストラップ分布の可視化

tibble(resample_mean = so_boot_distn) %>%
  ggplot(aes(resample_mean)) +
  geom_histogram(binwidth = 1000)

ブートストラップ分布のヒストグラム ― 釣鐘型で、およそ110000〜140000の範囲

Rによる仮説検定

標準誤差

std_error <- sd(so_boot_distn)
5511.674
Rによる仮説検定

zスコア

$\text{標準化値} = \dfrac{\text{値} - \text{平均}}{\text{標準偏差}}$

$z = \dfrac{\text{標本統計量} - \text{仮定した母数}}{\text{標準誤差}}$

$z = \dfrac{\$119,574.7 - \$110,000}{\$5511.67} = 1.737$

mean_comp_samp
119574.7
mean_comp_hyp <- 110000
std_error
5511.674
z_score <- (mean_comp_samp - mean_comp_hyp) / std_error
1.737171
Rによる仮説検定

仮説の検定

  • 1.737171は高いか低いか。
  • これを学ぶのが本講座の目的です。
仮説検定の用途:

標本統計量が期待(仮説)値に近いか遠いかを判断する。

Rによる仮説検定

標準正規(z)分布

標準正規分布: 平均0、標準偏差1の正規分布。

tibble(x = seq(-4, 4, 0.01)) %>% 
  ggplot(aes(x)) +
  stat_function(fun = dnorm) +
  ylab("PDF(x)")

標準正規分布のPDFの密度プロット

Rによる仮説検定

Let's practice!

Rによる仮説検定

Preparing Video For Download...