Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve numerical precision of several p-value calculations #1210

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

remlapmot
Copy link

Hi there,

This is relatively minor but I was looking at the source code for an unrelated reason and I noticed that in several places you have used the sub-optimal approach to calculating p-values.

You have coded 1 - ... instead of using the lower.tail = FALSE argument of the distribution functions. The 1 - ... calculation loses numerical precision.

Below are some examples which show this - admittedly you only see this with very large values of a z/t/X^2/F statistic, but it is probably worth using the more precise calculation. For example, for a z-test the 1 - ... calculation reports 0 for approx. abs(Z) > 8.3 , whereas the lower.tail = FALSE calculation will give you a p-value up to abs(Z) approx 37.5 (which is approx abs(qnorm(.Machine$double.xmin / 2))).

statistic <- -7.5
2 * (1 - stats::pnorm(abs(statistic)))
#> [1] 6.37268e-14
2 * (stats::pnorm(abs(statistic), lower.tail = FALSE))
#> [1] 6.381783e-14

statistic <- -8.3
2 * (1 - stats::pnorm(abs(statistic)))
#> [1] 0
2 * (stats::pnorm(abs(statistic), lower.tail = FALSE))
#> [1] 1.041114e-16

statistic <- -37.5 # approx qnorm(.Machine$double.xmin / 2)
2 * (1 - stats::pnorm(abs(statistic)))
#> [1] 0
2 * (stats::pnorm(abs(statistic), lower.tail = FALSE))
#> [1] 9.210706e-308

Created on 2024-07-22 with reprex v2.1.1

Also,

  • In the lfe-tidiers.R commit I added what I think is a missing abs() around the statistic
  • I added a missing stats:: qualifier in a few places.

Tom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant