easystat function

easystat=function(d1){
  d1=data.frame(d1) # dataframe으로 전환
  d1[,2]=as.factor(d1[,2]) # 2행을 factor변수로 변환
  lmd=lm(d1[,3]~d1[,2],data=d1) # 선형모형
  swd=shapiro.test(lmd$resid) # 정규성 검정
  if (swd$p.value<0.05){      # p-value가 0.05보다 작다면(정규성 가정 위반)
    cat("1. Normality assumption test by shapiro_wilk test is", "\n",
        "p=", paste(format.pval(swd$p.value, nsamll=3)), "\n",
        "Normality assumption was rejected", "\n")   #
    kwd=kruskal.test(d1[,3]~d1[,2],data=d1)             # 비모수 검정인 KW test 시행
    if (kwd$p.value<0.05){                           # KW test결과가 유의하게 나온 경우
      cat("2. The result of Kruskall_Wallis test:","\n",
          "A statistically significant difference", 
          "(", "p=", paste(format.pval(kwd$p.value, nsmall=3)), ")",
          sep="", "exist between groups")
      DT = dunnTest(d1[,3] ~ d1[,2],data=d1,method="bh") 
      DT
    } else {                                        #
      cat( "2. The result of Kruskall_Wallis test:","\n",
           "A statistically significant difference", 
          "(", "p=", paste(format.pval(kwd$p.value, nsmall=3)), ")",
          sep="", "do not exist between groups")
    }
  } 
  else {                      # 정규성 가정을 만족하는 경우
    cat("1. Normality assumption test by shapiro_wilk test is", "\n",
        "p=", paste(format.pval(swd$p.value, nsamll=3)), "\n",
        "Normality assumption was not rejected", "\n")     # 
    bnt=bartlett.test(d1[,3]~d1[,2],data=d1)                  # 등분산성 검정
    if (bnt$p.value<0.05) {                                # 등분산성 가정을 만족하지 않은 경우
      cat("2. Equal variance test by Bartlett test is", "\n",   # 만족하지 못했다고 출력
          "p=", paste(format.pval(bnt$p.value, nsmall=3)), "\n",
          "Equal variance assumption was rejected","\n")
      wtd=oneway.test(d1[,3]~d1[,2], data=d1, na.action=na.omit, var.equal=FALSE)          # 등분산성 가정을 만족하지 못했기 때문에 Welch anova 시행
      if (wtd$p.value<0.05) {                              # welch anova가 유의하게 나온경우
        cat("3. The result of Welch anova is", sep="", "\n",
            "p=", paste(format.pval(wtd$p.value, nsmall=3)), "\n",
            "A statistically significant difference exist between groups")
        md2=aov(d1[,3]~d1[,2])
      summary(md2)
      library(moonBook)
        paov=comma(summary(md2)[[1]][[1, "Pr(>F)"]])
        TukeyHSD(md2)
      } 
      else {                                             # Welch anova가 유의하지 않은 경우
        cat("3. The result of Welch anova is", sep="", "\n",   # 
            "p=", paste(format.pval(wtd$p.value, nsmall=3)), "\n",
            "A statistically significant difference do not exist between groups")
      }
    } else {                                               #
      cat("2. Equal variance test by Bartlett test is", "\n",   # 등분산 가정을 만족했다고 출력
          "p=", paste(format.pval(bnt$p.value, nsmall=3)), "\n",
          "Equal variance assumption was not rejected","\n")
      md2=aov(d1[,3]~d1[,2])
      summary(md2)
      library(moonBook)
        paov=comma(summary(md2)[[1]][[1, "Pr(>F)"]])# anova
      if(paov<0.05) {                               # 결과가 유의한 경우
        cat("3. The result of anova is", sep="", "\n",
            "p=", paste(format(paov)), "\n",
            "A statistically significant difference exist between groups")
     TukeyHSD(md2) } 
        
        else{                                              # 결과가 유의하지 않은 경우
        cat("3. The result of anova is", sep="", "\n",
            "p=",paste(paov), "\n",
            "A statistically significant difference do not exist between groups")
      }
    }
  }
}