Some examples of Normality test and power transformation
#Normality test and transformation to normality
#generate uniform distribution which is not normal
normalNo1 <- runif(1000, min=2, max =4)
hist(normalNo1, breaks = 50)
#null statement: Distribution is normal
shapiro.test(normalNo1)
##
## Shapiro-Wilk normality test
##
## data: normalNo1
## W = 0.95354, p-value < 2.2e-16
#generate a normal distribution and add skewness
normalNo2<- rnorm(1000, mean=0, sd=1)
hist(normalNo2, breaks = 50)
normalNo2 <- exp(normalNo2)
hist(normalNo2, breaks = 50)
#null statement: Distribution is normal
shapiro.test(normalNo2)
##
## Shapiro-Wilk normality test
##
## data: normalNo2
## W = 0.66337, p-value < 2.2e-16
##Power Transformation based on Box and Cox (1964)
#lambda = -1. is a reciprocal transform.
#lambda = -0.5 is a reciprocal square root transform.
#lambda = 0.0 is a log transform.
#lambda = 0.5 is a square root transform.
#lambda = 1.0 is no transform.
x2 <- powerTransform(normalNo2 ~ 1, family="bcPower")
summary(x2)
## bcPower Transformation to Normality
## Est Power Rounded Pwr Wald Lwr Bnd Wald Upr Bnd
## Y1 -0.0193 0 -0.0745 0.0359
##
## Likelihood ratio test that transformation parameter is equal to 0
## (log transformation)
## LRT df pval
## LR test, lambda = (0) 0.4694733 1 0.49323
##
## Likelihood ratio test that no transformation is needed
## LRT df pval
## LR test, lambda = (1) 1310.454 1 < 2.22e-16
lamb <- x2$lambda #lambda is very close to zero suggesting a log transform
lamb
## Y1
## -0.01930521
xNorm2 <- normalNo2^x2$lamb
hist(xNorm2, breaks = 50)
shapiro.test(xNorm2) ##the null statement that the distribution is normal could not be rejected.
##
## Shapiro-Wilk normality test
##
## data: xNorm2
## W = 0.99829, p-value = 0.4244