Skip to content

Instantly share code, notes, and snippets.

@gomesfellipe
Created February 20, 2018 03:49
Show Gist options
  • Save gomesfellipe/621f1f8947d35ff13207dcb54edb2d5b to your computer and use it in GitHub Desktop.
Save gomesfellipe/621f1f8947d35ff13207dcb54edb2d5b to your computer and use it in GitHub Desktop.
Funções para avaliar a normalidade dos dados
#Função realiza o qqplot com "envelope"
#Envelope
envelope<-function(x){
n <- length(x)
nsim <- 100 # Número de simulações
conf <- 0.95 # Coef. de confiança
# Dados simulados ~ normal
dadossim <- matrix(rnorm(n*nsim, mean = mean(x), sd = sd(x)), nrow = n)
dadossim <- apply(dadossim,2,sort)
# Limites da banda e média
infsup<-apply(dadossim,1,quantile, probs = c((1 - conf) / 2,(1 + conf) / 2))
xbsim <- rowMeans(dadossim)
faixay <- range(x, dadossim)
qq0 <- qqnorm(x, main = "", xlab = "Quantis teóricos N(0,1)", pch = 20, ylim = faixay)
eixox <- sort(qq0$x)
lines(eixox, xbsim)
lines(eixox, infsup[1,], col = "red")
lines(eixox, infsup[2,], col = "red")
}
normalidade<-function(x){
t1 <- ks.test(x, "pnorm",mean(x), sd(x)) # 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
t7<-pearson.test(x) # Pearson Test of Normality
testes <- c(t1$method, t2$method, t3$method, t4$method, t5$method,t6$method,t7$method)
valorp <- c(t1$p.value, t2$p.value, t3$p.value, t4$p.value, t5$p.value,t6$p.value,t7$p.value)
resultados <- cbind(valorp)
rownames(resultados) <- testes
print(resultados, digits = 4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment