【问题标题】:R Chi-square test error: 'x' and 'y' must have at least 2 levelsR 卡方检验误差:“x”和“y”必须至少有 2 个级别
【发布时间】:2020-08-02 12:20:13
【问题描述】:

我想知道当只进行一个级别的预测时是否有办法解决这个错误:

Error in stats::chisq.test(y[1:20], predictions[1:20]) : 
  'x' and 'y' must have at least 2 levels

chisq.test 中的所有预测都属于同一级别/类别时,我会得到它(即使变量有两个级别(尽管两者都不存在)。

测试数据:

y <- as.factor(c(rep(1, 10), rep(0, 11)))
predictions <- as.factor(c(rep(1, 20), 0))

# Works (with a warning). 
chisq <- stats::chisq.test(y, predictions)

# Does not work due to not having prediction of both factors. 
chisq <- stats::chisq.test(y[1:20], predictions[1:20])

(我想在函数中使用它;如果它不返回错误,我会更喜欢它,而是提供更多信息) 提前致谢

【问题讨论】:

  • 试试chisq.test(table(y[1:20], predictions[1:20]))。你会看到X-squaredNaNp-valueNA。这是因为一些预期计数为零。
  • @DarrenTsai 一旦 OP 确认它是题外话,我将删除我的答案

标签: r chi-squared


【解决方案1】:

考虑改用 Fisher 精确检验。

stats::fisher.test(y, predictions)

卡方检验的“经验法则”是它希望细胞计数为 5 或更多。 Fisher 精确法没有此限制,您可以在适合卡方检验的任何 2x2 表上使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2020-05-10
    • 2022-01-19
    • 2022-10-08
    相关资源
    最近更新 更多