Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 13, 2024
1 parent 5de1447 commit a52d295
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
21 changes: 15 additions & 6 deletions R/check_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,22 @@ check_dag <- function(...,
setdiff(ma, collider)
})
}
# these are required. we make sure that both sets include the exposure
# so we can correctly compare required and current sets
all_required_adjustments <- sort(unique(c(exposure, adjustment_set)))
# this is what we have and which are allow
all_allowed_adjustments <- unique(setdiff(c(exposure, adjustment_nodes), collider))
list(
adjustment_not_needed = (is.null(adjustment_set) && is.null(adjustment_nodes) ||
(identical(sort(unique(adjustment_set)), sort(unique(setdiff(c(exposure, adjustment_nodes), collider)))))) &&
is.null(collider),
incorrectly_adjusted = (is.null(adjustment_set) && !is.null(adjustment_nodes)) ||
(!identical(sort(unique(adjustment_set)), sort(unique(setdiff(c(exposure, adjustment_nodes), collider))))) ||
(!is.null(collider) && collider %in% adjustment_nodes),
# no adjustment needed when
# - required and current adjustment sets are NULL
# - OR required and current adjustments are identical
# - AND we have no collider in current adjustments
adjustment_not_needed = ((is.null(adjustment_set) && is.null(adjustment_nodes)) || identical(all_required_adjustments, all_allowed_adjustments)) && is.null(collider),

Check warning on line 275 in R/check_dag.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_dag.R,line=275,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 172 characters.

Check warning on line 275 in R/check_dag.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_dag.R,line=275,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 172 characters.
# incorrect adjustment when
# - required is NULL and current adjustment not NULL
# - OR required and current adjustments are *not* identical
# - OR we have a collider in current adjustments
incorrectly_adjusted = (is.null(adjustment_set) && !is.null(adjustment_nodes)) || !identical(all_required_adjustments, all_allowed_adjustments) || (!is.null(collider) && collider %in% adjustment_nodes),

Check warning on line 280 in R/check_dag.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_dag.R,line=280,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 208 characters.

Check warning on line 280 in R/check_dag.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_dag.R,line=280,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 208 characters.
current_adjustments = adjustment_nodes,
minimal_adjustments = minimal_adjustments,
collider = collider
Expand Down
28 changes: 14 additions & 14 deletions tests/testthat/_snaps/check_dag.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
Identification of direct and total effects
Model is correctly specified.
No adjustment needed to estimate the direct and total effect of `wt` on `mpg`.
Incorrectly adjusted!
To estimate the direct and total effect, do not adjust for `cyl`, `disp` and `gear`.

# check_dag, multiple adjustment sets
Expand Down Expand Up @@ -155,13 +155,13 @@
Identification of direct effects
Incorrectly adjusted!
To estimate the direct effect, do not adjust for `x1`.
Model is correctly specified.
No adjustment needed to estimate the direct effect of `exposure` on `outcome`.
Identification of total effects
Incorrectly adjusted!
To estimate the total effect, do not adjust for `x1`.
Model is correctly specified.
No adjustment needed to estimate the total effect of `exposure` on `outcome`.

---
Expand All @@ -176,13 +176,13 @@
Identification of direct effects
Incorrectly adjusted!
To estimate the direct effect, do not adjust for `x2`.
Model is correctly specified.
No adjustment needed to estimate the direct effect of `exposure` on `outcome`.
Identification of total effects
Incorrectly adjusted!
To estimate the total effect, do not adjust for `x2`.
Model is correctly specified.
No adjustment needed to estimate the total effect of `exposure` on `outcome`.

---
Expand All @@ -197,13 +197,13 @@
Identification of direct effects
Incorrectly adjusted!
To estimate the direct effect, do not adjust for `x1` and `x2`.
Model is correctly specified.
No adjustment needed to estimate the direct effect of `exposure` on `outcome`.
Identification of total effects
Incorrectly adjusted!
To estimate the total effect, do not adjust for `x1` and `x2`.
Model is correctly specified.
No adjustment needed to estimate the total effect of `exposure` on `outcome`.

# check_dag, collider bias
Expand Down

0 comments on commit a52d295

Please sign in to comment.