Skip to content

Conversation

@mbannick
Copy link
Owner

@mbannick mbannick commented Aug 6, 2025

  • Added log_ratio, log_odds_ratio, and odds_ratio as options for the built-in contrasts.
  • Give a warning if someone uses odds_ratio or ratio saying they should use the log options instead because they give better performance with variance estimation.
  • Give a warning if someone requests odds_ratio or log_odds_ratio and their estimates are outside of the 0-1 range since this would mean they probably didn't want something on the odds scale, since they clearly don't have a probability.
  • Added tests to check the above behavior.

@mbannick mbannick requested review from Copilot and tye27 August 6, 2025 22:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds new contrast functions for log ratios and log odds ratios to improve statistical analysis capabilities. The implementation includes appropriate warnings to guide users toward better variance estimation practices and to alert them when odds ratios may not be appropriate for their data.

  • Added log_ratio, log_odds_ratio, and odds_ratio contrast functions with corresponding Jacobian methods
  • Implemented warning system for ratio/odds ratio usage and invalid probability ranges
  • Added comprehensive tests for the new functionality and warning behaviors

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
R/robincar-contrast.R Implements new contrast functions, Jacobian methods, and warning logic
tests/testthat/test-contrast.R Adds test coverage for warnings and validates log odds ratio calculations


#' @exportS3Method
jacobian.ODDSRATIO <- function(settings, est){
col1 <- -1 / est[1] ** 2 * est[-1] / (1-est[-1])
Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complex mathematical expression should be broken down or commented for clarity. Consider splitting into multiple lines or adding a comment explaining the mathematical formula being implemented.

Suggested change
col1 <- -1 / est[1] ** 2 * est[-1] / (1-est[-1])
# Compute the first column of the Jacobian for the odds ratio transformation.
# Formula: -1 / (est[1]^2) * est[-1] / (1 - est[-1])
base_sq <- est[1]^2
odds_cont <- est[-1] / (1 - est[-1])
col1 <- -1 / base_sq * odds_cont

Copilot uses AI. Check for mistakes.
#' @exportS3Method
jacobian.ODDSRATIO <- function(settings, est){
col1 <- -1 / est[1] ** 2 * est[-1] / (1-est[-1])
mat1 <- diag((1-est[-1]) / est[-1] / (1-est[1]) ** 2, length(est)-1)
Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complex mathematical expression should be broken down or commented for clarity. Consider splitting into multiple lines or adding a comment explaining the mathematical formula being implemented.

Suggested change
mat1 <- diag((1-est[-1]) / est[-1] / (1-est[1]) ** 2, length(est)-1)
# mat1 is the derivative of the odds ratio with respect to each comparison group.
# It is calculated as: (1 - est[-1]) / (est[-1] * (1 - est[1])^2)
numer <- (1 - est[-1])
denom <- est[-1] * (1 - est[1])^2
mat1 <- diag(numer / denom, length(est)-1)

Copilot uses AI. Check for mistakes.
if(class(settings)[2] %in% c("ODDSRATIO", "RATIO")){
warning("Using a ratio or odds ratio is not recommended.
Consider using log_ratio or log_odds_ratio for better
performance of the variance estimator.")
Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning message has inconsistent indentation and formatting. The multi-line string should be properly formatted, either as a single line or with consistent indentation.

Suggested change
performance of the variance estimator.")
warning("Using a ratio or odds ratio is not recommended. Consider using log_ratio or log_odds_ratio for better performance of the variance estimator.")

Copilot uses AI. Check for mistakes.

warning_txt <- "Using a ratio or odds ratio is not recommended.
Consider using log_ratio or log_odds_ratio for better
performance of the variance estimator."
Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning text has inconsistent formatting with extra whitespace and indentation that doesn't match the actual warning message in the source code. This could cause test failures if the formatting doesn't exactly match.

Suggested change
performance of the variance estimator."
warning_txt <- "Using a ratio or odds ratio is not recommended. Consider using log_ratio or log_odds_ratio for better performance of the variance estimator."

Copilot uses AI. Check for mistakes.
test_that("Compare log odds ratio SEs to GLM", {
# We want these to give warnings when someone
# specifies that they want a log odds ratio.

Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is incorrect for this test. This test doesn't check for warnings - it compares log odds ratio estimates to GLM output. The comment appears to be copied from the previous test.

Suggested change
# Compare log odds ratio estimates from robincar_glm to standard GLM output.

Copilot uses AI. Check for mistakes.
@mbannick mbannick merged commit 2a04a5e into main Aug 15, 2025
2 checks passed
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.

2 participants