-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpull.R
221 lines (191 loc) · 5.29 KB
/
pull.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#' Pull components from DAG objects
#'
#' `pull_dag()` and `pull_dag_data()` are generic methods to pull components of
#' DAG objects, e.g. `tidy_dagitty`, such as the `dagitty` object or the data
#' frame associated with it. These methods are recommended over extracting
#' components manually, e.g. `my_dag$data`, because the internal structure of
#' these objects may change over time. Similarly, use `update_dag()` if you want
#' to sync the data back to the DAG object or override it with another DAG; use
#' `update_dag_data()` to do update the data frame. This is useful with
#' `pull_dag_data()`.
#'
#' @param x a `tidy_dagitty` or `dagitty` object.
#' @param value a value to set, either a `dagitty` or `data.frame` object,
#' depending on the function.
#' @param ... For `dagitty` objects, passed to `tidy_dagitty()` if needed,
#' otherwise currently unused.
#'
#' @return a DAG object, e.g. `dagitty`, or data frame
#'
#' @examples
#'
#' tidy_dagitty_obj <- dagify(y ~ x + z, x ~ z) %>%
#' tidy_dagitty()
#' dag <- pull_dag(tidy_dagitty_obj)
#' dag_data <- pull_dag_data(tidy_dagitty_obj)
#'
#' tidy_dagitty_obj %>%
#' dplyr::mutate(name = toupper(name)) %>%
#' # recreate the DAG component
#' update_dag()
#'
#' dag_data$label <- paste0(dag_data$name, "(observed)")
#' update_dag_data(tidy_dagitty_obj) <- dag_data
#'
#' @export
pull_dag <- function(x, ...) {
UseMethod("pull_dag")
}
#' @export
#' @rdname pull_dag
pull_dag.tidy_dagitty <- function(x, ...) {
x$dag
}
#' @export
#' @rdname pull_dag
pull_dag.dagitty <- function(x, ...) {
x
}
#' @export
#' @rdname pull_dag
pull_dag_data <- function(x, ...) {
UseMethod("pull_dag_data")
}
#' @export
#' @rdname pull_dag
pull_dag_data.tidy_dagitty <- function(x, ...) {
x$data
}
#' @export
#' @rdname pull_dag
pull_dag_data.dagitty <- function(x, ...) {
tidy_dagitty(x, ...)$data
}
#' @export
#' @rdname pull_dag
`update_dag_data<-` <- function(x, value) {
UseMethod("update_dag_data<-")
}
#' @export
#' @rdname pull_dag
`update_dag_data<-.tidy_dagitty` <- function(x, value) {
x$data <- prep_dag_data(value)
x
}
prep_dag_data <- function(value, layout = "nicely", coords = NULL, ...) {
if (any(c("name", "to") %nin% names(value))) {
stop("Columns `name` and `to` not found")
}
if (is.null(coords) && layout == "time_ordered") {
coords <- value %>%
edges2df() %>%
auto_time_order() %>%
time_ordered_coords() %>%
coords2list()
}
if ("direction" %nin% names(value)) {
value$direction <- "->"
}
if (any(c("x", "y", "xend", "yend") %nin% names(value))) {
coords_df <- value %>%
dplyr::select(name, to) %>%
dplyr::filter(!is.na(name), !is.na(to)) %>%
generate_layout(
layout = layout,
coords = coords,
...
)
value <- value %>%
tidy_dag_edges_and_coords(coords_df)
}
# TODO: remove this when circular is changed
if ("circular" %nin% names(value)) {
value$circular <- FALSE
}
dplyr::as_tibble(value)
}
#' @export
#' @rdname pull_dag
update_dag <- function(x, ...) {
UseMethod("update_dag")
}
#' @export
#' @rdname pull_dag
`update_dag<-` <- function(x, value) {
UseMethod("update_dag<-")
}
#' @export
#' @rdname pull_dag
`update_dag.tidy_dagitty` <- function(x, ...) {
update_dag(x) <- recompile_dag(x)
x
}
#' @export
#' @rdname pull_dag
`update_dag<-.tidy_dagitty` <- function(x, value) {
stopifnot(dagitty::is.dagitty(value))
x$dag <- value
x
}
recompile_dag <- function(.dag) {
new_dag <- .dag %>%
pull_dag_data() %>%
compile_dag_from_df()
if ("status" %in% names(pull_dag_data(.dag))) {
.exposures <- return_status(.dag, "exposure")
.outcomes <- return_status(.dag, "outcome")
.latents <- return_status(.dag, "latent")
} else {
.exposures <- dagitty::exposures(pull_dag(.dag))
.outcomes <- dagitty::outcomes(pull_dag(.dag))
.latents <- dagitty::latents(pull_dag(.dag))
}
if ("adjusted" %in% names(pull_dag_data(.dag))) {
.adjusted <- dplyr::filter(.dag, adjusted == "adjusted") %>%
pull_dag_data() %>%
dplyr::pull(name) %>%
empty2list()
} else {
.adjusted <- dagitty::adjustedNodes(pull_dag(.dag))
}
dagitty::exposures(new_dag) <- .exposures
dagitty::outcomes(new_dag) <- .outcomes
dagitty::latents(new_dag) <- .latents
dagitty::adjustedNodes(new_dag) <- .adjusted
dagitty::coordinates(new_dag) <- .dag %>%
pull_dag_data() %>%
select(name, x, y) %>%
coords2list()
new_dag
}
compile_dag_from_df <- function(.df) {
if ("direction" %nin% names(.df)) {
.df$direction <- "<-"
}
.df %>%
dplyr::filter(!is.na(to)) %>%
dplyr::mutate(
direction = as.character(direction),
direction = ifelse(direction == "<-", "->", direction)
) %>%
dplyr::group_by(name, direction) %>%
dplyr::summarise(to_formula = paste("{", paste(to, collapse = " "), "}"), .groups = "drop") %>%
dplyr::transmute(dag_formula = paste(name, direction, to_formula)) %>%
dplyr::pull() %>%
paste(collapse = "; ") %>%
paste("dag {", ., "}") %>%
dagitty::dagitty()
}
return_status <- function(.dag, .status) {
if (is.tidy_dagitty(.dag)) .dag <- pull_dag_data(.dag)
dplyr::filter(.dag, status == .status) %>%
dplyr::pull(name) %>%
empty2list()
}
empty2list <- function(.x) {
if (purrr::is_empty(.x)) {
list()
} else {
.x
}
}