Skip to content

Commit

Permalink
update docs; bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
nhejazi committed Sep 21, 2024
1 parent 2fa7308 commit 0c3b8f0
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 73 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/R-CMD-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ jobs:

steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup R
uses: r-lib/actions/setup-r@master
uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- name: Install pandoc
uses: r-lib/actions/setup-pandoc@v1
uses: r-lib/actions/setup-pandoc@v2

- name: Install tinyTeX
uses: r-lib/actions/setup-tinytex@v1
uses: r-lib/actions/setup-tinytex@v2

- name: Install system dependencies
if: runner.os == 'Linux'
Expand Down
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tmle3shift
Title: Targeted Learning of the Causal Effects of Stochastic Interventions
Version: 0.2.1
Version: 0.2.2
Authors@R: c(
person("Nima", "Hejazi", email = "[email protected]",
role = c("aut", "cre", "cph"),
Expand Down Expand Up @@ -39,12 +39,12 @@ Suggests:
covr,
stats,
ggplot2,
sl3 (>= 1.4.2),
txshift (>= 0.3.5),
haldensify (>= 0.0.5),
sl3 (>= 1.4.5),
txshift (>= 0.3.8),
haldensify (>= 0.2.3),
hal9001,
xgboost,
ranger,
speedglm,
Rsolnp,
nnls
Remotes:
Expand All @@ -56,5 +56,5 @@ Encoding: UTF-8
LazyData: true
LazyLoad: yes
VignetteBuilder: knitr
RoxygenNote: 7.2.2
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE, r6 = FALSE)
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# tmle3shift 0.2.2

* Change default value of argument `max_shifted_ratio` from 2 to 5 throughout.
* Bump up minimum versions of dependencies `sl3`, `hal9001`, and `haldensify`.

# tmle3shift 0.2.1

* Change all instances of default shift being `shift_additive_bounded` to use
`shift_additive` instead, since the former is incompatible with the simplifer
`shift_additive` instead, since the former is incompatible with the simpler
identifying functional under the modified treatment policy definition.

# tmle3shift 0.1.9
Expand Down
7 changes: 4 additions & 3 deletions R/tmle3_Spec_shift.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tmle3_Spec_shift <- R6::R6Class(
initialize = function(shift_fxn = shift_additive,
shift_fxn_inv = shift_additive_inv,
shift_val = 0,
max_shifted_ratio = 2,
max_shifted_ratio = 5,
...) {
options <- list(
shift_fxn = shift_fxn,
Expand All @@ -31,7 +31,7 @@ tmle3_Spec_shift <- R6::R6Class(
if (A_type$type != "continuous") {
msg <- paste(
"This parameter is defined as a shift of a continuous",
"treatment. The treatment detected is NOT continuous."
"treatment. The treatment variable is not continuous."
)
stop(msg)
}
Expand Down Expand Up @@ -112,7 +112,8 @@ tmle3_Spec_shift <- R6::R6Class(
#' @export
tmle_shift <- function(shift_fxn = shift_additive,
shift_fxn_inv = shift_additive_inv,
shift_val = 1, max_shifted_ratio = 2, ...) {
shift_val = 1, max_shifted_ratio = 5,
...) {
# TODO: unclear why this has to be in a factory function
tmle3_Spec_shift$new(
shift_fxn, shift_fxn_inv,
Expand Down
6 changes: 3 additions & 3 deletions R/tmle3_Spec_vimshift_delta.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tmle3_Spec_vimshift_delta <- R6::R6Class(
initialize = function(shift_fxn = shift_additive,
shift_fxn_inv = shift_additive_inv,
shift_grid = seq(-1, 1, by = 0.5),
max_shifted_ratio = 2,
max_shifted_ratio = 5,
weighting = c("identity", "variance"),
...) {
options <- list(
Expand Down Expand Up @@ -84,7 +84,7 @@ tmle3_Spec_vimshift_delta <- R6::R6Class(
private = list()
)

################################################################################
###############################################################################

#' Outcome Under a Grid of Shifted Interventions via Delta Method
#'
Expand Down Expand Up @@ -119,7 +119,7 @@ tmle3_Spec_vimshift_delta <- R6::R6Class(
tmle_vimshift_delta <- function(shift_fxn = shift_additive,
shift_fxn_inv = shift_additive_inv,
shift_grid = seq(-1, 1, by = 0.5),
max_shifted_ratio = 2,
max_shifted_ratio = 5,
weighting = c("identity", "variance"),
...) {
# set default for weighting
Expand Down
6 changes: 3 additions & 3 deletions R/tmle3_Spec_vimshift_msm.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tmle3_Spec_vimshift_msm <- R6::R6Class(
initialize = function(shift_fxn = shift_additive,
shift_fxn_inv = shift_additive_inv,
shift_grid = seq(-1, 1, by = 0.5),
max_shifted_ratio = 2,
max_shifted_ratio = 5,
weighting = c("identity", "variance"),
...) {
options <- list(
Expand Down Expand Up @@ -84,7 +84,7 @@ tmle3_Spec_vimshift_msm <- R6::R6Class(
private = list()
)

#################################################################################
###############################################################################

#' Outcome Under a Grid of Shifted Interventions via Targeted Working MSM
#'
Expand Down Expand Up @@ -119,7 +119,7 @@ tmle3_Spec_vimshift_msm <- R6::R6Class(
tmle_vimshift_msm <- function(shift_fxn = shift_additive,
shift_fxn_inv = shift_additive_inv,
shift_grid = seq(-1, 1, by = 0.5),
max_shifted_ratio = 2,
max_shifted_ratio = 5,
weighting = c("identity", "variance"),
...) {
# set default for weighting
Expand Down
2 changes: 1 addition & 1 deletion man/tmle_shift.Rd

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

2 changes: 1 addition & 1 deletion man/tmle_vimshift_delta.Rd

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

2 changes: 1 addition & 1 deletion man/tmle_vimshift_msm.Rd

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

47 changes: 26 additions & 21 deletions tests/testthat/test-vim_msm_delta_method.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ n_w <- 1 # number of baseline covariates
tx_mult <- 2 # multiplier for effect of W = 1 on treatment
delta_grid <- seq(-1, 1, 1) # grid of shifts


## baseline covariates -- simple, binary
W <- as.numeric(replicate(n_w, rbinom(n_obs, 1, 0.5)))
W <- as.numeric(replicate(n_w, rbinom(n_obs, 1, 0.65)))

## create treatment based on baseline W
A <- as.numeric(rnorm(n_obs, mean = tx_mult * W, sd = 1))
Expand All @@ -25,37 +24,43 @@ Y <- A + W + rnorm(n_obs, mean = 0, sd = 1)
data <- data.table(W, A, Y)
node_list <- list(W = "W", A = "A", Y = "Y")

# learners used for conditional expectation regression (e.g., outcome)
mean_lrnr <- Lrnr_mean$new()
glm_lrnr <- Lrnr_glm$new()
xgb_lrnr <- Lrnr_xgboost$new()
sl_lrnr <- Lrnr_sl$new(
learners = list(mean_lrnr, glm_lrnr, xgb_lrnr),
metalearner = Lrnr_nnls$new()
# learners used for outcome regression (conditional expectation)
mean_learner <- Lrnr_mean$new()
fglm_learner <- Lrnr_glm_fast$new()
xgb_learner <- Lrnr_xgboost$new()
sl_or_learner <- Lrnr_sl$new(
learners = list(mean_learner, fglm_learner, xgb_learner)
)

# learners used for conditional density estimation (i.e., propensity score)
hse_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = glm_lrnr
# learners used for generalized propensity score (conditional density)
hse_mean_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = mean_learner
)
hse_fglm_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = fglm_learner
)
hse_xgb_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = xgb_learner
)
mvd_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = xgb_lrnr,
var_learner = glm_lrnr
mvd_xgb_fglm_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = xgb_learner,
var_learner = fglm_learner
)
sl_density_lrnr <- Lrnr_sl$new(
learners = Stack$new(hse_learner, mvd_learner),
sl_gps_learner <- Lrnr_sl$new(
learners = Stack$new(hse_mean_learner, hse_xgb_learner, hse_fglm_learner,
mvd_xgb_fglm_learner),
metalearner = Lrnr_solnp_density$new()
)

# specify outcome and treatment regressions and create learner list
Q_learner <- sl_lrnr
g_learner <- sl_density_lrnr
learner_list <- list(Y = Q_learner, A = g_learner, delta_Y = Q_learner)
or_learner <- sl_or_learner
gps_learner <- sl_gps_learner
learner_list <- list(Y = or_learner, A = gps_learner, delta_Y = or_learner)

# initialize a tmle specification
tmle_spec <- tmle_vimshift_delta(
shift_grid = delta_grid,
max_shifted_ratio = 2
max_shifted_ratio = 10
)

## define data (from tmle3_Spec base class)
Expand Down
64 changes: 35 additions & 29 deletions tests/testthat/test-vim_targeted_msm.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,61 @@ set.seed(429153)

## simulate simple data for tmle-shift sketch
n_obs <- 1000 # number of observations
n_w <- 1 # number of baseline covariates
n_w <- 2 # number of baseline covariates
tx_mult <- 2 # multiplier for effect of W = 1 on treatment
delta_grid <- seq(-1, 1, 1) # grid of shifts to consider

## baseline covariates -- simple, binary
W <- as.numeric(replicate(n_w, rbinom(n_obs, 1, 0.5)))
W <- replicate(n_w, rbinom(n_obs, 1, 0.65))

## create treatment based on baseline W
A <- as.numeric(rnorm(n_obs, mean = tx_mult * W, sd = 1))
A <- as.numeric(rnorm(n_obs, mean = tx_mult * rowSums(W), sd = 0.5))

## create outcome as a linear function of A, W + white noise
Y <- A + W + rnorm(n_obs, mean = 0, sd = 1)
Y <- A + rowSums(W) + rnorm(n_obs, mean = 0, sd = 1)

## organize data and nodes for tmle3
data <- data.table(W, A, Y)
node_list <- list(W = "W", A = "A", Y = "Y")

# learners used for conditional expectation regression (e.g., outcome)
mean_lrnr <- Lrnr_mean$new()
glm_lrnr <- Lrnr_glm$new()
xgb_lrnr <- Lrnr_xgboost$new()
sl_lrnr <- Lrnr_sl$new(
learners = list(mean_lrnr, glm_lrnr, xgb_lrnr),
metalearner = Lrnr_nnls$new()
setnames(data, c("W1", "W2", "A", "Y"))
node_list <- list(W = c("W1", "W2"), A = "A", Y = "Y")

# learners used for outcome regression (conditional expectation)
mean_learner <- Lrnr_mean$new()
fglm_learner <- Lrnr_glm_fast$new()
xgb_learner <- Lrnr_xgboost$new()
sl_or_learner <- Lrnr_sl$new(
learners = list(mean_learner, fglm_learner, xgb_learner)
)

# learners used for conditional density estimation (i.e., propensity score)
hse_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = glm_lrnr
# learners used for generalized propensity score (conditional density)
hse_mean_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = mean_learner
)
mvd_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = xgb_lrnr,
var_learner = glm_lrnr
hse_fglm_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = fglm_learner
)
sl_density_lrnr <- Lrnr_sl$new(
learners = Stack$new(hse_learner, mvd_learner),
hse_xgb_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = xgb_learner
)
mvd_xgb_fglm_learner <- make_learner(Lrnr_density_semiparametric,
mean_learner = xgb_learner,
var_learner = fglm_learner
)
sl_gps_learner <- Lrnr_sl$new(
learners = Stack$new(hse_mean_learner, hse_xgb_learner, hse_fglm_learner,
mvd_xgb_fglm_learner),
metalearner = Lrnr_solnp_density$new()
)

# specify outcome and treatment regressions and create learner list
Q_learner <- sl_lrnr
g_learner <- sl_density_lrnr
learner_list <- list(Y = Q_learner, A = g_learner, delta_Y = Q_learner)
or_learner <- sl_or_learner
gps_learner <- sl_gps_learner
learner_list <- list(Y = or_learner, A = gps_learner, delta_Y = or_learner)

# initialize a tmle specification for direct targeting of MSM parameters
tmle_spec <- tmle_vimshift_msm(
shift_grid = delta_grid,
max_shifted_ratio = 2
max_shifted_ratio = 10
)

## define data (from tmle3_Spec base class)
Expand All @@ -80,7 +87,7 @@ tmle_fit_targeted_msm <- fit_tmle3(
# initialize a tmle specification for delta method
tmle_spec <- tmle_vimshift_delta(
shift_grid = delta_grid,
max_shifted_ratio = 2
max_shifted_ratio = 10
)

## define data (from tmle3_Spec base class)
Expand Down Expand Up @@ -108,7 +115,6 @@ msm_delta_summary <- tmle_fit_delta_method$summary[4:5, ]
msm_targeted_summary <- tmle_fit_targeted_msm$summary

test_that("Targeted MSM approach and delta method MSM match nearly", {
expect_equal(msm_delta_summary, msm_targeted_summary,
tol = 0.01,
)
expect_equal(msm_delta_summary$tmle_est, msm_targeted_summary$tmle_est,
tol = 0.05)
})

0 comments on commit 0c3b8f0

Please sign in to comment.