Skip to content

Commit

Permalink
allow formula interface
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 18, 2024
1 parent 61da662 commit 4da79dd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
43 changes: 33 additions & 10 deletions R/check_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
#' First element may also be model object. If a model objects is provided, its
#' formula is used as first formula, and all independent variables will be used
#' for the `adjusted` argument. See 'Details' and 'Examples'.
#' @param outcome Name of the dependent variable (outcome), as character string.
#' Must be a valid name from the formulas. If not set, the first dependent
#' variable from the formulas is used.
#' @param exposure Name of the exposure variable (as character string), for
#' which the direct and total causal effect on the `outcome` should be checked.
#' Must be a valid name from the formulas. If not set, the first independent
#' variable from the formulas is used.
#' @param adjusted A character vector with names of variables that are adjusted
#' for in the model. If a model object is provided in `...`, any values in
#' `adjusted` will be overwritten by the model's independent variables.
#' @param outcome Name of the dependent variable (outcome), as character string
#' or as formula. Must be a valid name from the formulas provided in `...`. If
#' not set, the first dependent variable from the formulas is used.
#' @param exposure Name of the exposure variable (as character string or
#' formula), for which the direct and total causal effect on the `outcome`
#' should be checked. Must be a valid name from the formulas provided in `...`.
#' If not set, the first independent variable from the formulas is used.
#' @param adjusted A character vector or formula with names of variables that
#' are adjusted for in the model. If a model object is provided in `...`, any
#' values in `adjusted` will be overwritten by the model's independent
#' variables.
#' @param latent A character vector with names of latent variables in the model.
#' @param effect Character string, indicating which effect to check. Can be
#' `"all"` (default), `"total"`, or `"direct"`.
Expand Down Expand Up @@ -138,6 +139,16 @@
#' )
#' dag
#'
#' # using formula interface for arguments "outcome", "exposure" and "adjusted"
#' check_dag(
#' y ~ x + b + c,
#' x ~ b,
#' outcome = ~y,
#' exposure = ~x,
#' adjusted = ~ b + c
#' )
#' dag
#'
#' # use specific layout for the DAG
#' dag <- check_dag(
#' score ~ exp + b + c,
Expand Down Expand Up @@ -218,6 +229,18 @@ check_dag <- function(...,
)$conditional[1]
}

# handle formula interface - if "outcome", "exposure" or "adjusted" are
# provided as formulas, convert to character here
if (inherits(outcome, "formula")) {
outcome <- all.vars(outcome)
}
if (inherits(exposure, "formula")) {
exposure <- all.vars(exposure)
}
if (inherits(adjusted, "formula")) {
adjusted <- all.vars(adjusted)
}

# convert to dag
dag_args <- c(formulas, list(
exposure = exposure,
Expand Down
31 changes: 21 additions & 10 deletions man/check_dag.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-check_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ test_that("check_dag, different adjustements for total and direct", {
expect_snapshot(print(dag))
})


test_that("check_dag, collider bias", {
dag <- check_dag(
SMD_ICD11 ~ agegroup + gender_kid + edgroup3 + residence + pss4_kid_sum_2sd + sm_h_total_kid,
Expand Down Expand Up @@ -173,3 +174,15 @@ test_that("check_dag, collider bias", {
)
expect_snapshot(print(dag))
})


test_that("check_dag, formula-interface", {
dag <- check_dag(
y ~ x + b + c,
x ~ b,
outcome = ~y,
exposure = ~x,
adjusted = ~ b + c
)
expect_identical(attributes(dag)$adjusted, c("b", "c"))
})

0 comments on commit 4da79dd

Please sign in to comment.