############################################################## # AULA 20 - Testes de Hipoteses # ############################################################## rm(list = ls()); cat('\014') ####################################################################### # Exemplo: Teste para a média de duas populações Normais (teste t de Student) # AMOSTRA # Mulheres n1 = 9782 xbarra = 36.883459 s1 = 13.532427 # Homens n2 = 20380 ybarra = 39.184004 s2 = 12.873243 var.test(idades ~ sexo, data, alternative = "two.sided") var.test(idadem, idadeh, alternative = "two.sided") Fcal = s1^2 / s2^2 Fcal ## [1] 1.105034 pvalor = pf(Fcal, n1 - 1, n2 - 1) pvalor ## [1] 1 # Erro tipo 1 = alfa alfa = 0.05 # Graus de liberdade gl = n1 + n2 - 2 # Valor de Ttab para o erro tipo 1 qt(alfa/2, gl) ## [1] -1.960043 qt(1 - alfa/2, gl) ## [1] 1.960043 S2p = ((n1-1)*s1^2 + (n2-1)*s2^2) / (n1 - 1 + n2 - 2) S2p ## [1] 171.371 ttab = (xbarra - ybarra) / sqrt(S2p*(1/n1+1/n2)) ttab ## [1] -14.28723 pvalor = 2*pt(ttab, n1 - 1 + n2 - 2) pvalor ## [1] 3.719862e-46 t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, ...) t.test(formula, data, subset, na.action, ...) require(graphics) sleep plot(extra ~ group, data = sleep) with(sleep, mean(extra[group == 1])) ## [1] 0.75 with(sleep, mean(extra[group == 2])) ## [1] 2.33 with(sleep, sd(extra[group == 1])) ## [1] 1.78901 with(sleep, sd(extra[group == 2])) ## [1] 2.002249 # Forma tradicional with(sleep, t.test(extra[group == 1], extra[group == 2])) ## ## Welch Two Sample t-test ## ## data: extra[group == 1] and extra[group == 2] ## t = -1.8608, df = 17.776, p-value = 0.07939 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -3.3654832 0.2054832 ## sample estimates: ## mean of x mean of y ## 0.75 2.33 # Formula tipo regressão t.test(extra ~ group, data = sleep) ## ## Welch Two Sample t-test ## ## data: extra by group ## t = -1.8608, df = 17.776, p-value = 0.07939 ## alternative hypothesis: true difference in means between group 1 and group 2 is not e## 95 percent confidence interval: ## -3.3654832 0.2054832 ## sample estimates: ## mean in group 1 mean in group 2 ## 0.75 2.33 set.seed(12345) x = rnorm(10, 5, 1) y = rnorm(10, 5, 2) mean(x) ## [1] 4.867056 mean(y) ## [1] 5.571956 s1 = sd(x) s2 = sd(y) n1 = length(x) n2 = length(y) # Teste igualdade de variancias var.test(x, y, alternative = "two.sided") ## ## F test to compare two variances ## ## data: x and y ## F = 0.23356, num df = 9, denom df = 9, p-value = 0.0413 ## alternative hypothesis: true ratio of variances is not equal to 1 ## 95 percent confidence interval: ## 0.05801292 0.94030993 ## sample estimates: ## ratio of variances ## 0.2335597 Fcal = s1^2/s2^2 Fcal ## [1] 0.2335597 pvalor = 2*pf(Fcal, n1 - 1, n2 - 1) pvalor ## [1] 0.04130414 # Variancias são diferentes, uma vez que pvalor < alfa. Portanto, usaremos o # teste de igualdade de medias para variancias desconhecidas e diferentes t.test(x, y, alternative = "two.sided", var.equal = FALSE) ## ## Welch Two Sample t-test ## ## data: x and y ## t = -1.1921, df = 12.987, p-value = 0.2546 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -1.9825002 0.5727008 ## sample estimates: ## mean of x mean of y ## 4.867056 5.571956 nu = (s1^2/n1 + s2^2/n2)^2/((s1^2/n1)^2/(n1-1) +(s2^2/n2)^2/(n2-1)) nu ## [1] 12.9866 2*pt(-1.1921, nu) ## [1] 0.2545508 # DADOS 1 set.seed(12345) x = rnorm(10, 5, 1) y = rnorm(10, 4, 1) mean(x) ## [1] 4.867056 mean(y) ## [1] 4.285978 sd(x) ## [1] 0.8136554 sd(y) ## [1] 0.8418052 length(x) ## [1] 10 length(y) ## [1] 10 t.test(x, y, alternative = "two.sided", var.equal = FALSE) ## ## Welch Two Sample t-test ## ## data: x and y ## t = 1.5695, df = 17.979, p-value = 0.134 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -0.1968018 1.3589580 ## sample estimates: ## mean of x mean of y ## 4.867056 4.285978 # DADOS 2 x = rnorm(50, 5, 1) y = rnorm(50, 4, 1) mean(x) ## [1] 5.20829 mean(y) ## [1] 4.27182 sd(x) ## [1] 1.234496 sd(y) ## [1] 1.046154 length(x) ## [1] 50 length(y) ## [1] 50 t.test(x, y, alternative = "two.sided", var.equal = FALSE) ## ## Welch Two Sample t-test ## ## data: x and y ## t = 4.0922, df = 95.432, p-value = 8.935e-05 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## 0.4821888 1.3907509 ## sample estimates: ## mean of x mean of y ## 5.20829 4.27182 t.test(x, y, alternative = "two.sided", var.equal = TRUE)