简体   繁体   English

在 R 中构建修剪均值的置信区间

[英]Constructing confidence intervals for trimmed means in R

I'd like to test the coverage probabilities for trimmed means, I am using the formula form Wilcox book for confidence intervals: Confidence interval The s_w is Winsorised variance and γ is the proportion coefficient, in my code it's denoted as alpha.我想测试修剪均值的覆盖概率,我使用 Wilcox 书中的公式作为置信区间:置信区间s_w 是 Winsorised 方差,γ 是比例系数,在我的代码中它表示为 alpha。 The problem is, that the code, I have made outputs confidence intervals with 0 always in them, so that the coverage probability is 1. So, I think there is some error in the construction.问题是,我的代码输出置信区间始终为 0,因此覆盖概率为 1。所以,我认为构造中存在一些错误。

Code:代码:

sample_var <- function(data, alpha){
  n <- length(data)
  data <- sort(data)
  data_t <- data[(floor(n*alpha)+1):(n-floor(alpha*n))]
  m <- length(data_t)
  t_mean <- mean(data_t)
 sigma <-  (1/(1-2*alpha)^2)* ((1/n) *sum((data_t-t_mean)^2)+ alpha*(data_t[1]-t_mean)^2 +
                       alpha*(data_t[m]-t_mean)^2)
 sigma
}
sample_var <- Vectorize(sample_var, vectorize.args = "alpha")

    conf_int <- function(data,alpha){
      a <- floor(alpha * n)
      n <- length(data)
      df <- n-2*a-1
      data_t <- data[a:(n-a)]
      t_mean <- mean(data_t)
      t_quantile <- qt(p = alpha, df = df)
      sw <- sample_var(data = data, alpha = alpha)
      ul <- t_mean + t_quantile * sw / ((1-2*alpha)*sqrt(n))
      ll <- t_mean - t_quantile * sw / ((1-2*alpha)*sqrt(n))
     c(ll, ul)
    }

Maybe someone sees the error?也许有人看到错误?

EDIT: Here I tried to construct the intervals using wilcox.test function, but I don't know whether it accurately constructs the interval for the trimmed mean.编辑:在这里,我尝试使用 wilcox.test 函数构造区间,但我不知道它是否准确地构造了修剪均值的区间。 Furthermore, no matter which alpha I use, for the given data set, I get the same interval.此外,无论我使用哪个 alpha,对于给定的数据集,我都会得到相同的间隔。 So, I suppose that the subset argument is wrong.所以,我认为子集论点是错误的。

set_seed(1)
data <- rnorm(100)
wilcox_test <- function(data, alpha){
  n <- length(alpha)
  a <- floor(alpha*n)+1
  b <- n-floor(alpha)
  wilcox.test(data, subset = data[a:b], conf.int = TRUE)
}

OK...with rnorm(100) and set.seed(1)好的...使用 rnorm(100) 和 set.seed(1)

Close-ish...近乎...

set.seed(1)  # note set.seed() is what you want here, I think.
data <- rnorm(100)
wilcox_test_out <- wilcox.test(data, subset = data[a:b], conf.int = .95)
summary(wilcox_test_out)

# Note the CI's are in wilcox_test_out$conf.int for further use should you need them
wilcox_test_out$conf.int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM