Chisq.test. Error in if (any(x < 0) || anyNA(x)) stop("all entries of 'x' must be nonnegative and finite") : missing value where TRUE/FALSE needed

Hi all,

Question linked to chisq.test question. I'm trying to run chisq-square goodness of fit on the following data:

> table (Group4_subset$Mood.Change)

      Declined       Improved Neg_maintained Pos_maintained 
             3             57             35             53 

I tried running:

> chisq.test(Group4_subset$Mood.Change, p = c(1/4, 1/4, 1/4, 1/4)

But I get this error:

Error in if (any(x < 0) || anyNA(x)) stop("all entries of 'x' must be nonnegative and finite") :  
 missing value where TRUE/FALSE needed 
In addition: Warning message: 
In Ops.factor(x, 0) : ‘<’ not meaningful for factors 

How would I rectify this? Also, it would be great if someone can address my question in my linked post.

Thanks!

It looks like the column that you are passing to chisq.test() is a factor. You have to give the test numeric data of the counts in each category, the data produced by the table() function. In the code below, I reproduce your error and then successfully do a test using the result of table().

#invent data
Moods <- c("Declined","Improved","Neg","Pos")
DF <- data.frame(Mood=sample(Moods,100,replace=TRUE),stringsAsFactors = TRUE)
table(DF$Mood)
#> 
#> Declined Improved      Neg      Pos 
#>       28       22       27       23
chisq.test(DF$Mood)
#> Warning in Ops.factor(x, 0): '<' not meaningful for factors
#> Error in if (any(x < 0) || anyNA(x)) stop("all entries of 'x' must be nonnegative and finite"): missing value where TRUE/FALSE needed

chisq.test(table(DF$Mood))
#> 
#>  Chi-squared test for given probabilities
#> 
#> data:  table(DF$Mood)
#> X-squared = 1.04, df = 3, p-value = 0.7916

Created on 2022-04-05 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.