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
3 changed files
with
86 additions
and
162 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
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 |
---|---|---|
@@ -1,121 +1,28 @@ | ||
# Teste de proporção no R ------------------------------------------------- | ||
|
||
numero_desemprego_pnad_2024 = 0.078*180000 | ||
# teste de hipotese sai de graça dos graficos de intervalo de conf -------- | ||
|
||
prop.test( | ||
x = 14040, | ||
n = 180000, | ||
p = 0.077, | ||
alternative = "two.sided") | ||
|
||
prop.test( | ||
x = 14040, | ||
n = 180000, | ||
p = 0.077, | ||
alternative = "greater") | ||
|
||
# Área da normal no R ----------------------------------------------------- | ||
|
||
?pnorm | ||
|
||
1-pnorm( | ||
0.078, | ||
0.077, | ||
sqrt(0.077*(1-0.077))/sqrt(180000) | ||
) | ||
|
||
# Ler dados --------------------------------------------------------------- | ||
|
||
library(tidyverse) | ||
|
||
dados <- readxl::read_excel("script/BD_CIS0684.xlsx") | ||
|
||
p32c_mulheres <- dados |> | ||
filter(sexo == "Feminino") |> | ||
count(p32c) | ||
|
||
prop.test( | ||
x = 294, | ||
n = 1089, | ||
p = 0.22 | ||
) | ||
|
||
prop.test( | ||
x = p32c_mulheres$n[2], | ||
n = sum(p32c_mulheres$n), | ||
p = 0.26 | ||
) | ||
|
||
p32c_homens <- dados |> | ||
filter(sexo == "Masculino") |> | ||
count(p32c) | ||
|
||
prop.test( | ||
x = p32c_homens$n[2], | ||
n = sum(p32c_homens$n), | ||
p = 0.26 | ||
) | ||
|
||
# pergunta do helder, porque testamos a segunda categoria? | ||
# porque colocamos "[2]"? | ||
|
||
# porque queremos testar a probabilidade de "sim" | ||
|
||
# daria pra testar a probabilidade de "não" também: | ||
|
||
prop.test( | ||
x = p32c_homens$n[1], | ||
n = sum(p32c_homens$n), | ||
p = 0.74 | ||
) | ||
|
||
# cálculo correto | ||
library(survey) | ||
# | ||
dclus1<-svydesign(id=~nquest, weights=~PESO, data=dados) | ||
|
||
dclus_mulheres <- svydesign( | ||
id=~nquest, | ||
weights=~PESO, | ||
data=filter(dados, sexo == "Feminino") | ||
) | ||
|
||
tabela_contagem <- svytable(~p32c, dclus_mulheres) | ||
|
||
prop.test(tabela_contagem, p = 0.74) | ||
# o proptest aceita tabelas de survei se precisar! | ||
|
||
# Tidy test --------------------------------------------------------------- | ||
|
||
library(infer) | ||
# tenho por hipotese que 30% das mulheres deveriam ter respondido | ||
# "sim" no ultimo grafico | ||
|
||
dados |> | ||
filter( | ||
sexo == "Feminino" | ||
) |> | ||
prop_test(p32c ~ NULL, p = 0.3, success = "Sim") | ||
# essa função é alternativa ao prop.test padrão do R | ||
# que aceita tabelas diretamente como input | ||
|
||
# pra gente não precisar fazer contagem na mão | ||
|
||
modelo <- dados |> | ||
dados_brutos <- dados |> | ||
filter(sexo == "Feminino") |> | ||
specify(response = p32c, success = "Sim") | ||
|
||
estatistica <- modelo |> | ||
hypothesize(null = "point", p = 0.3) |> | ||
sampling_dist <- dados |> | ||
filter(sexo == "Feminino") |> | ||
specify(response = p32c, success = "Sim") |> | ||
assume("z") | ||
|
||
estatistica_do_teste <- dados_brutos |> | ||
hypothesise(null = "point", p = 0.30) |> | ||
calculate(stat = "z") | ||
|
||
grafico_ggplot <- modelo |> | ||
assume("z") |> | ||
sampling_dist |> | ||
visualise() + | ||
shade_p_value(obs_stat = estatistica, direction = "both") | ||
|
||
grafico_ggplot + | ||
labs(caption = "valor-p calculado de 3,32%") + | ||
theme_bw() + | ||
labs(x = "Valor da estatística do teste", y = "", | ||
title = "Distribuição amostral teórica") | ||
|
||
?infer::prop_test | ||
shade_p_value(estatistica_do_teste, direction = "left") |
Binary file not shown.