Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. library(normtest)
  2. library(nortest)
  3. library(sn)
  4. library(ggplot2)
  5.  
  6.  
  7. #Comparing Jarque-Bera, Lilliefors, Anderson-Darling and Shapiro-Wilk normalitytests
  8. ssize <- seq(from=100, to=500, by=100)
  9. alpha <- 0.05
  10. sims <- 200
  11.  
  12. pow.jb <- rep(NA, length(ssize))
  13. pow.lillie <- rep(NA, length(ssize))
  14. pow.ad <- rep(NA, length(ssize))
  15. pow.shap <- rep(NA, length(ssize))
  16.  
  17. #Different sample sizes
  18. for (i in 1:length(ssize)){
  19. prob.jb <- numeric(sims)
  20. prob.lillie <- numeric(sims)
  21. prob.ad <- numeric(sims)
  22. prob.shap <-numeric(sims)
  23. N <- ssize[i]
  24.  
  25. #Run the simulations with the current sample size
  26. for (j in 1:sims){
  27. ##### Select one distribution ####
  28. #data <- rsn(N)
  29. data <- rchisq(N, df=5)
  30. #data <- rcauchy(N)
  31. #data <- rt(N,df=5)
  32. ##################################
  33.  
  34. #Execute the normalitytests
  35. prob.jb[j] <- jb.norm.test(data)$p.value
  36. prob.lillie[j] <- lillie.test(data)$p.value
  37. prob.ad[j] <- ad.test(data)$p.value
  38. prob.shap[j] <- shapiro.test(data)$p.value
  39. }
  40.  
  41. #Calculate the powers of the normalitytests
  42. pow.jb[i] <- mean(prob.jb < alpha)
  43. pow.lillie[i] <- mean(prob.lillie < alpha)
  44. pow.ad[i] <- mean(prob.ad < alpha)
  45. pow.shap[i] <- mean(prob.shap < alpha)
  46. }
  47.  
  48. #Plot the results
  49. df<- data.frame(samples=ssize,
  50. jb=pow.jb,
  51. lillie=pow.lillie,
  52. ad=pow.ad,
  53. shap=pow.shap)
  54. ggplot(df, aes(samples)) +
  55. geom_line(aes(y = jb, colour = "jb")) +
  56. geom_line(aes(y = lillie, colour = "lillie")) +
  57. geom_line(aes(y = ad, colour = "ad")) +
  58. geom_line(aes(y = shap, colour = "shap"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement