-
Notifications
You must be signed in to change notification settings - Fork 3
Added log ratio and log odds ratio contrasts #35
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,12 +5,30 @@ diff <- function(est){ | |||||||||||||
| return(cont) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| odds_ratio <- function(est){ | ||||||||||||||
| base <- est[1] / (1-est[1]) | ||||||||||||||
| cont <- est[-1] / (1-est[-1]) / base | ||||||||||||||
| return(cont) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| log_odds_ratio <- function(est){ | ||||||||||||||
| base <- est[1] / (1-est[1]) | ||||||||||||||
| cont <- log(est[-1] / (1-est[-1]) / base) | ||||||||||||||
| return(cont) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| ratio <- function(est){ | ||||||||||||||
| base <- est[1] | ||||||||||||||
| cont <- est[-1] / base | ||||||||||||||
| return(cont) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| log_ratio <- function(est){ | ||||||||||||||
| base <- est[1] | ||||||||||||||
| cont <- log(est[-1] / base) | ||||||||||||||
| return(cont) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| jacobian <- function (settings, est) { | ||||||||||||||
| UseMethod("jacobian", settings) | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -30,6 +48,30 @@ jacobian.RATIO <- function(settings, est){ | |||||||||||||
| return(jacob) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #' @exportS3Method | ||||||||||||||
| jacobian.LOGRATIO <- function(settings, est){ | ||||||||||||||
| col1 <- -1 / est[1] | ||||||||||||||
| mat1 <- diag(1/est[-1], length(est)-1) | ||||||||||||||
| jacob <- cbind(col1, mat1) | ||||||||||||||
| return(jacob) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #' @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) | ||||||||||||||
|
||||||||||||||
| 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
AI
Aug 6, 2025
There was a problem hiding this comment.
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.
| 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.") |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,3 +26,93 @@ test_that("Test contrast function ordering", { | |||||
| expect_equal(as.vector(fit.anova$contrast$result$estimate[1]), anova) | ||||||
|
|
||||||
| }) | ||||||
|
|
||||||
| test_that("Test warnings for ratio and odds ratio functions", { | ||||||
|
|
||||||
| data <- RobinCar:::data_sim %>% | ||||||
| # recode A from 0 to "a", 1 to "b", 2 to "c" | ||||||
| mutate(A=factor(A, levels=0:2, labels=c("a", "b", "c"))) %>% | ||||||
| mutate(y_bin=ifelse(y > mean(y), 1, 0)) # create binary outcome | ||||||
|
|
||||||
| 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." | ||||||
|
||||||
| 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
AI
Aug 6, 2025
There was a problem hiding this comment.
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.
| # Compare log odds ratio estimates from robincar_glm to standard GLM output. |
There was a problem hiding this comment.
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.