1

I have this data:

df <- structure(list(`Orientación dicotómica` = c("Neurogastro", "Neurogastro", 
"Neurogastro", "Neurogastro", "No neurogastro", "No neurogastro", 
"No neurogastro", "No neurogastro", "No neurogastro"), `Fisiopatología más frecuente variante constipación` = c("Más de una variante", 
"Obstrucción del tracto de salida", "Tránsito lento / Inercia", 
"Transito normal", "Más de una variante", "Obstrucción del tracto de salida", 
"Tránsito lento / Inercia", "Transito normal", "Uso de fármacos"
), n = c(22L, 8L, 12L, 11L, 108L, 12L, 101L, 25L, 1L), Proporcion = c(41.5, 
15.1, 22.6, 20.8, 43, 4.8, 40.2, 10, 0.4), ds = c(11.5, 11.5, 
11.5, 11.5, 19.6, 19.6, 19.6, 19.6, 19.6), IC25 = c(32, 5.6, 
13.1, 11.3, 29.8, -8.4, 27, -3.2, -12.8), IC75 = c(51, 24.6, 
32.1, 30.3, 56.2, 18, 53.4, 23.2, 13.6)), row.names = c(NA, -9L
), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`10` = 10L), class = "omit"))

Which looks like this:

enter image description here

And I'm trying to run a chisq test with grouped data to analyse if there's a statistically significant difference between "neurogastro" and "no neurogastro" regarding the "fisiopatología más frecuente".

As published in Chi -Square test with grouped data in dplyr

I have tried

test <- df %>%
   group_by(`Fisiopatología más frecuente variante constipación`) %>%
   summarise(pval = chisq.test(Proporcion,`Orientación dicotómica`)$p.value)

But I get an error which I guess is because there is no "Uso de fármacos" for neurogastro. Is my approach ok? How can I run the test for the rest of the groups except the one missing?

Thanks!

Ale Rey
  • 75
  • 8
  • you could use `tryCatch` to handle the errors `pval = tryCatch(chisq.test(...), error = function(e) NA)` – rawr May 25 '21 at 15:49
  • thanks @rawr but I keep geting the same "Error: Problem with `summarise()` input `pval`. x 'x' and 'y' must have at least 2 levels i Input `pval` is `tryCatch(chisq.test(Proporcion, `Orientación dicotómica`)$p.value)`. i The error occured in group 5: Fisiopatología más frecuente variante constipación = "Uso de fármacos"." – Ale Rey May 25 '21 at 21:26
  • you didn't add the `error = ...` part – rawr May 25 '21 at 21:49
  • I did. The code was `test <- fisiopato_constipacion_especialidad %>% group_by(`Fisiopatología más frecuente variante constipación`) %>% summarise(pval = tryCatch(chisq.test(Proporcion,`Orientación dicotómica`)$p.value), error = function(e) NA)`. The output is above... – Ale Rey May 25 '21 at 22:41
  • your parentheses are not in the correct place `summarise(pval = tryCatch(chisq.test(...)$p.value, error = function(e) NA))` – rawr May 26 '21 at 04:23

1 Answers1

2

Try using xtabs:

tbl <- xtabs(n ~ `Fisiopatología más frecuente variante constipación` + `Orientación dicotómica`, df)
tbl
#                                                   Orientación dicotómica
# Fisiopatología más frecuente variante constipación Neurogastro No neurogastro
#                   Más de una variante                       22            108
#                   Obstrucción del tracto de salida           8             12
#                   Tránsito lento / Inercia                  12            101
#                   Transito normal                           11             25
#                   Uso de fármacos                            0              1
chisq.test(tbl)
# 
#   Pearson's Chi-squared test
# 
# data:  tbl
# X-squared = 15.092, df = 4, p-value = 0.004514
# 
# Warning message:
# In chisq.test(tbl) : Chi-squared approximation may be incorrect
chisq.test(tbl)$p.value
# [1] 0.00451449
# Warning message:
# In chisq.test(tbl) : Chi-squared approximation may be incorrect

The warning is produced because the last line in the table has very few observations/expected values. It may be better to simulate the p-value although in this case it does not seem to make a difference in the p-value:

chisq.test(tbl, simulate.p.value=TRUE)
# 
#   Pearson's Chi-squared test with simulated p-value (based on 2000 replicates)
# 
# data:  tbl
# X-squared = 15.092, df = NA, p-value = 0.004498

To get results for each row:

results <- apply(tbl, 1, chisq.test)
sapply(results, function(x) x$p.value)
#              Más de una variante Obstrucción del tracto de salida         Tránsito lento / Inercia                  Transito normal 
#                     4.603430e-14                     3.710934e-01                     5.644889e-17                     1.963066e-02 
#                  Uso de fármacos 
#                     3.173105e-01 

or

sapply(results, "[", "p.value")
# $`Más de una variante.p.value`
# [1] 4.60343e-14
# 
# $`Obstrucción del tracto de salida.p.value`
# [1] 0.3710934
# 
# $`Tránsito lento / Inercia.p.value`
# [1] 5.644889e-17
# 
# $`Transito normal.p.value`
# [1] 0.01963066
# 
# $`Uso de fármacos.p.value`
# [1] 0.3173105
dcarlson
  • 10,936
  • 2
  • 15
  • 18
  • thanks @dcarlson, I think this approximation could be usefull. However, Im trying to get a p-value for each row (comparing neurogastro vs no neurogatro). Is that possible? – Ale Rey May 25 '21 at 21:23
  • I've added code to get the results for each row. – dcarlson May 25 '21 at 22:10
  • I've tried the code but `results <- apply(tbl, 1, chisq.test)$p.value` gives NULL and `sapply(results, function(x) x$p.value)` gives list()... sorry to bother but I am new in the field – Ale Rey May 25 '21 at 22:38
  • 1
    I've fixed it: `results <- apply(tbl, 1, chisq.test)`. – dcarlson May 25 '21 at 22:39