library(knitr)
opts_chunk$set(echo = TRUE )

Package install

Packages <- c("tidyverse", "car", "dunn.test","onewaytests","FSA")
lapply(Packages, library, character.only = TRUE)
## -- Attaching packages --------------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.2.1     √ purrr   0.3.3
## √ tibble  2.1.3     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## -- Conflicts ------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
## ## FSA v0.8.26. See citation('FSA') if used in publication.
## ## Run fishR() for related website and fishR('IFAR') for related book.
## 
## Attaching package: 'FSA'
## The following object is masked from 'package:car':
## 
##     bootCase
## [[1]]
##  [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
##  [7] "tibble"    "ggplot2"   "tidyverse" "knitr"     "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "car"       "carData"   "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "knitr"    
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[3]]
##  [1] "dunn.test" "car"       "carData"   "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "knitr"     "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [19] "methods"   "base"     
## 
## [[4]]
##  [1] "onewaytests" "dunn.test"   "car"         "carData"     "forcats"    
##  [6] "stringr"     "dplyr"       "purrr"       "readr"       "tidyr"      
## [11] "tibble"      "ggplot2"     "tidyverse"   "knitr"       "stats"      
## [16] "graphics"    "grDevices"   "utils"       "datasets"    "methods"    
## [21] "base"       
## 
## [[5]]
##  [1] "FSA"         "onewaytests" "dunn.test"   "car"         "carData"    
##  [6] "forcats"     "stringr"     "dplyr"       "purrr"       "readr"      
## [11] "tidyr"       "tibble"      "ggplot2"     "tidyverse"   "knitr"      
## [16] "stats"       "graphics"    "grDevices"   "utils"       "datasets"   
## [21] "methods"     "base"

Data import

d1<- read.csv("/Users/koho0/Desktop/Dr Hong/Fig 2 2groups/P7 mEPSC freq.csv")

Data structure

str(d1)
## 'data.frame':    47 obs. of  3 variables:
##  $ subject: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ group  : Factor w/ 2 levels "con","sevo": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ampl   : num  0.05 0.0667 0.025 0.0333 0.0333 ...

Explorative data analysis with graphics

p=ggplot(d1,aes(d1[,2],d1[,3]))
p + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2) + ylab("Value") +xlab("Groups")

Easystat function developed by S. Park (available at https://rpubs.com/goodlebang)

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(sprintf("%.3f",as.numeric(swd$p.value))), "\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",
          "p =", paste(format(round(kwd$p.value,3), nsmall=3)), "\n",
           "A statistically significant difference exist between groups")
      DT = dunnTest(d1[,3] ~ d1[,2],data=d1,method="bh") 
      DT
    } else {                                        #
      cat( "2. The result of Kruskall_Wallis test:","\n",
           "p =", paste(format(round(kwd$p.value,3), nsmall=3)), "\n",
           "A statistically significant difference do not exist between groups")
    }
  } 
  else {                      # 정규성 가정을 만족하는 경우
    cat("1. Normality assumption test by Shapiro_Wilk test is", "\n",
        "p =", paste(format(round(swd$p.value,3), 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(round(bnt$p.value,3), 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", "\n",
            "p =", paste(format(round(wtd$p.value,3), 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",  "\n",   # 
            "p =", paste(format(round(wtd$p.value,3), 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(round(bnt$p.value,3), nsmall=3)), "\n",
          "Equal variance assumption was not rejected","\n")
      md2=aov(d1[,3]~d1[,2],data=d1)
      summary(md2)
      library(moonBook)
        paov=comma(summary(md2)[[1]][[1, "Pr(>F)"]])# anova

      if(as.numeric(paov)<0.05) {                               # 결과가 유의한 경우
        cat("3. The result of anova is",  "\n",
            "p =", paste(sprintf("%.3f",as.numeric(paov))), "\n",
            "A statistically significant difference exist between groups")
     TukeyHSD(md2) } 
        
        else{                                              # 결과가 유의하지 않은 경우
        cat("3. The result of anova is", "\n",
            "p =",paste(sprintf("%.3f",as.numeric(paov))), "\n",
         
                       "A statistically significant difference do not exist between groups")
          }
    }
  }
}

Statistical Result

easystat(d1)
## 1. Normality assumption test by Shapiro_Wilk test is 
##  p = 0.000 
##  Normality assumption was rejected 
## 2. The result of Kruskall_Wallis test: 
##  p = 0.104 
##  A statistically significant difference do not exist between groups