_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › 覲覲

Contents

1 power 覲
2 蠏覿
3 log 覲
4 boxcox 覲
5 boxcox 覲:蠏覿
6 碁Μ 覲
7 企襴 覲
8 螻糾 覲


1 power 覲 #

螳 讌 覿螳 .
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))

2 蠏覿 #

shapiro.test襦 . p-value螳 0.05企 蠏覿手 覲碁.
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

Heigth 蠏覿願, Volume 蠏覿螳 . log覲 企慨.

3 log 覲 #

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.3766
log 覲 蠏覿る譟. boxcox 覲 企骸蟾?

4 boxcox 覲 #

蠏碁 襷れ 覺る. shapiro.test襯 伎覩襦 5000螳 危 覲螳襷 .

螻旧 = http://onlinestatbook.com/2/transformations/box-cox.html
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)

5 boxcox 覲:蠏覿 #

蠏讌朱 谿 覿 , 谿
  • 蠏覿襯 磯殊狩螻, --> shapiro.test(resid(model))
  • 覿一 殊螻, --> bptest(model)
  • 豪 豢碁ゼ 覲伎伎 . --> dwtest(model)
讀, 谿 蠏, 焔一, 襴曙煙 襷譟燕伎 . 谿瑚襦 bptest, dwtest lmtest library襯 .

襾殊 覲 覲 讌 襦蠏碁 蠏覿 企慨.
  • model <- lm(Volume~Height+Girth,data=trees)
  • model <- lm(I(log(Volume))~Height+Girth,data=trees)
襴曙煙 襷譟煙れ 覈詩. 蠏碁 box-cox覲 れ企慨.

library("MASS") 
boxcox(lm(Volume~Height+Girth,data=trees),lambda=seq(-1,1,by=.1))
boxcox.png

蠏碁殊 覲企 蟆讌襷 0.3 lambda螳 蟆一る 蟆 覲 . 豕 lambda 螳 谿場覲伎.
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)

6 碁Μ 覲 #

8 螻糾 覲 #

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))
t1_2.png
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2020-07-23 15:12:54

企れ 蟆語. 伎 . ル 螻旧. 伎 譴.