差の区間

R によるカテゴリカルデータの推測

Andrew Bray

Assistant Professor of Statistics at Reed College

2変数の問い

男女で信じる割合は異なるか?

$p$ を死後の世界を信じる割合とする。

  • $H_{0} : p_{female} - p_{male} = 0$
  • $H_{A} : p_{female} - p_{male} \ne 0$
R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

ggplot(gss2016, aes(x = sex, fill = postlife)) +
  geom_bar()

棒グラフ:postlife

R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

ggplot(gss2016, aes(x = sex, fill = postlife)) +
  geom_bar(position = "fill")

積み上げ比率棒グラフ:postlife

R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

p_hats <- gss2016 %>%
  group_by(sex) %>%
  summarize(mean(postlife == "YES", na.rm = TRUE)) %>%
  pull()
d_hat <- diff(p_hats)
d_hat
0.1472851
R によるカテゴリカルデータの推測

帰無仮説からのデータ生成

  • $H_{0} : p_{female} - p_{male} = 0$
  • 来世の信念と性別に関連はない。
  • 変数 postlife は変数 sex と独立。

置換でデータを生成

R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

gss2016 %>%
  specify(
    response = postlife, 
    explanatory = sex, 
    success = "YES"
  ) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1, type = "permute")
R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

gss2016 %>%
  specify(
    postlife ~ sex,  # この行が新規
    success = "YES"
  ) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1, type = "permute")
Response: postlife (factor)
Explanatory: sex (factor)
Null Hypothesis:  independence 
# A tibble: 137 x 3
# Groups:   replicate [1]
   postlife sex    replicate
   <fct>    <fct>      <int>
 1 YES      FEMALE         1
 2 YES      MALE           1
 3 YES      FEMALE         1
 4 YES      MALE           1
 5 YES      MALE           1
 6 YES      FEMALE         1
 7 NO       FEMALE         1
R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

gss2016 %>%
  specify(
    postlife ~ sex, 
    success = "YES"
  ) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1, type = "permute")
Response: postlife (factor)
Explanatory: sex (factor)
Null Hypothesis:  independence 
# A tibble: 137 x 3
# Groups:   replicate [1]
   postlife sex    replicate
   <fct>    <fct>      <int>
 1 YES      FEMALE         1
 2 NO       MALE           1
 3 NO       FEMALE         1
 4 YES      MALE           1
 5 YES      MALE           1
 6 YES      FEMALE         1
 7 YES      FEMALE         1
R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

gss2016 %>%
  specify(postlife ~ sex, success = "YES") %>%
  hypothesize(null = "independence") %>%
  generate(reps = 500, type = "permute") %>%
  calculate(stat = "diff in props", order = c("FEMALE", "MALE"))
Warning message:
Removed 13 rows containing missing values.
R によるカテゴリカルデータの推測

男女で死後の世界観は異なるか?

ggplot(null, aes(x = stat)) +
  geom_density() +
  geom_vline(xintercept = d_hat, color = "red")

このデータは、死後の世界を巡る信念に性差があることを示唆します。

密度プロット

R によるカテゴリカルデータの推測

練習しましょう!

R によるカテゴリカルデータの推測

Preparing Video For Download...