Skip to content

Convert change in probability to probability / odds#676

Merged
mattansb merged 18 commits intomainfrom
new-prob-change-to-probs
Jul 25, 2025
Merged

Convert change in probability to probability / odds#676
mattansb merged 18 commits intomainfrom
new-prob-change-to-probs

Conversation

@mattansb
Copy link
Copy Markdown
Member

@mattansb mattansb commented Jul 19, 2025

Closes #660

  • NEWS / Version bump
    • we also dropped log(rr)
  • Make sure all oddsratio_to_<change>() functions work with a binomial logistic model.
  • Make sure all riskratio_to_<change>() functions work with a binomial log-link model.
  • Deprecate probs_to_odds(<data.frame>) and odds_to_probs(<data.frame>) - suggest something else instead.
  • Tests
  • Vignette?

@bwiernik
Copy link
Copy Markdown
Contributor

Maybe "risk" instead of "probs"

@mattansb
Copy link
Copy Markdown
Member Author

We already have odds_to_probs() - should that be renamed to odds_to_risk()?

@mattansb
Copy link
Copy Markdown
Member Author

m1 <- glm(am ~ factor(cyl),
          family = binomial("logit"),
          data = mtcars)

m2 <- glm(am ~ factor(cyl),
          family = binomial("log"),
          data = mtcars)

effectsize::oddsratio_to_arr(m1)
#> Warning: 'p0' not provided:
#>   Computing effect size relative to the intercept (p0 = 0.727);
#>   Make sure your intercept is meaningful.
#> Parameter |   ARR |         95% CI
#> ----------------------------------
#> (p0)      |  0.73 |               
#> cyl [6]   | -0.30 | [-0.65,  0.11]
#> cyl [8]   | -0.58 | [-0.71, -0.22]
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.
effectsize::riskratio_to_arr(m2)
#> Warning: 'p0' not provided:
#>   Computing effect size relative to the intercept (p0 = 0.727);
#>   Make sure your intercept is meaningful.
#> Parameter |   ARR |         95% CI
#> ----------------------------------
#> (p0)      |  0.73 |               
#> cyl [6]   | -0.30 | [-0.60,  0.23]
#> cyl [8]   | -0.58 | [-0.70, -0.30]
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.

effectsize::oddsratio_to_riskratio(m1)
#> Warning: 'p0' not provided:
#>   Computing effect size relative to the intercept (p0 = 0.727);
#>   Make sure your intercept is meaningful.
#> Parameter | Risk ratio |       95% CI
#> -------------------------------------
#> (p0)      |       0.73 |             
#> cyl [6]   |       0.59 | [0.11, 1.16]
#> cyl [8]   |       0.20 | [0.02, 0.70]
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.
parameters::model_parameters(m2, exp = TRUE)
#> Parameter   | Risk Ratio |   SE |       95% CI |     z |     p
#> --------------------------------------------------------------
#> (Intercept) |       0.73 | 0.13 | [0.44, 0.92] | -1.72 | 0.085
#> cyl [6]     |       0.59 | 0.28 | [0.17, 1.31] | -1.12 | 0.264
#> cyl [8]     |       0.20 | 0.13 | [0.03, 0.59] | -2.39 | 0.017
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.

effectsize::riskratio_to_oddsratio(m2)
#> Warning: 'p0' not provided:
#>   Computing effect size relative to the intercept (p0 = 0.727);
#>   Make sure your intercept is meaningful.
#> Parameter | Odds ratio |       95% CI
#> -------------------------------------
#> (p0)      |       0.73 |             
#> cyl [6]   |       0.28 | [0.05, 7.54]
#> cyl [8]   |       0.06 | [0.01, 0.28]
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.
parameters::model_parameters(m1, exp = TRUE)
#> Parameter   | Odds Ratio |   SE |        95% CI |     z |     p
#> ---------------------------------------------------------------
#> (Intercept) |       2.67 | 1.81 | [0.77, 12.17] |  1.45 | 0.147
#> cyl [6]     |       0.28 | 0.29 | [0.03,  1.99] | -1.24 | 0.214
#> cyl [8]     |       0.06 | 0.06 | [0.01,  0.39] | -2.72 | 0.007
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.

Created on 2025-07-19 with reprex v2.1.1

@bwiernik
Copy link
Copy Markdown
Contributor

What did you mean by?

Deprecate probs_to_odds(<data.frame>) and odds_to_probs(<data.frame>) - suggest something else instead.

@mattansb
Copy link
Copy Markdown
Member Author

There used to be a method for probs_to_odds() for converting selected columns in data frames between odds and probabilities, but that was using old {datawizard} selection syntax and was IMO a very specific use case that can easily be converted done with other tools. If a user now passes a data frame to probs_to_odds() they get this error:

Error: probs_to_odds(<data.frame>) is deprecated.
  You can use:
  datawizard::data_modify(data, .at = ..., .modify = probs_to_odds)
  Or
  dplyr::mutate(data, dplyr::across(..., probs_to_odds))
  Instead.

@mattansb mattansb requested a review from bwiernik July 22, 2025 11:52
Comment thread NEWS.md Outdated
Comment thread R/convert_between_riskchange.R Outdated
mattansb and others added 4 commits July 23, 2025 09:27
Co-authored-by: Brenton M. Wiernik <bwiernik@users.noreply.github.com>
Co-authored-by: Brenton M. Wiernik <bwiernik@users.noreply.github.com>
@mattansb mattansb merged commit 3974554 into main Jul 25, 2025
21 of 25 checks passed
@mattansb mattansb deleted the new-prob-change-to-probs branch July 25, 2025 19:20
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.

convert change in probability to probability

3 participants