-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add the wrappers for glm * [skip style] [skip vbump] Restyle files * Empty * update lint * update wordlist * [skip style] [skip vbump] Restyle files * update docs and namespace --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
419c99d
commit f9d5c12
Showing
10 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ Depends: | |
Imports: | ||
checkmate, | ||
numDeriv, | ||
prediction, | ||
sandwich, | ||
stats | ||
Suggests: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#' Covariate adjusted glm model | ||
#' @param formula (`formula`) A formula of analysis. | ||
#' @param data (`data.frame`) Input data frame. | ||
#' @param treatment (`formula` or `character(1)`) A formula of treatment assignment or assignment by stratification, | ||
#' or a string name of treatment assignment. | ||
#' @param contrast (`function` or `character(1)`) A function to calculate the treatment effect, or character of | ||
#' "difference", "risk_ratio", "odds_ratio" for default contrasts. | ||
#' @param contrast_jac (`function`) A function to calculate the Jacobian of the contrast function. Ignored if using | ||
#' default contrasts. | ||
#' @param vcov (`function`) A function to calculate the variance-covariance matrix of the treatment effect, | ||
#' including `vcovHC` and `vcovANHECOVA`. | ||
#' @param family (`family`) A family object of the glm model. | ||
#' @param ... Additional arguments passed to `vcov`. For finer control of glm, refer to usage of `treatment_effect`, | ||
#' `difference`, `risk_ratio`, `odds_ratio`. | ||
#' @export | ||
#' @examples | ||
#' robin_glm(y ~ treatment * s1, data = dummy_data, treatment = treatment ~ s1, contrast = "difference") | ||
robin_glm <- function( | ||
formula, data, treatment, contrast = "difference", | ||
contrast_jac = NULL, vcov = vcovANHECOVA, family = gaussian, ...) { | ||
attr(formula, ".Environment") <- environment() | ||
fit <- glm(formula, family = family, data = data) | ||
pc <- predict_counterfactual(fit, treatment, data, unbiased = TRUE) | ||
has_interaction <- h_interaction(formula, treatment) | ||
if (has_interaction && identical(vcov, vcovHC) && !identical(contrast, "difference")) { | ||
stop( | ||
"Huber-White standard error only works for difference contrasts in models without interaction term." | ||
) | ||
} | ||
if (identical(contrast, "difference")) { | ||
difference(pc) | ||
} else if (identical(contrast, "risk_ratio")) { | ||
risk_ratio(pc) | ||
} else if (identical(contrast, "odds_ratio")) { | ||
odds_ratio(pc) | ||
} else { | ||
assert_function(contrast) | ||
assert_function(contrast_jac, null.ok = TRUE) | ||
if (is.null(contrast_jac)) { | ||
contrast_jac <- function(x) { | ||
numDeriv::jacobian(contrast, x) | ||
} | ||
} | ||
treatment_effect(pc, eff_measure = contrast, eff_jacobian = contrast_jac, variance = vcov, ...) | ||
} | ||
} | ||
|
||
h_interaction <- function(formula, treatment) { | ||
assert_formula(formula) | ||
treatment <- h_get_vars(treatment) | ||
assert_subset(treatment$treatment, all.vars(formula[[length(formula)]])) | ||
tms <- terms(formula) | ||
fct <- attr(tms, "factors") | ||
any(fct[treatment$treatment, ] %in% c(1, 2) & colSums(fct != 0) > 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
ANHECOVA | ||
RobinCar | ||
glm | ||
jacobian | ||
jacobians | ||
pbo | ||
trt |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# h_interaction ---- | ||
|
||
test_that("h_interaction works correctly", { | ||
expect_false(h_interaction(y ~ trt + z, treatment = trt ~ x)) | ||
expect_true(h_interaction(y ~ trt:z, treatment = trt ~ x)) | ||
expect_true(h_interaction(trt * y ~ trt:z, treatment = trt ~ x)) | ||
expect_true(h_interaction(y ~ trt:z, treatment = "trt")) | ||
}) | ||
|
||
# robin_glm ---- | ||
|
||
test_that("robin_glm works correctly", { | ||
expect_silent( | ||
robin_glm( | ||
y ~ treatment * s1, | ||
data = dummy_data, treatment = treatment ~ s1, | ||
contrast = "difference", vcov = vcovHC | ||
) | ||
) | ||
expect_silent(robin_glm(y_b ~ treatment * s1, data = dummy_data, treatment = treatment ~ s1, contrast = "difference")) | ||
expect_silent(robin_glm(y_b ~ treatment * s1, data = dummy_data, treatment = treatment ~ s1, contrast = "risk_ratio")) | ||
expect_silent(robin_glm(y_b ~ treatment * s1, data = dummy_data, treatment = treatment ~ s1, contrast = "odds_ratio")) | ||
expect_error( | ||
robin_glm( | ||
y_b ~ treatment * s1, | ||
data = dummy_data, treatment = treatment ~ s1, | ||
contrast = "odds_ratio", vcov = vcovHC | ||
), | ||
"Huber-White standard error only works for difference contrasts in models without interaction term." | ||
) | ||
expect_silent(robin_glm(y_b ~ treatment * s1, data = dummy_data, treatment = treatment ~ s1, contrast = h_diff)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters