set.seed(1) x <- rexp(1000,1) hist(x)
pwr <- function(x){ x <- sample(x, 500) pval <- 0 for(p in seq(-5, 5, 0.01)){ if(p != 0) { tmp <- shapiro.test(x^p)$p.value } if(pval <= tmp) { pval <- tmp } } return(pval) } hist(x^pwr(x))
data(trees) shapiro.test(trees$Girth) shapiro.test(trees$Volume)
> shapiro.test(trees$Height) Shapiro-Wilk normality test data: trees$Height W = 0.9655, p-value = 0.4034 > shapiro.test(trees$Volume) Shapiro-Wilk normality test data: trees$Volume W = 0.8876, p-value = 0.003579
shapiro.test(log(trees$Volume)) hist(log(trees$Volume))
> shapiro.test(log(trees$Volume)) Shapiro-Wilk normality test data: log(trees$Volume) W = 0.9643, p-value = 0.3766log 覲 蠏覿る譟. boxcox 覲 企骸蟾?
boxcox.f <- function(x) { p <- c() l <- c() for(lambda in seq(-2,2,0.1)) { if (lambda != 0) { p.tmp <- shapiro.test((x^lambda-1) / lambda)$p.value if (p.tmp >= 0.05) { l <- c(l, lambda) p <- c(p, p.tmp) } } } d <- data.frame(p,l) #names(d)[names(d)=="p"] <- "pvalue" #names(d)[names(d)=="l"] <- "lambda" #d[d$p==min(d$p)|d$p==max(d$p), c("pvalue", "lambda")] max.lambda <- d[d$p==max(d$p), c("l")] rs <- (x^max.lambda-1) / max.lambda return (rs) } x <- boxcox.f(trees$Volume)
library("MASS") boxcox(lm(Volume~Height+Girth,data=trees),lambda=seq(-1,1,by=.1))
bc <- boxcox(lm(Volume~Height+Girth,data=trees),lambda=seq(-1,1,by=.1)) lambda <- bc$x[which.max(bc$y)]
> bc <- boxcox(lm(Volume~Height+Girth,data=trees),lambda=seq(-1,1,by=.1)) > lambda <- bc$x[which.max(bc$y)] > lambda [1] 0.3030303
lambda <- 0.3 model <- lm(I((Volume^lambda - 1)/lambda)~Height+Girth,data=trees) shapiro.test(resid(model)) #install.packages("lmtest") #library("lmtest") bptest(model) dwtest(model)
> shapiro.test(resid(model)) Shapiro-Wilk normality test data: resid(model) W = 0.96822, p-value = 0.4714 > library("lmtest") れ襯 襦譴: zoo れ れ襯 覿谿: zoo The following objects are masked from package:base: as.Date, as.Date.numeric > dwtest(model) Durbin-Watson test data: model DW = 2.069, p-value = 0.4811 alternative hypothesis: true autocorrelation is greater than 0 > dwtest(model) Durbin-Watson test data: model DW = 2.069, p-value = 0.4811 alternative hypothesis: true autocorrelation is greater than 0 >谿 蠏, 焔一, 襴曙煙 覈 襷譟燕.
b <- 100 lambda <- 0.2 y <- (b^lambda - 1)/lambda ((y*lambda) + 1)^(1/lambda)
x <- rnorm(100, 80, 10) y <- rnorm(100, 80, 10) mydata <- data.frame(x, y, grp="a") mydata <- rbind(mydata, data.frame(x = rnorm(10, 30, 5), y = rnorm(10, 30, 5), grp = "b")) head(mydata) plot(y~x, data=mydata, col=grp) mydata$x <- scale(mydata$x) mydata$y <- scale(mydata$y) mydata$x1 <- mydata$x / sqrt(mydata$x^2 + mydata$y^2) mydata$y1 <- mydata$y / sqrt(mydata$x^2 + mydata$y^2) plot(mydata$x1) plot(mydata$y1) par(mfrow=c(1,2)) plot(mydata$x, mydata$y, main="伎豺螳 一危", col=mydata$grp) plot(mydata$x1, mydata$y1, main="螻糾 覲", col=mydata$grp) par(mfrow=c(1,1))