R chisquare异常值测试while循环

R chisquare outlier test while loop

我是 R 的新手。我想做卡方离群值测试,在变量 x$indel 上使用离群值库,直到返回 p.value is > 0.01(从数据中删除离群值后) . 这是我尝试过的:

while(chisq.out.test(x$indel)$p.value < 0.01)
{
    # str: string contains the outlier value and some text 
    #   n: extract the outlier value and transform to numeric 
    str <- chisq.out.test(x$indel)$alternative
    print(str)

    n <- as.numeric(unlist(regmatches(str,
             gregexpr("[[:digit:]]+\.*[[:digit:]]*",str))))
    x <- x[x$indel < n,]
    print(nrow(x))
}

下面是 x$indel 列

    c(0.287749287749, 0.324786324786, 0.330484330484, 0.293447293447, 
0.293447293447, 0.31339031339, 0.31339031339, 0.327635327635, 
0.344729344729, 0.327635327635, 0.304843304843, 0.296296296296, 
0.433048433048, 0.700854700855, 0.467236467236, 0.31339031339, 
0.373219373219, 0.293447293447, 0.304843304843, 0.293447293447, 
0.407407407407, 0.301994301994, 0.307692307692, 0.301994301994, 
0.381766381766, 0.307692307692)

当我将此命令粘贴到控制台时没有任何反应,怎么了?

使用 "outliers"

生成一些数据
x = round(rnorm(100, 100, 100), 2)

将所有 x$indel 替换为 x。使用 data.frame 的问题在于,当您从列中删除值并尝试替换原始列表时,您会收到有关维度不匹配的投诉。

还改进了正则表达式以处理负数,并改进了子集逻辑以处理 "highest value" 和 "lowest value" 情况。

while(chisq.out.test(x)$p.value < 0.01)
{
  # str: string contains the outlier value and some text 
  #   n: extract the outlier value and transform to numeric 
  str <- chisq.out.test(x)$alternative
  print(str)
  n <- as.numeric(unlist(regmatches(str,
                                    gregexpr("(?<=value)(.*)(?=is an outlier)", str, perl = T))))
  x <- x[x != n]
  print(length(x))
}