Intervalos para diferenças

Inferência para Dados Categóricos em R

Andrew Bray

Assistant Professor of Statistics at Reed College

Uma pergunta com duas variáveis

Mulheres e homens acreditam em taxas diferentes?

Seja $p$ a proporção que acredita em vida após a morte.

  • $H_{0} : p_{female} - p_{male} = 0$
  • $H_{A} : p_{female} - p_{male} \ne 0$
Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

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

ch2v2-postlife-barplot.png

Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

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

ch2v2-postlife-barplot-filled.png

Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

p_hats <- gss2016 %>%
  group_by(sex) %>%
  summarize(mean(postlife == "YES", na.rm = TRUE)) %>%
  pull()
d_hat <- diff(p_hats)
d_hat
0.1472851
Inferência para Dados Categóricos em R

Gerando dados a partir de H0

  • $H_{0} : p_{female} - p_{male} = 0$
  • Não há associação entre crença na vida após a morte e o sexo do entrevistado.
  • A variável postlife é independente da variável sex.

⇒ Gerar dados por permutação

Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

gss2016 %>%
  specify(
    response = postlife, 
    explanatory = sex, 
    success = "YES"
  ) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1, type = "permute")
Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

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
Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

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
Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

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.
Inferência para Dados Categóricos em R

Mulheres e homens têm opiniões diferentes sobre vida após a morte?

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

Esses dados sugerem diferença entre os sexos na crença em vida após a morte.

ch2v2-density-plot.png

Inferência para Dados Categóricos em R

Vamos praticar!

Inferência para Dados Categóricos em R

Preparing Video For Download...