"검정은 하나뿐이다" 프레임워크

R로 하는 가설 검정

Richie Cotton

Data Evangelist at DataCamp

불균형 데이터

stack_overflow_imbalanced %>% 
  count(hobbyist, age_cat, .drop = FALSE)
  hobbyist     age_cat    n
1       No At least 30    0
2       No    Under 30  191
3      Yes At least 30   15
4      Yes    Under 30 1025

일부 그룹이 다른 그룹보다 훨씬 크면 표본이 불균형 상태입니다.

R로 하는 가설 검정

가설

$H_{0}$: 30세 미만 취미 개발자 비율은 30세 이상 비율과 같다.

$H_{A}$: 30세 미만 취미 개발자 비율은 30세 이상 비율과 다르다.

alpha <- 0.1

R로 하는 가설 검정

그럼에도 비율 검정 진행하기

stack_overflow_imbalanced %>% 
  prop_test(
    hobbyist ~ age_cat,
    order = c("At least 30", "Under 30"),
    success = "Yes",
    alternative = "two.sided",
    correct = FALSE
  )
# A tibble: 1 x 6
  statistic chisq_df p_value alternative lower_ci upper_ci
      <dbl>    <dbl>   <dbl> <chr>          <dbl>    <dbl>
1      2.79        1  0.0949 two.sided    0.00718   0.0217
R로 하는 가설 검정

그래픽 문법

플롯 유형 base-R ggplot2
산점도 plot(, type = "p") ggplot() + geom_point()
선 그래프 plot(, type = "l") ggplot() + geom_line()
히스토그램 hist() ggplot() + geom_histogram()
상자 그림 boxplot() ggplot() + geom_boxplot()
막대 그래프 barplot() ggplot() + geom_bar()
원 그래프 pie() ggplot() + geom_bar() + coord_polar()
R로 하는 가설 검정

가설 검정의 문법

  • Allen Downey의 검정은 하나뿐이다 프레임워크.
  • R의 infer 패키지로 구현됨.
  • generate()는 시뮬레이션 데이터를 생성합니다.
    • 계산 비용이 높습니다.
    • 소규모 표본이나 불균형 데이터에 강건합니다.
null_distn <- dataset %>% 
  specify() %>% 
  hypothesize() %>% 
  generate() %>% 
  calculate()
obs_stat <- dataset %>% 
  specify() %>% 
  calculate()
get_p_value(null_distn, obs_stat)
1 Allen Downey는 "Python을 이용한 탐색적 데이터 분석"을 강의합니다.
R로 하는 가설 검정

관심 변수 지정

왼쪽에는 데이터 프레임을 나타내는 셀 격자가 있으며 두 열이 강조 표시되어 있습니다. 격자 오른쪽에 'specify'라는 단어와 오른쪽을 가리키는 화살표가 있고, 그 오른쪽에는 강조된 두 열만 있는 또 다른 격자가 있습니다.

R로 하는 가설 검정

specify()

specify()는 검정할 변수를 선택합니다.

  • 2표본 검정: response ~ explanatory 사용.
  • 1표본 검정: response ~ NULL 사용.
stack_overflow_imbalanced %>%
  specify(hobbyist ~ age_cat, success = "Yes")
Response: hobbyist (factor)
Explanatory: age_cat (factor)
# A tibble: 1,231 x 2
  hobbyist age_cat    
  <fct>    <fct>      
1 Yes      At least 30
2 Yes      At least 30
3 Yes      At least 30
4 Yes      Under 30   
5 Yes      At least 30
6 Yes      At least 30
7 No       Under 30   
# ... with 1,224 more rows
R로 하는 가설 검정

hypothesize()

hypothesize()는 귀무가설의 유형을 선언합니다.

  • 2표본 검정: "independence" 또는 "point" 사용.
  • 1표본 검정: "point" 사용.
stack_overflow_imbalanced %>%
  specify(hobbyist ~ age_cat, success = "Yes") %>%
  hypothesize(null = "independence")
Response: hobbyist (factor)
Explanatory: age_cat (factor)
Null Hypothesis: independence
# A tibble: 1,231 x 2
  hobbyist age_cat    
  <fct>    <fct>      
1 Yes      At least 30
2 Yes      At least 30
3 Yes      At least 30
4 Yes      Under 30   
5 Yes      At least 30
6 Yes      At least 30
7 No       Under 30   
# ... with 1,224 more rows
R로 하는 가설 검정

연습해 봅시다!

R로 하는 가설 검정

Preparing Video For Download...