Skip to content

Commit

Permalink
Updated gvcov to vcovG (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbannick authored Nov 19, 2024
1 parent 0a3e44b commit eafee4c
Show file tree
Hide file tree
Showing 10 changed files with 635 additions and 27 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ S3method(treatment_effect,prediction_cf)
S3method(vcovHC,prediction_cf)
export(bias)
export(find_data)
export(gvcov)
export(h_diff)
export(h_jac_diff)
export(h_jac_odds_ratio)
Expand All @@ -22,6 +21,7 @@ export(h_ratio)
export(predict_counterfactual)
export(robin_glm)
export(treatment_effect)
export(vcovG)
import(checkmate)
importFrom(MASS,negative.binomial)
importFrom(numDeriv,jacobian)
Expand Down
4 changes: 2 additions & 2 deletions R/robin_glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @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 `gvcov`.
#' including `vcovHC` and `vcovG`.
#' @param family (`family`) A family object of the glm model.
#' @param vcov_args (`list`) Additional arguments passed to `vcov`.
#' @param ... Additional arguments passed to `glm` or `glm.nb`.
Expand All @@ -23,7 +23,7 @@
#' )
robin_glm <- function(
formula, data, treatment, contrast = "difference",
contrast_jac = NULL, vcov = "gvcov", family = gaussian(), vcov_args = list(), ...) {
contrast_jac = NULL, vcov = "vcovG", family = gaussian(), vcov_args = list(), ...) {
attr(formula, ".Environment") <- environment()
# check if using negative.binomial family with NA as theta.
# If so, use MASS::glm.nb instead of glm.
Expand Down
6 changes: 3 additions & 3 deletions R/treatment_effect.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ treatment_effect <- function(object, pair, variance, eff_measure, eff_jacobian,

#' @export
treatment_effect.prediction_cf <- function(
object, pair = names(object), variance = "gvcov", eff_measure, eff_jacobian, vcov_args = list(), ...) {
object, pair = names(object), variance = "vcovG", eff_measure, eff_jacobian, vcov_args = list(), ...) {
assert(
test_string(variance),
test_function(variance),
Expand Down Expand Up @@ -75,15 +75,15 @@ treatment_effect.prediction_cf <- function(
#' @export
#' @inheritParams predict_counterfactual
treatment_effect.lm <- function(
object, pair, variance = "gvcov", eff_measure, eff_jacobian,
object, pair, variance = "vcovG", eff_measure, eff_jacobian,
vcov_args = list(), treatment, data = find_data(object), ...) {
pc <- predict_counterfactual(object, data = data, treatment)
treatment_effect(pc, pair = pair, variance = variance, eff_measure = eff_measure, eff_jacobian = eff_jacobian, ...)
}

#' @export
treatment_effect.glm <- function(
object, pair, variance = "gvcov", eff_measure, eff_jacobian,
object, pair, variance = "vcovG", eff_measure, eff_jacobian,
vcov_args = list(), treatment, data = find_data(object), ...) {
pc <- predict_counterfactual(object, treatment, data)
treatment_effect(pc, pair = pair, variance = variance, eff_measure = eff_measure, eff_jacobian = eff_jacobian, ...)
Expand Down
2 changes: 1 addition & 1 deletion R/variance_anhecova.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#'
#' @return Named covariance matrix.
#' @export
gvcov <- function(x, decompose = TRUE, ...) { # nolint
vcovG <- function(x, decompose = TRUE, ...) { # nolint
assert_class(x, "prediction_cf")
assert_flag(decompose)
resi <- attr(x, "residual")
Expand Down
4 changes: 2 additions & 2 deletions man/robin_glm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/gvcov.Rd → man/vcovG.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

608 changes: 608 additions & 0 deletions tests/testthat/_snaps/bias.new.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/treatment_effect.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
0.03359913 0.03441801 0.03401864
Variance Type: gvcov
Variance Type: vcovG
Estimate Std.Err Z Value Pr(>|z|)
trt1 - pbo 0.2246 0.0477 4.71 2.5e-06 ***
trt2 - pbo 0.2653 0.0475 5.58 2.4e-08 ***
Expand Down Expand Up @@ -47,7 +47,7 @@
0.06768998 0.07592944 0.07654319
Variance Type: gvcov
Variance Type: vcovG
Estimate Std.Err Z Value Pr(>|z|)
trt1 - pbo 0.564 0.101 5.60 2.2e-08 ***
trt2 - pbo 0.771 0.101 7.61 2.8e-14 ***
Expand Down Expand Up @@ -98,7 +98,7 @@
0.03359913 0.03441801
Variance Type: gvcov
Variance Type: vcovG
Estimate Std.Err Z Value Pr(>|z|)
trt1 - pbo 0.2246 0.0477 4.71 2.5e-06 ***
---
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/_snaps/variance.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
trt1 4.523445e-07 1.164889e-03 -7.709031e-07
trt2 -9.709004e-06 -7.709031e-07 1.170214e-03

# gvcov works
# vcovG works

Code
gvcov(pc)
vcovG(pc)
Output
pbo trt1 trt2
pbo 1.128902e-03 1.856234e-05 1.333885e-05
Expand All @@ -31,7 +31,7 @@
---

Code
gvcov(pc)
vcovG(pc)
Output
pbo trt1 trt2
pbo 1.128902e-03 1.856234e-05 1.333885e-05
Expand All @@ -41,7 +41,7 @@
---

Code
gvcov(pc)
vcovG(pc)
Output
pbo trt1 trt2
pbo 1.128902e-03 1.856234e-05 1.333885e-05
Expand All @@ -51,7 +51,7 @@
---

Code
gvcov(pc, decompose = FALSE)
vcovG(pc, decompose = FALSE)
Output
pbo trt1 trt2
pbo 1.127076e-03 1.856234e-05 1.333885e-05
Expand All @@ -61,7 +61,7 @@
---

Code
gvcov(pc)
vcovG(pc)
Output
pbo trt1 trt2
pbo 1.128902e-03 1.856234e-05 1.333885e-05
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-variance.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ test_that("vcovHC works", {
)
})

test_that("gvcov works", {
test_that("vcovG works", {
pc <- predict_counterfactual(fit_binom, treatment ~ s1)
expect_snapshot(
gvcov(pc)
vcovG(pc)
)
pc <- predict_counterfactual(fit_binom, treatment ~ 1)
expect_snapshot(
gvcov(pc)
vcovG(pc)
)
pc <- predict_counterfactual(fit_binom, treatment ~ pb(s1))
expect_snapshot(
gvcov(pc)
vcovG(pc)
)
expect_snapshot(
gvcov(pc, decompose = FALSE)
vcovG(pc, decompose = FALSE)
)
pc <- predict_counterfactual(fit_binom, treatment ~ ps(s1))
expect_snapshot(
gvcov(pc)
vcovG(pc)
)
})

0 comments on commit eafee4c

Please sign in to comment.