3

Is it possible to calculate chi squared in R when your data is in the form of a list of observations? What I mean is, it is simple to get chi squared if you know the cross. For instance, if you have a survey and you ask for gender and a true-false question, you only need four numbers to calculate the chi squared. What I have instead is two columns of data with each respondent's answers. Is it possible to get chi squared from this structure of the data, or do I have to convert it?

If I have to convert it for R, does anyone know of another language that will allow me to get the chi squared directly?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2047228
  • 73
  • 1
  • 3
  • 10
  • 2
    If you've got the "raw" data, you'll have to compute the cross tabulation, for example with the `table` function. That's the most usual way a chi-squared test is done I think. – juba Mar 09 '13 at 22:57

1 Answers1

5

If you use table before putting your data into chisq.test you should be fine

# Create some fake 'raw' data
dat <- data.frame(gender = sample(c("M","F"), 100,rep = T), ans = as.logical(rbinom(100,1,.3)))
head(dat)
# just use table to get the data into the form needed
chisq.test(table(dat))
Dason
  • 60,663
  • 9
  • 131
  • 148
  • awesome, thanks guys. related question - is there a command to cycle through the columns and calculate the chi square for one column against all the others? or even better, for every column against the others? or, do you need to use loops? – user2047228 Mar 10 '13 at 00:08
  • 1
    @user2047228 `combn(names(dat),2,FUN=function(x) {idx=c(x); print(idx); chisq.test(table(dat[,idx]))})` – topchef Mar 10 '13 at 00:43
  • @topchef I'm not sure that code does what it is supposed to do. – Gimelist Oct 16 '14 at 07:49
  • give it a try and report any problems pls. – topchef Oct 16 '14 at 17:32