【发布时间】: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-squared是NaN和p-value是NA。这是因为一些预期计数为零。 -
@DarrenTsai 一旦 OP 确认它是题外话,我将删除我的答案
标签: r chi-squared