我认为下面的代码是形状测试的参数化方法,尽管可能存在一些错误:
# 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 :(得分:0)
使用Bootstrapping,使用Sample函数并从同一样本中绘制。
只有以下2个命令才会更改为sample()函数。替换将为True。您将从glu1和Bpre1中绘制相同的长度。
s1&lt; - rnorm(长度(glu1)) s2&lt; -rnorm(长度(BPre1))
- 去哈士奇