function - Continuous PowerTransform/BoxCox Transformation in R -
i have dataset need transfer normal distribution.
first, generate reproducible dataset.
df <- runif(500, 0, 100)
second, define function. function continue transforming d.f. until p > 0.05. transformed d.f. generated , named y.
boxcoxtrans <- function(y) { lambda <- 1 constant <- 0 while(shapiro.test(y)$p.value < 0.10) { constant <- abs(min(y, na.rm = true)) + 0.001 y <- y + constant lambda <- powertransform(y)$lambda y <- y ^ lambda } assign("y", y, envir = .globalenv) }
third, test df
shapiro.test(df) shapiro-wilk normality test data: df w = 0.95997, p-value = 2.05e-10
because p < 0.05, transform df
boxcoxtrans(df)
then gives me following error messages,
error in qr.resid(xqr, w * fam(y, lambda, j = true)) : na/nan/inf in foreign function call (arg 5)
what did wrong?
you use box-muller transformation generate approximately normal distribution random uniform distribution. might more appropriate box-cox transformation, afaik typically applied convert skewed distribution 1 normal.
here's example of box-muller transformation applied set of uniformly distributed numbers:
set.seed(1234) size <- 5000 <- runif(size) b <- runif(size) y <- sqrt(-2 * log(a)) * cos(2 * pi * b) plot(density(y), main = "example of box-muller transformation", xlab="x", ylab="f(x)") library(nortest) #> lillie.test(y) # # lilliefors (kolmogorov-smirnov) normality test # #data: y #d = 0.009062, p-value = 0.4099 # #> shapiro.test(y) # # shapiro-wilk normality test # #data: y #w = 0.99943, p-value = 0.1301 #
hope helps.
Comments
Post a Comment