0

I am trying to use a chi-square test with a crosstab of different survey answers. The answers are categorical but they are represented by numbers.

Below is an example:

         1  2   3   4   
    1   368 768 346 155 
    2   213 598 286 140 
    3   528 2047 1293 501 
    4   910 2953 1764 806 
    5   1579 7448 7489 4259 
    6   961 4851 6481 7944 

I have tried several examples of the chisq.test but have encountered error messages every time. I think ideally I would like to run the test in a loop for each possibility (see below code).

CHIS <- lapply(df[,-1], function(x) chisq.test(Q5_Q8.1[,1], x))

With this code above I got the following error: "Chi-squared approximation may be incorrect."

I'm still new to R and still learning how to do a lot of statistics, so any help in the best way to approach this would be great. Thanks ahead of time!

deschen
  • 10,012
  • 3
  • 27
  • 50
Molly
  • 5
  • 1

1 Answers1

0

The chi-squared test is in fact two different types of tests. One is the goodness-of-fit test, which needs two variables or a variable and a distribution. This is the test you are conducting with the question's code.

But you are asking for a crosstab test. Then pass only a table with 2 columns.

CHIS <- lapply(seq_along(Q5_Q8.1)[-1], function(i) 
  chisq.test(Q5_Q8.1[c(1, i)]))

Data

Q5_Q8.1 <-
structure(list(`1` = c(368L, 213L, 528L, 910L, 1579L, 961L), 
    `2` = c(768L, 598L, 2047L, 2953L, 7448L, 4851L), `3` = c(346L, 
    286L, 1293L, 1764L, 7489L, 6481L), `4` = c(155L, 140L, 501L, 
    806L, 4259L, 7944L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 1
    This worked! And to answer your question the df = Q5_Q8.1, I just forgot to put df instead of the vector name in my question. Thank you so much for your response! – Molly Nov 07 '21 at 16:02