library(dplyr)
library(knitr)
Criação da função
test.normal <- function(x)
{
if(!require(nortest))
install.packages("nortest")
library(nortest)
# Estimativas dos parâmetros
xb <- mean(x) # mu
sx <- sd(x) # sigma
cat("\n Média amostral =", xb, "\n Desvio padrão amostral =", sx, "\n")
# Testes
t1 <- ks.test(x, "pnorm", xb, sx) # KS
t2 <- lillie.test(x) # Lilliefors
t3 <- cvm.test(x) # Cramér-von Mises
t4 <- shapiro.test(x) # Shapiro-Wilk
t5 <- sf.test(x) # Shapiro-Francia
t6 <- ad.test(x) # Anderson-Darling
# Tabela de resultados
testes <- c(t1$method,
t2$method,
t3$method,
t4$method,
t5$method,
t6$method)
estt <- as.numeric(c(t1$statistic,
t2$statistic,
t3$statistic,
t4$statistic,
t5$statistic,
t6$statistic))
valorp <- c(t1$p.value,
t2$p.value,
t3$p.value,
t4$p.value,
t5$p.value,
t6$p.value)
resultados <- cbind(estt, valorp) %>% as.data.frame() %>%
mutate(valorp = ifelse(valorp < 0.001,
paste("< 0.001", "***"),
ifelse(valorp < 0.01,
paste(round(valorp, 4), "**"),
ifelse(valorp < 0.05,
paste(round(valorp, 4), "*"),
ifelse(valorp < 0.1,
paste(round(valorp, 4), "."),
paste(round(valorp, 4), "ns"))))))
rownames(resultados) <- testes
colnames(resultados) <- c("Estatística", "p")
resultados
}
Gerando os dados
set.seed(1234)
dados <- rnorm(100,5)
dados
## [1] 3.792934 5.277429 6.084441 2.654302 5.429125 5.506056 4.425260 4.453368
## [9] 4.435548 4.109962 4.522807 4.001614 4.223746 5.064459 5.959494 4.889715
## [17] 4.488990 4.088805 4.162828 7.415835 5.134088 4.509314 4.559452 5.459589
## [25] 4.306280 3.551795 5.574756 3.976344 4.984862 4.064051 6.102298 4.524407
## [33] 4.290560 4.498742 3.370907 3.832381 2.819960 3.659007 4.705706 4.534102
## [41] 6.449496 3.931357 4.144635 4.719377 4.005660 4.031486 3.892682 3.748014
## [49] 4.476172 4.503150 3.193969 4.417924 3.891110 3.985038 4.837690 5.563056
## [57] 6.647817 4.226647 6.605910 3.842191 5.656588 7.548991 4.965240 4.330366
## [65] 4.992395 6.777084 3.861392 6.367827 6.329565 5.336473 5.006893 4.544531
## [73] 4.633476 5.648287 7.070271 4.846602 3.609299 4.276418 5.258262 4.682941
## [81] 4.822210 4.830006 3.627698 4.826213 5.850232 5.697609 5.549997 4.597268
## [89] 4.808406 3.805472 4.946841 5.255196 6.705964 6.001513 4.504417 5.355550
## [97] 3.865392 5.878204 5.972917 7.121117
Utilizando a função
test.normal(dados) %>% kable(digits = 4)
##
## Média amostral = 4.843238
## Desvio padrão amostral = 1.004405
| Estatística | p | |
|---|---|---|
| One-sample Kolmogorov-Smirnov test | 0.1012 | 0.257 ns |
| Lilliefors (Kolmogorov-Smirnov) normality test | 0.1012 | 0.0133 * |
| Cramer-von Mises normality test | 0.2190 | 0.003 ** |
| Shapiro-Wilk normality test | 0.9659 | 0.0108 * |
| Shapiro-Francia normality test | 0.9669 | 0.0147 * |
| Anderson-Darling normality test | 1.2438 | 0.0029 ** |