2017-03-07 25 views
0

我認爲下面的代碼是形狀測試參數的方式,雖然可能有一些錯誤:如何進行非參數形狀測試?

# Shape test 
glu <- diabetes$Glucose 
BPre <- diabetes$BloodPressure 
plot(density(glu)) 
plot(density(BPre)) 
# To compare the shape, first to standardize them 
glu1<- (glu - mean(glu))/sd(glu) 
BPre1 <- (BPre - mean(BPre))/sd(BPre) 


plot(density(glu1)) 
lines(density(BPre1), col = "red") 


# Compute test statistic 
q <- c(0.1, 0.4, 0.6, 0.8, 0.9) 
x1 <- quantile(glu1, probs = q) 
x2 <- quantile(BPre1, probs = q)   
tstat <- sum(abs(x1 - x2))   
tstat   
s1 <- rnorm(length(glu1)) 
s2 <- rnorm(length(BPre1)) 

# Describe the population and generate one synthetic sample 
f1 <- function() 
{ 
    s1 <- rnorm(length(glu1)) 
    s2 <- rnorm(length(BPre1)) 
    q <- c(0.1, 0.4, 0.6, 0.8, 0.9) 
    x1 <- quantile(s1, probs = q) 
    x2 <- quantile(s2, probs = q)   
    return(sum(abs(x1 - x2))) 

} 

# Create sampling distribution 
sdist <- replicate(10000, f1()) 
# Plot sampling distribution & create p-value 
plot(density(sdist)) 
abline(v = tstat, col = "dark red") 
# Gap 
gap <- abs(mean(sdist) - tstat) 
abline(v = mean(sdist) + c(-1,1) * gap, col = "dark orange") 
s1 <- sdist[sdist <(mean(sdist - gap)) | sdist >(mean(sdist + gap))] 
pvalue <- length(s1)/length(sdist) 
pvalue 

我在想,如果我可以用相同的數據做非參數檢驗形狀。我的直覺告訴我是可能的。對於如何將glu1和BPres轉換爲非參數化方式,我只需要一點點啓發。謝謝!

回答

0

使用Bootstrapping,通過使用Sample函數並從相同的樣本中繪製。

只有這些低於2個命令纔會變成sample()函數。替換將爲True.You將從glu1和Bpre1中繪製出相同的長度。

S1 < - RNORM(長度(glu1)) S2 < - RNORM(長度(BPre1))

- 轉到哈士奇

相關問題