
Test a DAG against the conditional independencies it implies
Source:R/causal_dag_test.R
causal_dag_test.RdRestricts a graph to the nodes for which data has columns, enumerates the
conditional independencies the restricted graph implies, and tests each one.
Reports how many the data reject, which ones, and — optionally — the same
test after large-scale spatial structure has been removed.
Arguments
- dag
A
dagittygraph, e.g. fromcausal_cerrado_real_dag().- data
A data frame whose columns are named after the graph's nodes. Nodes without a column are dropped from the tested sub-graph and reported in
nodes_without_data.- alpha
Significance level for calling an implied independence rejected. Applied twice: per test (unadjusted counts) and as the family-wise level of a Holm adjustment over all implied independencies, which is what the printed verdict on the graph is based on.
- coords
Optional length-2 character vector naming the coordinate columns in
data. When supplied (andmgcvis installed), a second test is run on variables residualised on a thin-plate spline of position, which removes the large-scale spatial structure that inflates the effective sample size of a pooled test.- k
Basis dimension of that spline. The residual degrees of freedom are reduced by
kbefore testing, so the spatial test is not credited with observations the smoother consumed.
Value
An edaphos_dag_test object: a list with pooled (the
dagitty::localTests table), spatial (the same after residualising, or
NULL), n_tests, n_rejected and n_rejected_spatial (unadjusted
p-value below alpha), n_rejected_holm and n_rejected_spatial_holm
(Holm-adjusted p-value below alpha, via stats::p.adjust()),
max_abs_r (largest absolute partial correlation over all tests,
rejected or not), nodes_tested, nodes_without_data, and n.
With n_tests implied independencies that all hold, about
alpha * n_tests unadjusted rejections are expected by chance alone, so
the unadjusted count is not by itself evidence against the graph. The
print method reports it against that expectation and rests its verdict on
the Holm count.
Examples
if (requireNamespace("dagitty", quietly = TRUE)) {
# Simulated profiles, already using the graph's own column names.
d <- edaphos_example_profiles()
tst <- causal_dag_test(causal_cerrado_real_dag(), d)
tst
# The spatial variant fits one thin-plate spline per variable, so it is
# seconds rather than milliseconds.
# \donttest{
tsp <- causal_dag_test(causal_cerrado_real_dag(), d,
coords = c("lon", "lat"))
# The graph fails on both tests -- but the counts differ, which is why
# reporting only the pooled one would overstate the result.
c(pooled = tsp$n_rejected, spatial = tsp$n_rejected_spatial,
of = tsp$n_tests)
# }
}
#> pooled spatial of
#> 6 2 66