Joshua Onyango
Joshua Onyango

Reputation: 35

Error on simple chi-square test

I am try to carry out chi-square test to see if there is a significant difference in disease proportion between regions but I end up with error in R. Any suggestions on how to correct this error?

data:   

           E    NE  NW  SE  SW  EM  WM  YH
Cases     11    37  54  30  114 44  31  39
Non.cases 28    73  116 68  211 80  78  92

d=read.csv(file.choose(),header=T)
attach(d)
chisq.test(d)

Error in chisq.test(d) : 
  all entries of 'x' must be nonnegative and finite

Upvotes: 0

Views: 3212

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226332

Your problem must be somewhere upstream of the chi-squared test, i.e. the data are getting mangled somehow when being read in.

d <- read.table(header=TRUE,text="
               E    NE  NW  SE  SW  EM  WM  YH
    Cases     11    37  54  30  114 44  31  39
    Non.cases 28    73  116 68  211 80  78  92")

However you read the data, results should look like this:

str(d)
## 'data.frame':    2 obs. of  8 variables:
##  $ E : int  11 28
##  $ NE: int  37 73
## ... etc.

chisq.test(d)
##  Pearson's Chi-squared test
## data:  d
## X-squared = 3.3405, df = 7, p-value = 0.8518

(attach() is not necessary, and usually actually harmful/confusing ...)

Upvotes: 3

Related Questions