Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ##############################################################################
- # ########################### Autonomia de Veículos ############################
- # ##############################################################################
- # 1. Carregamento de pacotes ---------------------------------------------------
- ## Carregando
- if(!require("pacman")) install.packages("pacman"); library(pacman)
- p_load("tidyverse", "tidymodels")
- # 2. Preparação do ambiente e dos dados ----------------------------------------
- ## Limpa o console o ambiente
- cat("\014")
- rm(list = ls())
- ## Ajuda para o pacote 'infer'
- help(package = "infer")
- vignette("infer")
- mpg_ <- mpg %>%
- mutate(trans = ifelse(str_detect(trans, "auto"), "auto", "manual"),
- trans = factor(trans),
- class = factor(class))
- # A quilometragem/litro média é 23 ? -------------------------------------------
- bootstrap_distribution <- mpg_ %>%
- specify(response = hwy) %>%
- hypothesize(null = "point",
- mu = 23) %>%
- generate(reps = 10^3,
- type = "bootstrap") %>%
- calculate(stat = "mean")
- obs_mean <- mpg_ %>%
- specify(response = hwy) %>%
- calculate(stat = "mean")
- visualize(bootstrap_distribution)+
- shade_p_value(obs_stat = obs_mean,
- direction = "two_sided")
- bootstrap_distribution %>%
- get_p_value(obs_stat = obs_mean,
- direction = "two_sided")
- # p-valor: 0.283 / não rejeitar a hipótese nula (a média é 23)
- # Carros automáticos e manuais tem a mesma autonomia? --------------------------
- null_distribution <- mpg_ %>%
- specify(hwy ~ trans) %>%
- hypothesize(null = "independence") %>%
- generate(reps = 10^3, type = "permute") %>%
- calculate(stat = "diff in means",
- order = c("manual", "auto"))
- obs_mean_diff <- mpg_ %>%
- specify(hwy ~ trans) %>%
- calculate(stat = "diff in means",
- order = c("manual", "auto"))
- visualize(null_distribution)+
- shade_p_value(obs_stat = obs_mean_diff,
- direction = "two_sided")
- null_distribution %>%
- get_p_value(obs_stat = obs_mean_diff,
- direction = "two_sided")
- # p-valor: 0 / rejeitar a hipótese nula
- # O tipo de transmissão influencia no gasto de combustível? --------------------
- null_distribution <- mpg_ %>%
- specify(class ~ trans) %>%
- hypothesize(null = "independence") %>%
- generate(reps = 10^4, type = "permute") %>%
- calculate(stat = "Chisq")
- obs_chisq <- mpg_ %>%
- specify(class ~ trans) %>%
- calculate(stat = "Chisq")
- visualize(null_distribution)+
- shade_p_value(obs_stat = obs_chisq,
- direction = "greater")
- null_distribution %>%
- get_p_value(obs_stat = obs_chisq,
- direction = "greater")
- # p-valor: 0 / rejeitar a hipótese nula
- # 3. Plataforma de testes ------------------------------------------------------
- ## Função densidade
- dens.fun <- function(z) with(density(null_distribution$stat), approx(x, y, z)$y)
- x_start <- quantile(null_distribution$stat, probs = 0.95, names = FALSE)
- x_end <- max(null_distribution$stat)
- ## Colorir área sob a curva de densidade (p-valor)
- ggplot(null_distribution, aes(x = stat))+
- geom_density()+
- geom_area(data = data.frame(value = seq(x_start, x_end, len=100)),
- aes(x = value, y = dens.fun(value), color = NULL),
- fill = "red", alpha = 0.3)
- # ------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment