Various helpers to inspect or access internal objects generated or used by Causal Models
Returns specified elements from a causal_model
and prints summary.
Users can use inspect
to extract model's components or objects implied by
the model structure including nodal types, causal types, parameter priors,
parameter posteriors, type priors, type posteriors, and other relevant elements.
See argument what
for other options.
Returns specified elements from a causal_model
.
Users can use inspect
to extract model's components or objects implied by
the model structure including nodal types, causal types, parameter priors,
parameter posteriors, type priors, type posteriors, and other relevant elements.
See argument what
for other options.
Arguments
- model
A
causal_model
. A model object generated bymake_model
.- what
A character string specifying the component to retrieve. Available options are:
"
statement
" a character string describing causal relations using dagitty syntax,"
nodes
" A list containing the nodes in the model,"
parents_df
" A table listing nodes, whether they are root nodes or not, and the number and names of parents they have,"
parameters
" A vector of 'true' parameters,"
parameter_names
" A vector of names of parameters,"
parameter_mapping
" A matrix mapping from parameters into data types,"
parameter_matrix
" A matrix mapping from parameters into causal types,"
parameters_df
" A data frame containing parameter information,"
causal_types
" A data frame listing causal types and the nodal types that produce them,"
nodal_types
" A list with the nodal types of the model,"
data_types
" A list with all data types consistent with the model; for options see?get_all_data_types
,"
ambiguities_matrix
" A matrix mapping from causal types into data types,"
type_prior
" A matrix of type probabilities using priors,"
prior_hyperparameters
" A vector of alpha values used to parameterize Dirichlet prior distributions; optionally provide node names to reduce output, e.g.,inspect(prior_hyperparameters, nodes = c('M', 'Y'))
,"
prior_event_probabilities
" A vector of data (event) probabilities given a single realization of parameters; for options see?get_event_probabilities
,"
prior_distribution
" A data frame of the parameter prior distribution,"
posterior_distribution
" A data frame of the parameter posterior distribution,"
posterior_event_probabilities
" A sample of data (event) probabilities from the posterior,"
type_distribution
" A matrix of type probabilities using posteriors,"
data
" A data frame with data that was provided to update the model,"
stanfit
" Astanfit
object generated by Stan; printsstanfit
summary with updated parameter names,"
stan_warnings
" Messages generated during the generation of astanfit
object."
stan_summary
" A list of Stan outputs that includesstanfit
,data
, and, if requested when updating the model, posteriorevent_probabilities
andtype_distribution
; printsstanfit
summary with updated parameter names.
- ...
Other arguments passed to helper
"get_*"
functions: get_all_data_types, get_event_probabilities, get_priors, Anys such additional arguments must be named.
Value
Objects that can be derived from a causal_model
, with summary.
Quiet return of objects that can be derived from a causal_model
.
Examples
# \donttest{
model <- make_model("X -> Y")
data <- make_data(model, n = 4)
inspect(model, what = "statement")
#>
#> Causal statement:
#> X -> Y
inspect(model, what = "parameters")
#>
#> parameters
#> Model parameters with associated probabilities:
#>
#> X.0 X.1 Y.00 Y.10 Y.01 Y.11
#> 0.50 0.50 0.25 0.25 0.25 0.25
inspect(model, what = "nodes")
#>
#> Nodes:
#> X, Y
inspect(model, what = "parents_df")
#>
#> Root vs Non-Root status with number and names of parents for each node:
#>
#> node root parents parent_nodes
#> 1 X TRUE 0
#> 2 Y FALSE 1 X
inspect(model, what = "parameters_df")
#>
#> parameters_df
#> Mapping of model parameters to nodal types:
#>
#> param_names: name of parameter
#> node: name of endogeneous node associated
#> with the parameter
#> gen: partial causal ordering of the
#> parameter's node
#> param_set: parameter groupings forming a simplex
#> given: if model has confounding gives
#> conditioning nodal type
#> param_value: parameter values
#> priors: hyperparameters of the prior
#> Dirichlet distribution
#>
#> param_names node gen param_set nodal_type given param_value priors
#> 1 X.0 X 1 X 0 0.50 1
#> 2 X.1 X 1 X 1 0.50 1
#> 3 Y.00 Y 2 Y 00 0.25 1
#> 4 Y.10 Y 2 Y 10 0.25 1
#> 5 Y.01 Y 2 Y 01 0.25 1
#> 6 Y.11 Y 2 Y 11 0.25 1
inspect(model, what = "causal_types")
#>
#> causal_types (Causal Types)
#>
#> Cartesian product of nodal types
#> X Y
#> X0.Y00 0 00
#> X1.Y00 1 00
#> X0.Y10 0 10
#> X1.Y10 1 10
#> X0.Y01 0 01
#> X1.Y01 1 01
#> X0.Y11 0 11
#> X1.Y11 1 11
inspect(model, what = "prior_distribution")
#>
#> prior_distribution
#> Summary statistics of model parameters prior distributions:
#>
#> Distributions matrix dimensions are
#> 4000 rows (draws) by 6 cols (parameters)
#>
#> mean sd
#> X.0 0.51 0.29
#> X.1 0.49 0.29
#> Y.00 0.25 0.19
#> Y.10 0.25 0.19
#> Y.01 0.25 0.19
#> Y.11 0.25 0.19
inspect(model, what = "prior_hyperparameters", nodes = "Y")
#>
#> prior_hyperparameters
#> Alpha parameter values used for Dirichlet prior distributions:
#>
#> Y.00 Y.10 Y.01 Y.11
#> 1 1 1 1
inspect(model, what = "prior_event_probabilities", parameters = c(.1, .9, .25, .25, 0, .5))
#>
#> prior_event_probabilities
#> Probabilities of observing data (events)
#> for a specified set of parameter values:
#>
#> event_probs
#> X0Y0 0.025
#> X1Y0 0.450
#> X0Y1 0.075
#> X1Y1 0.450
inspect(model, what = "prior_event_probabilities", given = "Y==1")
#>
#> prior_event_probabilities
#> Probabilities of observing data (events)
#> for a specified set of parameter values:
#>
#> event_probs
#> X0Y0 0.0
#> X1Y0 0.0
#> X0Y1 0.5
#> X1Y1 0.5
inspect(model, what = "data_types", complete_data = TRUE)
#>
#> data_types (Data types):
#> Data frame of all possible data (events) given the model:
#>
#> event X Y
#> X0Y0 X0Y0 0 0
#> X1Y0 X1Y0 1 0
#> X0Y1 X0Y1 0 1
#> X1Y1 X1Y1 1 1
inspect(model, what = "data_types", complete_data = FALSE)
#>
#> data_types (Data types):
#> Data frame of all possible data (events) given the model:
#>
#> event X Y
#> X0Y0 X0Y0 0 0
#> X1Y0 X1Y0 1 0
#> X0Y1 X0Y1 0 1
#> X1Y1 X1Y1 1 1
#> Y0 Y0 NA 0
#> Y1 Y1 NA 1
#> X0 X0 0 NA
#> X1 X1 1 NA
#> None None NA NA
model <- update_model(model,
data = data,
keep_fit = TRUE,
keep_event_probabilities = TRUE)
#>
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 4e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.4 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.137 seconds (Warm-up)
#> Chain 1: 0.144 seconds (Sampling)
#> Chain 1: 0.281 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 1.7e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.17 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 0.139 seconds (Warm-up)
#> Chain 2: 0.149 seconds (Sampling)
#> Chain 2: 0.288 seconds (Total)
#> Chain 2:
#>
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 3).
#> Chain 3:
#> Chain 3: Gradient evaluation took 1.7e-05 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.17 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3:
#> Chain 3:
#> Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 3:
#> Chain 3: Elapsed Time: 0.125 seconds (Warm-up)
#> Chain 3: 0.12 seconds (Sampling)
#> Chain 3: 0.245 seconds (Total)
#> Chain 3:
#>
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 4).
#> Chain 4:
#> Chain 4: Gradient evaluation took 1.6e-05 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.16 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4:
#> Chain 4:
#> Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 4:
#> Chain 4: Elapsed Time: 0.133 seconds (Warm-up)
#> Chain 4: 0.123 seconds (Sampling)
#> Chain 4: 0.256 seconds (Total)
#> Chain 4:
inspect(model, what = "posterior_distribution")
#>
#> posterior_distribution
#> Summary statistics of model parameters posterior distributions:
#>
#> Distributions matrix dimensions are
#> 4000 rows (draws) by 6 cols (parameters)
#>
#> mean sd
#> X.0 0.67 0.18
#> X.1 0.33 0.18
#> Y.00 0.34 0.19
#> Y.10 0.25 0.17
#> Y.01 0.23 0.17
#> Y.11 0.18 0.14
inspect(model, what = "posterior_event_probabilities")
#>
#> posterior_event_probabilities
#> Posterior draws of event probabilities (transformed parameters):
#>
#> Distributions matrix dimensions are
#> 4000 rows (draws) by 4 cols (events)
#>
#> mean sd
#> X0Y0 0.38 0.16
#> X1Y0 0.19 0.13
#> X0Y1 0.29 0.14
#> X1Y1 0.14 0.11
inspect(model, what = "type_distribution")
#>
#> type_distribution
#> Posterior draws of causal types (transformed parameters):
#>
#> Distributions matrix dimensions are
#> 4000 rows (draws) by 8 cols (causal types)
#>
#> mean sd
#> X0.Y00 0.23 0.15
#> X1.Y00 0.11 0.10
#> X0.Y10 0.17 0.13
#> X1.Y10 0.08 0.08
#> X0.Y01 0.15 0.12
#> X1.Y01 0.08 0.08
#> X0.Y11 0.12 0.10
#> X1.Y11 0.06 0.06
inspect(model, what = "data")
#>
#> Data used to update the model:
#>
#> data
#> Data frame dimensions are
#> 4 rows by 2 cols
#>
#> X Y
#> 1 0 0
#> 2 0 0
#> 3 0 1
#> 4 1 0
inspect(model, what = "stan_warnings")
#>
#> stan_warnings
#> Stan warnings generated during updating:
inspect(model, what = "stanfit")
#>
#> stanfit
#> Stan model summary:
#> Inference for Stan model: simplexes.
#> 4 chains, each with iter=2000; warmup=1000; thin=1;
#> post-warmup draws per chain=1000, total post-warmup draws=4000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> lambdas[1] 0.67 0.00 0.18 0.29 0.55 0.69 0.81 0.95 2679 1
#> lambdas[2] 0.33 0.00 0.18 0.05 0.19 0.31 0.45 0.71 2679 1
#> lambdas[3] 0.34 0.00 0.19 0.02 0.19 0.33 0.48 0.73 1666 1
#> lambdas[4] 0.25 0.00 0.17 0.01 0.12 0.22 0.36 0.62 3926 1
#> lambdas[5] 0.23 0.00 0.17 0.01 0.10 0.20 0.33 0.62 2967 1
#> lambdas[6] 0.18 0.00 0.14 0.01 0.07 0.15 0.26 0.52 3470 1
#> w[1] 0.38 0.00 0.16 0.11 0.26 0.37 0.49 0.71 3084 1
#> w[2] 0.19 0.00 0.13 0.02 0.10 0.17 0.27 0.49 2604 1
#> w[3] 0.29 0.00 0.14 0.06 0.18 0.27 0.38 0.61 3269 1
#> w[4] 0.14 0.00 0.11 0.01 0.06 0.11 0.19 0.41 2359 1
#> types[1] 0.23 0.00 0.15 0.02 0.11 0.21 0.32 0.55 1810 1
#> types[2] 0.11 0.00 0.10 0.01 0.04 0.09 0.16 0.35 2033 1
#> types[3] 0.17 0.00 0.13 0.01 0.07 0.14 0.24 0.47 3741 1
#> types[4] 0.08 0.00 0.08 0.00 0.03 0.06 0.11 0.28 3155 1
#> types[5] 0.15 0.00 0.12 0.01 0.06 0.12 0.22 0.45 3093 1
#> types[6] 0.08 0.00 0.08 0.00 0.02 0.05 0.11 0.28 2620 1
#> types[7] 0.12 0.00 0.10 0.00 0.04 0.09 0.17 0.38 3435 1
#> types[8] 0.06 0.00 0.06 0.00 0.02 0.04 0.08 0.23 2688 1
#> lp__ -9.93 0.04 1.58 -13.93 -10.74 -9.58 -8.78 -7.95 1448 1
#>
#> Samples were drawn using NUTS(diag_e) at Tue Dec 17 17:49:41 2024.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
# }
model <- make_model("X -> Y")
x <- grab(model, what = "statement")
x
#> [1] "X -> Y"