AndriSignorell/DescTools

GTest should fail with wrong values of argument `p` (and add `rescale_p`)

GegznaV opened this issue · 3 comments

Consider the following examples:

expected <- c(1, 1/2, 1)
empirical <- c(152, 68, 124)


# When expected probabilities do not add up to 1: 

# DescTools → wrong p-value
DescTools::GTest(empirical, p = expected)
#> 
#>  Log likelihood ratio (G-test) goodness of fit test
#> 
#> data:  empirical
#> G = -627.55, X-squared df = 2, p-value = 1

 # Base R→ error
chisq.test(empirical, p = expected)
#> Error in chisq.test(empirical, p = expected): probabilities must sum to 1.

# When expected probabilities add up to 1:
DescTools::GTest(empirical, p = expected/sum(expected))
#> 
#>  Log likelihood ratio (G-test) goodness of fit test
#> 
#> data:  empirical
#> G = 2.8571, X-squared df = 2, p-value = 0.2397

Created on 2023-01-12 with reprex v2.0.2

Next, chisq.test() has the argument rescale.p

rescale.p a logical scalar; if TRUE then p is rescaled (if necessary) to sum to 1. If rescale.p is FALSE, and p does not sum to 1, an error is given.

For consistency with base R, GTest() should also have this argument (either rescale_p or rescale.p ).

Great idea, already done! I will republish in the next days...