The main user-facing class. Specify a DAG, optionally assign mechanisms to nodes, fit to data, and extract causal effects.
Public fields
dagNamed list - the DAG specification.
mechanismsNamed list of
Mechanisminstances, one per node.dataThe training
data.frame(set after$fit()).tracesNamed list of mechanism fit objects, one per node (after fit).
.fittedLogical - whether
$fit()has been called.
Methods
KaguModel$new()
Create a new KaguModel.
Usage
KaguModel$new(dag, mechanisms = NULL, default_mechanism = NULL)Arguments
dagNamed list mapping each node to a character vector of its parent node names. Root nodes map to
c().mechanismsOptional named list of
Mechanisminstances. Any node not specified receives a copy ofdefault_mechanism.default_mechanismOptional
Mechanismused for every node not listed inmechanisms(each node gets its own copy). Defaults toGPMechanism$new(); pass e.g.GPMechanism$new(prior = gp_prior(...))to set one prior for the model.
KaguModel$fit()
Fit each node's conditional distribution in topological order.
KaguModel$effects()
Estimate the causal effect of source on target.
Usage
KaguModel$effects(
source,
target,
values = NULL,
std_units = FALSE,
conditions = NULL,
sweep = FALSE,
sweep_n = 50L,
sweep_range = NULL,
hdi = 0.9
)Arguments
sourceCharacter scalar - the intervention (treatment) node.
targetCharacter scalar - the outcome node.
valuesOptional numeric vector of length 2
c(from, to)giving the explicit intervention contrast.std_unitsLogical - if
TRUE, compute the effect of a 1-SD increase centred at the mean.conditionsOptional named list of node values to condition on (fixes those nodes at the given values during propagation). Can also be a list of such lists to compute and overlay multiple conditions.
sweepLogical - if
TRUE, compute the dose-response curve.sweep_nInteger - number of points in the sweep grid (default 50).
sweep_rangeNumeric vector
c(min, max)for the sweep grid. Defaults to the observed range ofsource.hdiNumeric in (0, 1) - HDI probability (default 0.90).
KaguModel$summary()
Per-node summary table.
For each node, reports the direct local effect of each parent (the function's gradient at the parents' means - comparable to a regression coefficient) and the residual noise sd, each with posterior mean, sd and HDI.
KaguModel$diagnostics()
Fit diagnostics for each node.
The Gaussian-process sampler produces a single chain, so r-hat / ESS do not apply; this reports the posterior draw count and the residual noise sd per node.
KaguModel$prior_predictive()
Simulate replicated datasets from the prior predictive distribution, by ancestral sampling through the DAG: each draw samples every node's hyperparameters, function and noise from its mechanism's prior, feeding simulated parents into their children. Needs no fit.
Arguments
dataA
data.frame, used only for each variable's location and scale (priors are on the standardised scale) and the number of rows. Defaults to the fitted data.ndrawsInteger - number of replicated datasets.
Returns
A PredictiveResult with type = "prior".
KaguModel$posterior_predictive()
Simulate replicated datasets from the posterior predictive distribution.
type = "conditional": every node is simulated given its observed parents. This checks each mechanism (local fit) in isolation.type = "joint": the whole graph is simulated ancestrally from the fitted model, with each node's posterior draw fed its simulated parents. This checks what the DAG implies globally - e.g. a missing edge shows up as a dependence the replicated data cannot reproduce.
Hyperparameters are point estimates, so the replicates do not carry hyperparameter uncertainty and can be slightly too narrow.
Usage
KaguModel$posterior_predictive(ndraws = 100L, type = c("conditional", "joint"))Arguments
ndrawsInteger - number of replicated datasets (at most the number of stored posterior draws).
type"conditional"or"joint".
KaguModel$function_draws()
Prior or posterior draws of a node's conditional-mean function against one parent, with the other parents held fixed - for plotting what functions the prior allows or the posterior has learned.
Usage
KaguModel$function_draws(
node,
parent = NULL,
ndraws = 50L,
n_grid = 100L,
prior = FALSE,
at = NULL,
data = NULL
)Arguments
nodeCharacter scalar - the node whose function to draw.
parentCharacter scalar - the parent to vary (default: the first).
ndrawsInteger - number of function draws.
n_gridInteger - number of grid points across the parent's range.
priorLogical - draw from the prior (
TRUE) or posterior.atOptional named list of values for the other parents (default: their means).
dataOptional
data.frame(defaults to the fitted data).
KaguModel$plot_dag()
Visualise the DAG structure.
KaguModel$plot_posterior()
Plot the posterior for a fitted node.
Shows the node's direct local effects (each parent's gradient at the means) and residual noise, as posterior means with HDI intervals.