generated from curso-r/template-pagina-do-curso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
# Na ultima eleição a candidata recebeu 53% dos votos | ||
|
||
# 4 anos depois, fizemos uma pesquisa e em uma amostra de tamanho 10000 | ||
# ela recebeu 51% das intenções de voto. Essa queda | ||
# é motivo pra preocupação ou pode ser um "acaso" | ||
|
||
prop.test( | ||
n = 10000, | ||
x = 5100, | ||
p = 0.53) | ||
|
||
x_barra = 5100/10000 | ||
desv_pad_x_barra = sqrt(x_barra*(1-x_barra))/sqrt(10000) | ||
|
||
# valor p deu muito pequeno, o que quer dizer que não tinha muito espaço | ||
# pra ser pior o resultado do que foi. é motivo pra se preocupar o 53 caiu sim... | ||
|
||
library(infer) | ||
library(tidyverse) | ||
|
||
tabela_exemplo <- tibble( | ||
voto = c(rep("candidata A", 5100), rep("candidato B", 4900)) | ||
) | ||
|
||
prop_test( | ||
tabela_exemplo, | ||
voto ~ NULL, | ||
p = 0.53 | ||
) | ||
|
||
dados_brutos <- tabela_exemplo |> | ||
specify(response = voto, success = "candidata A") | ||
|
||
sampling_dist <- tabela_exemplo |> | ||
specify(response = voto, success = "candidata A") |> | ||
assume("z") | ||
|
||
estatistica_do_teste <- dados_brutos |> | ||
hypothesise(null = "point", p = 0.52) |> | ||
calculate(stat = "z") | ||
|
||
sampling_dist |> | ||
visualise() + | ||
shade_p_value(estatistica_do_teste, direction = "left") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
library(tidyverse) | ||
|
||
dados <- readxl::read_excel("exemplos_de_aula//BD_CIS0684.xlsx") | ||
|
||
dados |> | ||
filter( | ||
sexo == "Feminino" | ||
) |> | ||
t_test(idade1 ~ NULL, mu = 41) | ||
|
||
# teste de comparacao de grupos ------------------------------------------- | ||
|
||
# H_0: os homens e mulheres tem a mesma idade | ||
# M, H. M-H = 0 | ||
|
||
# media amostral das mulheres - media amostral dos homens | ||
# eu faco medias dos desvio padrao das populações e | ||
# minha "estatistica" fica: | ||
|
||
# (media amostral das mulheres - media amostral dos homens) | ||
# / (sobre) | ||
# raiz(desvio padrao das mulheres + desvio padrao dos homens)/raiz(2) | ||
|
||
dados |> | ||
t_test(idade1 ~ sexo, alternative = "less") | ||
|
||
# nao temos evidência para rejeitar a hipotese de que as idades sao iguais | ||
|
||
dados |> | ||
prop_test(p32c ~ sexo, success = "Sim") | ||
|
||
dados |> | ||
t_test(p32c ~ sexo) | ||
# isso aqui dá erro porque eu preciso de um desvio padrao no teste t, que é estimado | ||
|
||
# fazendo graficos -------------------------------------------------------- | ||
|
||
dados_brutos <- dados |> | ||
group_by(sexo) |> | ||
sample_n(4) |> | ||
specify(idade1 ~ sexo) | ||
|
||
estatistica <- dados_brutos |> | ||
calculate(stat = "t") | ||
|
||
dados_brutos |> | ||
assume("t") |> | ||
visualise() + | ||
shade_p_value(estatistica, direction = "right") | ||
|
||
|
||
# ------------------------------------------------------------------------- | ||
|
||
# Teste chi quadrado de independência | ||
|
||
dados_brutos <- dados |> | ||
filter(idade1 < 45) |> | ||
specify(p32c ~ idade, success = "Sim") |> | ||
hypothesise(null = "independence") | ||
|
||
dados |> count(idade) | ||
|
||
dados |> | ||
count(idade, p32c) |> | ||
spread(p32c, n) | ||
|
||
glm(factor(p32c) ~ idade, data = dados, family = "binomial") |> | ||
summary() | ||
|
||
estatistica_chi_quadrado <- dados_brutos |> | ||
calculate(stat = "Chisq") | ||
|
||
dados_brutos |> | ||
assume("Chisq") |> | ||
visualise() + | ||
shade_p_value(estatistica_chi_quadrado, direction = "right") | ||
|
||
dados |> | ||
prop_test(p32c ~ idade, success = "Sim") | ||
|
||
chisq_test(dados, p32c ~ idade) | ||
|
||
# mais testes ------------------------------------------------------------- | ||
|
||
# prop_test | ||
# sempre vai ser para comparar proporcoes entre grupos ou proporcoes | ||
# em proporcoes de uma coluna com um valor fixo | ||
|
||
# t_test | ||
# sempre vai ser para comparar valores numeros entr grupos ou | ||
# a media/sd/mediana de uma coluna com um valor fixo | ||
|
||
# pra outros casos: | ||
# hypothetize pra definir a hipótese nula + | ||
# assume pra definir a estatistica (algumas hipoteses só funcionam com certas 'assume') + | ||
# calculate da estatistica e plota o gráfico | ||
|
||
|
||
dados |> | ||
specify(p32c ~ idade*sexo, success = "Sim") |> | ||
hypothesise(null = "independence") | ||
|
||
dados |> | ||
prop_test(p32c ~ NULL, z = 0, p = 0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: "Untitled" | ||
output: html_document | ||
date: "2024-09-23" | ||
--- | ||
|
||
$$\text{Percentual da pesquisa} \sim \text{(dist amostral)} Normal\left(p, \sqrt{\frac{p(1-p)}{n}}\right)$$ | ||
O intervalo sempre vai ser: | ||
$$\left[p - 2\sqrt{\frac{p(1-p)}{n}}, p + 2\sqrt{\frac{p(1-p)}{n}}\right]$$ | ||
Como p*(1-p) é menor que 1/4 sempre o MAIOR intervalo possivel, pra qualquer p (um intervalo que vai conter todos os outros) é: | ||
|
||
$$\left[\bar{x} - 2\sqrt{\frac{1/4}{n}}, \bar{x} + 2\sqrt{\frac{1/4}{n}}\right] = \left[\bar{x} - 2\frac{1/2}{\sqrt{n}}, \bar{x} + 2\frac{1/2}{\sqrt{n}}\right] = $$ | ||
|
||
$$\left[\bar{x}-\frac{1}{\sqrt{n}}, \bar{x}+\frac{1}{\sqrt{n}}\right]$$ | ||
|
||
Digamos que eu queira que o "erro" do intervalo, pra mais ou pra menos, seja 1 por cento. Eu posso escolher um tamanho de amostra fazendo: | ||
|
||
$$\frac{1}{\sqrt{n}} = 1\% \implies \frac{1}{\sqrt{n}} = \frac{1}{100} \implies \sqrt{n} = 100 = 10^2 \implies n = 10^4 = 10000$$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
library(infer) | ||
|
||
sample_mean <- dados %>% | ||
specify(response = p32c, success = "Sim") %>% | ||
calculate(stat = "prop") | ||
# esse aqui ^ você consegue ler o código | ||
# ele diz o que tá fazendo | ||
|
||
#mean(dados$p32c == "Sim") | ||
#sum(dados$p32c == "Sim")/2079 | ||
# esse aqui vc tem que interpretar o código ^ | ||
|
||
# define a sampling distribution | ||
sampling_dist <- dados %>% | ||
specify(response = p32c, success = "Sim") %>% | ||
assume("z") | ||
# assume 'z' eu tô mandando o R entender | ||
# que pra tudo que seguir eu quero que seja | ||
# considerada uma distribuição teórica normal | ||
|
||
# get the confidence interval---note that the | ||
# point estimate is required here | ||
ci <- get_confidence_interval( | ||
sampling_dist, | ||
level = .95, | ||
point_estimate = sample_mean | ||
) | ||
|
||
sqrt(sample_mean*(1-sample_mean))/sqrt(2079) | ||
|
||
sampling_dist |> | ||
visualise() + | ||
shade_confidence_interval(ci) | ||
|
||
# distribuição t ---------------------------------------------------------- | ||
|
||
library(infer) | ||
|
||
sample_mean <- dados %>% | ||
specify(response = idade1) %>% | ||
calculate(stat = "mean") | ||
|
||
# define a sampling distribution | ||
sampling_dist <- dados %>% | ||
specify(response = idade1) %>% | ||
assume("t") | ||
|
||
# get the confidence interval---note that the | ||
# point estimate is required here | ||
ci <- get_confidence_interval( | ||
sampling_dist, | ||
level = .95, | ||
point_estimate = sample_mean | ||
) | ||
|
||
sampling_dist |> | ||
visualise() + | ||
shade_confidence_interval(ci) | ||
|
||
# distribuicao t amostra pequena ------------------------------------------ | ||
|
||
library(infer) | ||
|
||
sample_mean <- dados %>% | ||
sample_n(5) |> | ||
specify(response = idade1) %>% | ||
calculate(stat = "mean") | ||
|
||
# define a sampling distribution | ||
sampling_dist <- dados %>% | ||
sample_n(5) |> | ||
specify(response = idade1) %>% | ||
assume("t") | ||
|
||
# get the confidence interval---note that the | ||
# point estimate is required here | ||
ci <- get_confidence_interval( | ||
sampling_dist, | ||
level = .95, | ||
point_estimate = sample_mean | ||
) | ||
|
||
sampling_dist |> | ||
visualise() + | ||
shade_confidence_interval(ci) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
library(tidyverse) | ||
|
||
dados <- readxl::read_excel("script/BD_CIS0684.xlsx") | ||
|
||
dados |> | ||
filter( | ||
sexo == "Feminino" | ||
) |> | ||
prop_test(p32c ~ NULL, p = 0.3, success = "Sim") | ||
|
||
dados |> | ||
filter( | ||
sexo == "Feminino" | ||
) |> | ||
t_test(idade1 ~ NULL, mu = 42) | ||
|
||
# a diferença entre esses dois testes é que | ||
# o teste t calcula o valor-p (compara a média observada | ||
# com o que esperaria de outras amostras) a partir | ||
# de uma distribuição t de student, com mais | ||
# dispersão do que a normal | ||
|
||
dados |> | ||
filter( | ||
sexo == "Feminino" | ||
) |> | ||
t_test(idade1 ~ NULL, mu = 41) | ||
|
||
|
||
# plotar o teste-t -------------------------------------------------------- | ||
|
||
modelo <- dados |> | ||
filter(sexo == "Feminino") |> | ||
specify(response = idade1) | ||
|
||
estatistica <- modelo |> | ||
hypothesize(null = "point", mu = 41) |> | ||
calculate(stat = "t") | ||
|
||
modelo |> | ||
assume("t") |> | ||
visualise() + | ||
shade_p_value(obs_stat = estatistica, direction = "both") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# duas amostras ----------------------------------------------------------- | ||
|
||
prop_test(dados, | ||
p32c ~ sexo, | ||
order = c("Masculino", "Feminino")) | ||
|
||
t_test(dados, | ||
idade1 ~ sexo, | ||
order = c("Masculino", "Feminino")) |