between and within subject comparisons on categorical data in R

Viewed 25

I have more like a statistical question... I have a data frame like this:

     ID diagnosis   Q1   Q2   Q3   Q4
1      x       yes    A    D    B    B
2      y        no    B    D    B    A
3      z       yes    A    D    C    C
4     ad       yes <NA>    C    A    C
5   tgfg       yes    C    E <NA>    C
6   gfgh        no    C <NA>    A    C
7    asj       yes    D    A    B    D
8     gh        no    A    A    D    B
9    sdf        no    B    A    E <NA>
10 asdgz        no    D    A    B    A

Here Q1 to Q4 correspond to the questions in my test that I applied to participants(in real data, I have 30 questions). The letters below represent the options they choose. My questions actually have "right" answers. But I also want to analyze whether diagnosed and healthy group differs in their choice of a specific option and whether are there within-group differences across questions in my test. So, I want to analyze this as categorical data.

I first wanted to do multiple chi-squares for each question for diagnosed and undiagnosed groups but it gave an error:

mydf %>% 
  group_by(diagnosis, Q1) %>% 
  summarise(count = count(Q1)) %>% 
  summarise(pvalue= chisq.test(count)$p.value) 
Error in `summarise()`:
! Problem while computing `count =
  count(Q1)`.
i The error occurred in group 1: diagnosis =
  "no", Q1 = "A".
Caused by error in `UseMethod()`:
! no applicable method for 'count' applied to an object of class "character"
Run `rlang::last_error()` to see where the error occurred.

Sorry that I am not clear enough... In short, how can I compare the within and between groups' choices on the options in my test?

1 Answers

With regards to the between-group differences, the code might be:

require(tidyverse)
mydf <- tribble(
      ~ID, ~diagnosis, ~Q1,    ~Q2,    ~Q3,    ~Q4,
      "x",       T,    "A",    "D",    "B",    "B",
      "y",       F,    "B",    "D",    "B",    "A",
      "z",       T,    "A",    "D",    "C",    "C",
     "ad",       T,    NA,     "C",    "A",    "C",
   "tgfg",       T,    "C",    "E",    NA,     "C",
   "gfgh",       F,    "C",    NA,     "A",    "C",
    "asj",       T,    "D",    "A",    "B",    "D",
     "gh",       F,    "A",    "A",    "D",    "B",
    "sdf",       F,    "B",    "A",    "E",    NA,
  "asdgz",       F,    "D",    "A",    "B",    "A"
)

mydf <- mydf %>%
    mutate(count=1, Q1=as.factor(Q1), Q2=as.factor(Q2), Q3=as.factor(Q3), Q4=as.factor(Q4))

for (question in colnames(data)[3:length(colnames(data))]) {
    mydf %>%   
        select(diagnosis, all_of(question), count) %>%
        drop_na() %>%
        pivot_wider(names_from=diagnosis, values_from=count, values_fn=sum, values_fill=0) %>%
        select(2:3) %>%
        chisq.test() %>%
        .$p.value %>%
        sprintf(. , fmt = '%#.5f') %>%
        paste(question, . ) %>%
        print()
}

Regarding the potential within-group difference, I can't see a 2nd categorical dimension besides the question (Q1, Q2, ...), which is the 1st. Theoretically the options (A, B, ...) might be the 2nd dimension, but in that case the option A, for instance, should have meant more or less the same in every question, which is very unlikely in a clinical study or survey, so I don't assume this.

Related