차이의 구간

R로 배우는 범주형 데이터 추론

Andrew Bray

Assistant Professor of Statistics at Reed College

두 변수의 질문

여성과 남성의 신념 비율은 다를까요?

$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()

ch2v2-postlife-barplot.png

R로 배우는 범주형 데이터 추론

여성과 남성은 사후세계에 대해 다른 의견을 갖나요?

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

ch2v2-postlife-barplot-filled.png

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로 배우는 범주형 데이터 추론

H0로부터 데이터 생성

  • $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,  # this line is new
    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")

이 데이터는 성별에 따른 사후세계 신념의 차이를 시사합니다.

ch2v2-density-plot.png

R로 배우는 범주형 데이터 추론

Ayo berlatih!

R로 배우는 범주형 데이터 추론

Preparing Video For Download...