######################################################Example 1: Shapiro-Wilk Test on Normal Data 3 ≤n ≤5000 
#make this example reproducible
set.seed(0)

#create dataset of 100 random values generated from a normal distribution
data <- rnorm(100)

#perform Shapiro-Wilk test for normality
shapiro.test(data)
## 
##  Shapiro-Wilk normality test
## 
## data:  data
## W = 0.98957, p-value = 0.6303
shapiro.test(data)$statistic
##         W 
## 0.9895747
shapiro.test(data)$p.value
## [1] 0.630297
shapiro.test(data)$method
## [1] "Shapiro-Wilk normality test"
hist(data, col='steelblue')

#The p-value of the test turns out to be 0.6303. Since this value is not less than .05, 
#we can assume the sample data comes from a population that is normally distributed.

#Example 2: Shapiro-Wilk Test on Non-Normal Data

#make this example reproducible
set.seed(0)

#create dataset of 100 random values generated from a Poisson distribution
data <- rpois(n=100, lambda=3)

#perform Shapiro-Wilk test for normality
shapiro.test(data)
## 
##  Shapiro-Wilk normality test
## 
## data:  data
## W = 0.94397, p-value = 0.0003393
hist(data, col='coral2')

#What to Do with Non-Normal Data
#1. Log Transformation: Transform the response variable from y to log(y).

#2. Square Root Transformation: Transform the response variable from y to √y.
#3. Cube Root Transformation: Transform the response variable from y to y1/3.


####################################################Kolmogorov-Smirnov Test in R n > 5000
#Example 1: One Sample Kolmogorov-Smirnov Test
#make this example reproducible
set.seed(0)

#generate dataset of 100 values that follow a Poisson distribution with mean=5
data <- rpois(n=20, lambda=5)
#perform Kolmogorov-Smirnov test
ks.test(data, "pnorm")
## Warning in ks.test.default(data, "pnorm"): Kolmogorov - Smirnov检验里不应该有连
## 结
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  data
## D = 0.97725, p-value < 2.2e-16
## alternative hypothesis: two-sided
hist(data, col='coral2')

#ref https://www.statology.org/kolmogorov-smirnov-test-r/
#ref  https://www.statology.org/shapiro-wilk-test-r/
#ref https://zhuanlan.zhihu.com/p/65585633