R Markdown - Exemplo 1 09/11

#Rsolução do exemplo 1 Aula de EAD 09/11

1 - Inserindo os dados no R

od <- c(1.2, 1.4, 1.4, 1.3, 1.2, 1.35, 1.4, 2.0, 1.95, 1.1, 1.75, 1.05, 1.05, 1.4)
od
##  [1] 1.20 1.40 1.40 1.30 1.20 1.35 1.40 2.00 1.95 1.10 1.75 1.05 1.05 1.40
str(od)
##  num [1:14] 1.2 1.4 1.4 1.3 1.2 1.35 1.4 2 1.95 1.1 ...

2 - Analise exploratória

summary(od)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.050   1.200   1.375   1.396   1.400   2.000
sort(od)
##  [1] 1.05 1.05 1.10 1.20 1.20 1.30 1.35 1.40 1.40 1.40 1.40 1.75 1.95 2.00

3 - Visualizando distribuição dos dados

boxplot(od-1.2, col="lightgrey", ylab = "Oxigênio Dossilvido mg/l")

4 - Teste de Shapiro-wilks de normalidade

shapiro.test(od - 1.2)
## 
##  Shapiro-Wilk normality test
## 
## data:  od - 1.2
## W = 0.86929, p-value = 0.041
# Diferença observada 
mean(od) - 1.2
## [1] 0.1964286
### 4.1 - Teste de intervalo de confiança - Tstudent 

#com alpha = 5% 
t.test(od - 1.2, alternative = c("two.sided"), mu = 0, conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  od - 1.2
## t = 2.4068, df = 13, p-value = 0.03168
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.02010878 0.37274836
## sample estimates:
## mean of x 
## 0.1964286

As evidências do boxplot e do teste de Shapiro-wilks indicam que os dados não tem distribuição aproximadamente normal. Entretanto, como exercício, iremos utilizar o teste e o intervalo de confiança baseado na distribuição t para responder, inicialmente, a questão.

5 - Teste não-paramétrico de Wilcoxon

wilcox.test(od - 1.2, mu = 0, conf.int = TRUE)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  od - 1.2
## V = 70, p-value = 0.01627
## alternative hypothesis: true location is not equal to 0
## 95 percent confidence interval:
##  0.0249788 0.4749679
## sample estimates:
## (pseudo)median 
##      0.1999742

6 - Teste de Hipóteses e IC para duas Amostras

6.1 - Teste e IC para duas amostras independentes no R

t.test(x, y = NULL, 
alternative = c ("two.sided", "less"","greater"), 
mu = 0, 
paired = FALSE, 
var.equal = FALSE, 
conf.level = 0.95, ...)

7 - Determinando o Tamanho da Amostra

library(pwr)
pwr.p.test(h=ES.h(p1 = 0.75, p2 = 0.50), sig.level = 0.05, power = 0.80, alternative = "greater")
## 
##      proportion power calculation for binomial distribution (arcsine transformation) 
## 
##               h = 0.5235988
##               n = 22.55126
##       sig.level = 0.05
##           power = 0.8
##     alternative = greater

Definindo \(H_a\) como bilateral

pwr.p.test(h = ES.h(p1 = 0.75, p2 = 0.50),
           sig.level = 0.05,
           n = 23)
## 
##      proportion power calculation for binomial distribution (arcsine transformation) 
## 
##               h = 0.5235988
##               n = 23
##       sig.level = 0.05
##           power = 0.7092308
##     alternative = two.sided

Reduzindo o Tamanho do Efeito

pwr.p.test(h = ES.h(p1 = 0.65, p2 = 0.50),
           sig.level = 0.05,
           power = 0.80)
## 
##      proportion power calculation for binomial distribution (arcsine transformation) 
## 
##               h = 0.3046927
##               n = 84.54397
##       sig.level = 0.05
##           power = 0.8
##     alternative = two.sided

Diversos Tamanhos do Efeito

pwr.t.test(d = c(0.2, 0.5, 0.8), n = 14, sig.level = 0.05,
type= "one.sample", alternative= "two.sided")
## 
##      One-sample t test power calculation 
## 
##               n = 14
##               d = 0.2, 0.5, 0.8
##       sig.level = 0.05
##           power = 0.1068087, 0.4102363, 0.7900878
##     alternative = two.sided

8 - Estimativa de IC Via Bootstrap

# dados
gas = c(64, 65, 75, 67, 65, 74, 75)
xbar = c() # inicializando o vetor
for (i in 1:1999) {
  amostras = sample(gas, size = length(gas), replace = TRUE) 
  xbar[i] = mean(amostras)
  }
hist(xbar)

# Estimativa de IC via Boostrapp 
quantile(xbar, c(.025, .975))
##     2.5%    97.5% 
## 66.00000 73.42857