Chi -Square test for grouped data excepting certain groups with missing values in r

302 Views Asked by At

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!

1

There are 1 best solutions below

4
On BEST ANSWER

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