John
John

Reputation: 403

R Chi-square test error: 'x' and 'y' must have at least 2 levels

I wonder if there is a way around this error when predictions of only one level has been made:

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

I get it when all predictions are of the same level/class in chisq.test (even though the variable has two levels (although both are not present).

Test data:

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])

(I want to use this in a function; and would prefer if it did not return an error, but rather something more informative) Thanks in advance

Upvotes: 1

Views: 7992

Answers (1)

itsMeInMiami
itsMeInMiami

Reputation: 2659

Consider using Fisher's Exact test instead.

stats::fisher.test(y, predictions)

The "rule of thumb" for a chi-squared test is that it would like to have expected cell counts of 5 or more. Fisher's exact does not have this restriction and you can use it on any 2x2 table where a chi-squared test would be appropriate.

Upvotes: 2

Related Questions