I have a one-dimensional matrix and I want to run a goodness of fit test. (See sample data bellow)

I have tried to run this test using chisq.test() in the tbl_summary() but it always gives me a different answer than when I create my own matrix or do the calculation by hand.

The tbl_summary code I use is:

tbl4 <- DentalHealth %>% select(Race, PG_Level)tbl4 %>%tbl_summary(by = Race,value = list(PG_Level ~ "PG>10000"),label = list(PG_Level ~ "PG>10000"),statistic = list(all_categorical() ~ c("{n}")),missing = "no") %>%add_p(everything() ~ "chisq.test", pvalue_fun = function(x) style_number(x, digits = 3)) %>%modify_header(label ~ "**Race groups**") %>%modify_caption("**Table 3.b Detection rates of PG greater than >10,000 cells in different races**") %>%bold_labels()

This table summary returns a p-value of 0.114. This is incorrect by my own manual calculations.

The table looks something like this:

AACAHAp-value15718.0114

When I run the test as below, I get the correct output:

observed <- c(15, 7, 18) expected <- c(1/3, 1/3, 1/3)chisq.test(x=observed, p=expected)

output is correct:

> Chi-squared test for given probabilities> > data: observed> X-squared = 4.85, df = 2, p-value = 0.08848

How can I get the correct p-value of 0.08848 to show up in my table?


Data:

structure(list(Race = c("HA", "CA", "HA", "CA", "HA", "HA", "HA", "HA", "AA", "CA", "AA", "CA", "CA", "HA", "AA", "AA", "CA", "HA", "HA", "HA", "HA", "AA", "CA", "AA", "CA", "HA", "HA", "AA", "HA", "AA", "HA", "HA", "HA", "AA", "HA", "CA", "AA", "HA", "CA", "CA", "AA", "HA", "HA", "AA", "HA", "CA", "AA", "AA", "CA", "CA", "HA", "CA", "CA", "HA", "HA", "CA", "HA", "AA", "HA", "AA", "AA", "CA", "AA", "CA", "AA", "CA", "CA", "HA", "AA", "AA", "CA", "CA", "CA", "CA", "AA", "HA", "AA", "HA", "HA", "CA", "AA", "HA", "HA", "AA", "CA", "CA", "HA", "CA", "CA", "HA", "AA", "CA", "CA", "HA", "HA", "CA", "CA", "CA", "HA", "AA", "CA", "AA", "HA", "HA", "AA", "HA", "AA", "HA", "AA", "CA", "CA", "CA", "CA", "CA", "CA", "HA", "HA", "HA", "AA", "HA", "HA", "AA", "CA", "AA", "HA", "CA", "HA", "CA", "HA", "CA", "AA", "AA", "AA", "HA", "HA", "AA", "CA", "CA", "HA", "HA", "HA", "CA", "HA", "AA", "CA", "HA", "CA", "HA", "AA", "CA", "AA", "AA", "CA", "HA", "CA", "CA", "CA", "CA", "HA", "CA", "CA", "CA", "AA", "CA", "CA", "HA", "CA", "CA", "HA", "CA", "CA", "AA", "CA", "AA", "CA", "HA", "HA", "CA", "HA", "CA", "HA", "AA", "CA", "CA", "HA", "AA", "CA", "HA", "CA", "CA", "AA", "HA", "HA", "HA", "HA", "HA", "HA", "AA", "HA", "CA", "HA", "HA", "CA", "HA", "CA", "CA", "HA", "AA", "CA", "CA", "CA", "CA", "HA", "HA", "AA", "HA", "HA", "AA", "HA", "HA", "AA", "HA", "AA", "HA", "HA", "CA", "CA", "CA", "HA", "AA", "HA", "CA", "AA", "AA", "HA", "CA", "HA", "AA", "HA", "CA", "HA", "HA", "AA", "CA", "CA", "CA", "CA", "HA", "HA", "CA", "HA", "CA", "CA", "AA", "HA", "HA", "HA", "HA", "HA", "HA", "CA", "HA", "HA", "CA", "CA", "AA", "CA", "HA", "CA", "CA", "HA", "HA", "CA", "CA", "CA", "CA", "CA", "HA", "CA", "CA", "CA", "HA", "HA", "HA", "HA", "HA", "CA", "CA", "HA", "CA", "AA", "HA", "AA", "AA", "HA", "HA", "AA", "HA", "CA", "CA", "HA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA"), PG_Level = c("PG>10000", "0", "0", "0", "PG>10000", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "PG1~1000", "0", "0", "0", "0", "PG1001~10000", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "PG1~1000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "PG1001~10000", "0", "0", "PG1001~10000", "0", "0", "0", "0", "0", "0", "PG>10000", "PG>10000", "PG>10000", "0", "0", "PG>10000", "0", "PG>10000", "PG>10000", "0", "PG1~1000", "0", "0", "PG>10000", "0", "0", "0", "0", "0", "PG1~1000", "PG>10000", "0", "0", "0", "PG1~1000", "PG1~1000", "0", "PG1~1000", "PG1~1000", "PG1~1000", "0", "0", "0", "0", "0", "0", "0", "0", "PG1~1000", "0", "0", "0", "0", "0", "0", "0", "PG1001~10000", "0", "PG1001~10000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "PG1001~10000", "0", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "0", "PG>10000", "0", "0", "0", "PG1~1000", "0", "0", "0", "0", "PG1001~10000", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "PG1001~10000", "PG>10000", "0", "0", "0", "PG>10000", "0", "PG1~1000", "0", "PG1~1000", "0", "PG>10000", "PG1~1000", "0", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "PG1001~10000", "0", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "0", "PG1001~10000", "0", "0", "PG1~1000", "0", "0", "0", "0", "PG1~1000", "0", "0", "PG>10000", "PG1~1000", "0", "0", "0", "PG1~1000", "0", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "0", "PG>10000", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "PG1~1000", "PG>10000", "0", "0", "0", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "PG1001~10000", "PG>10000", "0", "0", "PG>10000", "0", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "PG>10000", "0", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "0", "0", "PG1001~10000", "0", "0", "0", "PG1001~10000", "0", "0", "0", "0", "0", "0", "PG1001~10000", "PG1001~10000", "PG1~1000", "0", "0", "PG1001~10000", "0", "0", "0", "PG1~1000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "PG>10000", "0", "0", "0", "PG1001~10000", "0", "0", "PG1~1000", "PG>10000", "0", "0", "0", "0", "0", "0", "PG1001~10000", "0", "0", "0", "0", "0", "PG>10000", "0", "0", "0", "0", "0", "0", "PG>10000")), class = "data.frame", row.names = c(NA, -340L))
1

Best Answer


As you know, the Chi-Square test is a statistical method that can be used to determine if two categorical variables are independent to each other.

So, in your 1st example, you are trying to see if the Race is independent to the PG_Level.

In your code above, and more precisely in the summary table you show, you only print the rows when PG_Level is "PG>10000". However, the "chisq.test" compare the full Racecolumn against the full PG_Level column.

Let us illustrate this with this example: I took the same code as before while showing all values.

DentalHealth %>% select(Race, PG_Level) %>%tbl_summary(by = Race, missing = "no") %>%add_p(everything() ~ "chisq.test", pvalue_fun = function(x) style_number(x, digits = 3)) 

which gives us:

enter image description here

As you can see, we have the same p-value here and we print all the values that are fount in the PG_Level columns.

And you can optain the same results when you write:

chisq.test(x = DentalHealth$Race, y = DentalHealth$PG_Level)

Pearson's Chi-squared test

data: DentalHealth$Race and DentalHealth$PG_Level X-squared = 10.255,df = 6, p-value = 0.1143

I have the impression that what you are trying to do is to see if there are significant differences in the Race when PG_Level is "PG>10000". That will not be possible with a Chi-squared test.