WHoZ

Dataset mpg - Simulação - Testes de Hipóteses

Feb 5th, 2020
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.44 KB | None | 0 0
  1. # ##############################################################################
  2. # ########################### Autonomia de Veículos ############################
  3. # ##############################################################################
  4.  
  5. # 1. Carregamento de pacotes ---------------------------------------------------
  6.  
  7. ## Carregando
  8. if(!require("pacman")) install.packages("pacman"); library(pacman)
  9.  
  10. p_load("tidyverse", "tidymodels")
  11.  
  12. # 2. Preparação do ambiente e dos dados ----------------------------------------
  13.  
  14. ## Limpa o console o ambiente
  15. cat("\014")
  16. rm(list = ls())
  17.  
  18. ## Ajuda para o pacote 'infer'
  19. help(package = "infer")
  20. vignette("infer")
  21.  
  22. mpg_ <- mpg %>%
  23.   mutate(trans = ifelse(str_detect(trans, "auto"), "auto", "manual"),
  24.          trans = factor(trans),
  25.          class = factor(class))
  26.  
  27. # A quilometragem/litro média é 23 ? -------------------------------------------
  28.  
  29. bootstrap_distribution <- mpg_ %>%
  30.   specify(response = hwy) %>%
  31.   hypothesize(null = "point",
  32.               mu = 23) %>%
  33.   generate(reps = 10^3,
  34.            type = "bootstrap") %>%
  35.   calculate(stat = "mean")
  36.  
  37. obs_mean <- mpg_ %>%
  38.   specify(response = hwy) %>%
  39.   calculate(stat = "mean")
  40.  
  41. visualize(bootstrap_distribution)+
  42.   shade_p_value(obs_stat = obs_mean,
  43.                 direction = "two_sided")
  44.  
  45. bootstrap_distribution %>%
  46.   get_p_value(obs_stat = obs_mean,
  47.               direction = "two_sided")
  48.  
  49. # p-valor: 0.283 / não rejeitar a hipótese nula (a média é 23)
  50.  
  51. # Carros automáticos e manuais tem a mesma autonomia? --------------------------
  52.  
  53. null_distribution <- mpg_ %>%
  54.   specify(hwy ~ trans) %>%
  55.   hypothesize(null = "independence") %>%
  56.   generate(reps = 10^3, type = "permute") %>%
  57.   calculate(stat = "diff in means",
  58.             order = c("manual", "auto"))
  59.  
  60. obs_mean_diff <- mpg_ %>%
  61.   specify(hwy ~ trans) %>%
  62.   calculate(stat = "diff in means",
  63.             order = c("manual", "auto"))
  64.  
  65. visualize(null_distribution)+
  66.   shade_p_value(obs_stat = obs_mean_diff,
  67.                 direction = "two_sided")
  68.  
  69. null_distribution %>%
  70.   get_p_value(obs_stat = obs_mean_diff,
  71.               direction = "two_sided")
  72.  
  73. # p-valor: 0 / rejeitar a hipótese nula
  74.  
  75. # O tipo de transmissão influencia no gasto de combustível? --------------------
  76.  
  77. null_distribution <- mpg_ %>%
  78.   specify(class ~ trans) %>%
  79.   hypothesize(null = "independence") %>%
  80.   generate(reps = 10^4, type = "permute") %>%
  81.   calculate(stat = "Chisq")
  82.  
  83. obs_chisq <- mpg_ %>%
  84.   specify(class ~ trans) %>%
  85.   calculate(stat = "Chisq")
  86.  
  87. visualize(null_distribution)+
  88.   shade_p_value(obs_stat = obs_chisq,
  89.                 direction = "greater")
  90.  
  91. null_distribution %>%
  92.   get_p_value(obs_stat = obs_chisq,
  93.               direction = "greater")
  94.  
  95. # p-valor: 0 / rejeitar a hipótese nula
  96.  
  97. # 3. Plataforma de testes ------------------------------------------------------
  98.  
  99. ## Função densidade
  100. dens.fun <- function(z) with(density(null_distribution$stat), approx(x, y, z)$y)
  101.  
  102. x_start <- quantile(null_distribution$stat, probs = 0.95, names = FALSE)
  103. x_end <- max(null_distribution$stat)
  104.  
  105. ## Colorir área sob a curva de densidade (p-valor)
  106. ggplot(null_distribution, aes(x = stat))+
  107.   geom_density()+
  108.   geom_area(data = data.frame(value = seq(x_start, x_end, len=100)),
  109.             aes(x = value, y = dens.fun(value), color = NULL),
  110.             fill = "red", alpha = 0.3)
  111.  
  112. # ------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment