Cadrul „There is only one test"

Testarea ipotezelor în R

Richie Cotton

Data Evangelist at DataCamp

Date dezechilibrate

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

Un eșantion este dezechilibrat dacă unele grupuri sunt mult mai mari decât altele.

Testarea ipotezelor în R

Ipoteze

$H_{0}$: Proporția pasionaților sub 30 de ani este egală cu proporția pasionaților de cel puțin 30 de ani.

$H_{A}$: Proporția pasionaților sub 30 de ani este diferită de proporția pasionaților de cel puțin 30 de ani.

alpha <- 0.1

Testarea ipotezelor în R

Aplicarea testului de proporții în ciuda dezechilibrului

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
Testarea ipotezelor în R

O gramatică a graficelor

Tip grafic base-R ggplot2
Scatter plot plot(, type = "p") ggplot() + geom_point()
Line plot plot(, type = "l") ggplot() + geom_line()
Histogramă hist() ggplot() + geom_histogram()
Box plot boxplot() ggplot() + geom_boxplot()
Bar plot barplot() ggplot() + geom_bar()
Pie plot pie() ggplot() + geom_bar() + coord_polar()
Testarea ipotezelor în R

O gramatică a testelor de ipoteză

  • Cadrul There is only one test al lui Allen Downey.
  • Implementat în R în pachetul infer.
  • generate() creează date simulate.
    • Costisitor computațional.
    • Robust față de eșantioane mici sau date dezechilibrate.
null_distn <- dataset %>% 
  specify() %>% 
  hypothesize() %>% 
  generate() %>% 
  calculate()
obs_stat <- dataset %>% 
  specify() %>% 
  calculate()
get_p_value(null_distn, obs_stat)
1 Allen Downey predă „Exploratory Data Analysis in Python".
Testarea ipotezelor în R

Specificarea variabilelor de interes

În stânga se află o grilă dreptunghiulară de celule reprezentând un cadru de date. Două coloane sunt evidențiate. În dreapta grilei apare cuvântul „specify" cu o săgeată spre dreapta, lângă o altă grilă care conține doar cele două coloane evidențiate.

Testarea ipotezelor în R

specify()

specify() selectează variabila (variabilele) de testat.

  • Pentru teste cu 2 eșantioane, utilizați response ~ explanatory.
  • Pentru teste cu 1 eșantion, utilizați 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
Testarea ipotezelor în R

hypothesize()

hypothesize() declară tipul ipotezei nule.

  • Pentru teste cu 2 eșantioane, utilizați "independence" sau "point".
  • Pentru teste cu 1 eșantion, utilizați "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
Testarea ipotezelor în R

Să exersăm!

Testarea ipotezelor în R

Preparing Video For Download...