Tidy way to do multiple chisq tests

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Tidy way to do multiple chisq tests

Mark Matsche
Hi!  I'm very new to the world of r.  I have the following test data:

A<tibble(parasite=sample(0:1,10,rep=TRUE),L1=sample(0:1,10,rep=TRUE),L2=sample(0:1,10,rep=TRUE),L3=sample(0:1,10,rep=TRUE),
          L4=sample(0:1,10,rep=TRUE))

Looks like:
   parasite L1 L2 L3 L4
1         0  0  1  0  0
2         1  0  1  1  1
3         1  1  1  0  1
4         0  1  1  1  0
5         1  1  1  1  0
...10 rows total

What I want to do is to run 4 chisq tests:
parasite vs L1
parasite vs L2
parasite vs L3
parasite vs L4

I want to then produce a summary tibble that lists the Y component of each table (L1,L2...), chisq values, and pvalues (rounded to a reasonable extent) of the tests.  Like:

variable    chisq      pvalue
L1            1.475      0.0892
L2            18.453    0.0000E8
L3            2.4781    0.0012
L4            0.6785    0.2755

I've seen the use of map() to do something similar but I cant get it to work.

e.g.  map(~chisq.test(.x, data$column)) %>%
  tibble(names = names(.), data = .) %>%
  mutate(stats = map(data, tidy))
unnest(data,stats)

Can anyone show me how to do this?

Thanks!