Skip to contents

Returns specified elements from a causal_model. Users can use grab 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 object for other

Usage

grab(model, object = NULL, ...)

Arguments

model

A causal_model. A model object generated by make_model.

object

A character string specifying the component to retrieve. Available options are:

  • "causal_statement" a character. Statement describing causal relations using dagitty syntax,

  • "dag" A data frame with columns ‘parent’ and ‘children’ indicating how nodes relate to each other,

  • "nodes" A list containing the nodes in the model,

  • "parents" a table listing nodes, whether they are root nodes or not, and the number and names of parents they have,

  • "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 the all data types consistent with the model; for options see "?get_all_data_types",

  • "event_probabilities" a vector of data (event) probabilities given a parameter vector; for options see "?get_event_probabilities",

  • "ambiguities_matrix" a matrix mapping from causal types into data types,

  • "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,

  • "prior_hyperparameters" a vector of alpha values used to parameterize Dirichlet prior distributions,

  • "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,

  • "stan_objects" stan_objects is a list of Stan outputs that can include the stanfit object, the data that was used, and distributions over causal types and event probabilities.

  • "stan_fit" the stanfit object generated by Stan,

  • "stan_summary" a summary of the stanfit object generated by Stan,

  • "type_prior" a matrix of type probabilities using priors,

  • "type_posterior" a matrix of type probabilities using posteriors,

...

Other arguments passed to helper "get_*" functions.

Value

Objects from a causal_model as specified.

Examples

# \donttest{
model <-
  make_model('X -> Y') |>
   update_model(
   keep_event_probabilities = TRUE,
   keep_fit = TRUE,
   refresh = 0 )
#> No data provided

grab(model, object = "causal_statement")
#> 
#> Statement: 
#> X -> Y
grab(model, object = "dag")
#> 
#> Dag: 
#>   parent children
#> 1      X        Y
grab(model, object = "nodes")
#> 
#> Nodes: 
#> X, Y
grab(model, object = "parents")
#>   node  root parents parent_nodes
#> 1    X  TRUE       0             
#> 2    Y FALSE       1            X
grab(model, object = "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
grab(model, object = "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
grab(model, object = "nodal_types")
#> Nodal types: 
#> $X
#> 0  1
#> 
#>   node position display interpretation
#> 1    X       NA      X0          X = 0
#> 2    X       NA      X1          X = 1
#> 
#> $Y
#> 00  10  01  11
#> 
#>   node position display interpretation
#> 1    Y        1   Y[*]*      Y | X = 0
#> 2    Y        2   Y*[*]      Y | X = 1
#> 
#> 
#> Number of types by node
#> X Y 
#> 2 4 
grab(model, object = "data_types")
#>      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
grab(model, object = "event_probabilities")
#> 
#> The probability of observing a given combination of data 
#> realizations for a given set of parameter values.
#> 
#>      event_probs
#> X0Y0        0.25
#> X1Y0        0.25
#> X0Y1        0.25
#> X1Y1        0.25
grab(model, object = "ambiguities_matrix")
#>       X0Y0 X1Y0 X0Y1 X1Y1
#> X0Y00    1    0    0    0
#> X1Y00    0    1    0    0
#> X0Y10    0    0    1    0
#> X1Y10    0    1    0    0
#> X0Y01    1    0    0    0
#> X1Y01    0    0    0    1
#> X0Y11    0    0    1    0
#> X1Y11    0    0    0    1
grab(model, object = "parameters")
#> Model parameters with associated probabilities: 
#> 
#> X.0 X.1 Y.00 Y.10 Y.01 Y.11
#> 0.5 0.5 0.25 0.25 0.25 0.25
grab(model, object = "parameter_names")
#> [1] "X.0"  "X.1"  "Y.00" "Y.10" "Y.01" "Y.11"
grab(model, object = "parameter_mapping")
#>      X0Y0 X1Y0 X0Y1 X1Y1
#> X.0     1    0    1    0
#> X.1     0    1    0    1
#> Y.00    1    1    0    0
#> Y.10    0    1    1    0
#> Y.01    1    0    0    1
#> Y.11    0    0    1    1
#> attr(,"map")
#>      X0Y0 X1Y0 X0Y1 X1Y1
#> X0Y0    1    0    0    0
#> X1Y0    0    1    0    0
#> X0Y1    0    0    1    0
#> X1Y1    0    0    0    1
grab(model, object = "parameter_matrix")
#> 
#> Rows are parameters, grouped in parameter sets
#> 
#> Columns are causal types
#> 
#> Cell entries indicate whether a parameter probability isused
#> in the calculation of causal type probability
#> 
#>      X0.Y00 X1.Y00 X0.Y10 X1.Y10 X0.Y01 X1.Y01 X0.Y11 X1.Y11
#> X.0       1      0      1      0      1      0      1      0
#> X.1       0      1      0      1      0      1      0      1
#> Y.00      1      1      0      0      0      0      0      0
#> Y.10      0      0      1      1      0      0      0      0
#> Y.01      0      0      0      0      1      1      0      0
#> Y.11      0      0      0      0      0      0      1      1
#> 
#>  
#>  param_set  (P)
#>  
grab(model, object = "prior_hyperparameters")
#>  X.0  X.1 Y.00 Y.10 Y.01 Y.11 
#>    1    1    1    1    1    1 
grab(model, object = "prior_distribution")
#> Prior distribution added to model
#> Summary statistics of model parameter prior distributions:
#> Dimensions: 4000 rows (draws) by 6 cols (parameters) 
#> 
#> Summary: 
#> 
#>      mean   sd
#> X.0  0.50 0.29
#> X.1  0.50 0.29
#> Y.00 0.25 0.20
#> Y.10 0.25 0.19
#> Y.01 0.25 0.19
#> Y.11 0.25 0.20
grab(model, object = "posterior_distribution")
#> Summary statistics of model parameter posterior distributions:
#> : 4000 rows (draws) by 6 cols (parameters)
#> 
#>      mean   sd
#> X.0  0.50 0.29
#> X.1  0.50 0.29
#> Y.00 0.25 0.20
#> Y.10 0.25 0.20
#> Y.01 0.25 0.19
#> Y.11 0.25 0.19
grab(model, object = "posterior_event_probabilities")
#> 
#> Posterior draws of event probabilities (transformed parameters)
#> 
#> Dimensions: 4000 rows (draws) by 4 cols (data types)
#> 
#> Summary: 
#> 
#>       mean    sd
#> X0Y0 0.250 0.192
#> X1Y0 0.250 0.194
#> X0Y1 0.250 0.197
#> X1Y1 0.249 0.193
grab(model, object = "stan_objects")
#> $data
#> NULL
#> 
#> $stanfit
#> 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.50    0.00 0.29   0.03  0.25  0.50  0.75  0.98  3337    1
#> lambdas[2]  0.50    0.00 0.29   0.02  0.25  0.50  0.75  0.97  3337    1
#> lambdas[3]  0.25    0.00 0.20   0.01  0.09  0.21  0.38  0.71  1871    1
#> lambdas[4]  0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.73  4143    1
#> lambdas[5]  0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.72  4744    1
#> lambdas[6]  0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.70  4148    1
#> w[1]        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3049    1
#> w[2]        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.71  3346    1
#> w[3]        0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.71  3058    1
#> w[4]        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3112    1
#> types[1]    0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.49  2080    1
#> types[2]    0.13    0.00 0.14   0.00  0.03  0.08  0.19  0.50  2297    1
#> types[3]    0.13    0.00 0.14   0.00  0.02  0.08  0.18  0.50  3135    1
#> types[4]    0.12    0.00 0.13   0.00  0.02  0.08  0.18  0.48  3964    1
#> types[5]    0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3969    1
#> types[6]    0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3640    1
#> types[7]    0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.49  3854    1
#> types[8]    0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.50  3773    1
#> lp__       -7.59    0.05 1.67 -11.75 -8.46 -7.23 -6.37 -5.42  1004    1
#> 
#> Samples were drawn using NUTS(diag_e) at Tue Mar 26 22:39:43 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).
#> 
#> $type_distribution
#> Posterior draws of causal types (transformed parameters)
#> Dimensions: 4000 rows (draws) by 8 cols (types) 
#> 
#> Summary: 
#> 
#>         mean    sd
#> X0.Y00 0.126 0.133
#> X1.Y00 0.128 0.136
#> X0.Y10 0.127 0.139
#> X1.Y10 0.122 0.132
#> X0.Y01 0.124 0.130
#> X1.Y01 0.125 0.133
#> X0.Y11 0.124 0.132
#> X1.Y11 0.124 0.133
#> 
#> $event_probabilities
#> 
#> Posterior draws of event probabilities (transformed parameters)
#> 
#> Dimensions: 4000 rows (draws) by 4 cols (data types)
#> 
#> Summary: 
#> 
#>       mean    sd
#> X0Y0 0.250 0.192
#> X1Y0 0.250 0.194
#> X0Y1 0.250 0.197
#> X1Y1 0.249 0.193
#> 
#> $stan_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
#> X.0         0.50    0.00 0.29   0.03  0.25  0.50  0.75  0.98  3337    1
#> X.1         0.50    0.00 0.29   0.02  0.25  0.50  0.75  0.97  3337    1
#> Y.00        0.25    0.00 0.20   0.01  0.09  0.21  0.38  0.71  1871    1
#> Y.10        0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.73  4143    1
#> Y.01        0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.72  4744    1
#> Y.11        0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.70  4148    1
#> X0Y0        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3049    1
#> X1Y0        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.71  3346    1
#> X0Y1        0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.71  3058    1
#> X1Y1        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3112    1
#> X0.Y00      0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.49  2080    1
#> X1.Y00      0.13    0.00 0.14   0.00  0.03  0.08  0.19  0.50  2297    1
#> X0.Y10      0.13    0.00 0.14   0.00  0.02  0.08  0.18  0.50  3135    1
#> X1.Y10      0.12    0.00 0.13   0.00  0.02  0.08  0.18  0.48  3964    1
#> X0.Y01      0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3969    1
#> X1.Y01      0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3640    1
#> X0.Y11      0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.49  3854    1
#> X1.Y11      0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.50  3773    1
#> lp__       -7.59    0.05 1.67 -11.75 -8.46 -7.23 -6.37 -5.42  1004    1
#> 
#> Samples were drawn using NUTS(diag_e) at Tue Mar 26 22:39:43 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).
#> 
#> attr(,"class")
#> [1] "stan_objects" "list"        
grab(model, object = "stan_fit")
#> 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.50    0.00 0.29   0.03  0.25  0.50  0.75  0.98  3337    1
#> lambdas[2]  0.50    0.00 0.29   0.02  0.25  0.50  0.75  0.97  3337    1
#> lambdas[3]  0.25    0.00 0.20   0.01  0.09  0.21  0.38  0.71  1871    1
#> lambdas[4]  0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.73  4143    1
#> lambdas[5]  0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.72  4744    1
#> lambdas[6]  0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.70  4148    1
#> w[1]        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3049    1
#> w[2]        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.71  3346    1
#> w[3]        0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.71  3058    1
#> w[4]        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3112    1
#> types[1]    0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.49  2080    1
#> types[2]    0.13    0.00 0.14   0.00  0.03  0.08  0.19  0.50  2297    1
#> types[3]    0.13    0.00 0.14   0.00  0.02  0.08  0.18  0.50  3135    1
#> types[4]    0.12    0.00 0.13   0.00  0.02  0.08  0.18  0.48  3964    1
#> types[5]    0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3969    1
#> types[6]    0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3640    1
#> types[7]    0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.49  3854    1
#> types[8]    0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.50  3773    1
#> lp__       -7.59    0.05 1.67 -11.75 -8.46 -7.23 -6.37 -5.42  1004    1
#> 
#> Samples were drawn using NUTS(diag_e) at Tue Mar 26 22:39:43 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).
grab(model, object = "stan_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
#> X.0         0.50    0.00 0.29   0.03  0.25  0.50  0.75  0.98  3337    1
#> X.1         0.50    0.00 0.29   0.02  0.25  0.50  0.75  0.97  3337    1
#> Y.00        0.25    0.00 0.20   0.01  0.09  0.21  0.38  0.71  1871    1
#> Y.10        0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.73  4143    1
#> Y.01        0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.72  4744    1
#> Y.11        0.25    0.00 0.19   0.01  0.09  0.20  0.37  0.70  4148    1
#> X0Y0        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3049    1
#> X1Y0        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.71  3346    1
#> X0Y1        0.25    0.00 0.20   0.01  0.09  0.20  0.37  0.71  3058    1
#> X1Y1        0.25    0.00 0.19   0.01  0.09  0.21  0.37  0.70  3112    1
#> X0.Y00      0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.49  2080    1
#> X1.Y00      0.13    0.00 0.14   0.00  0.03  0.08  0.19  0.50  2297    1
#> X0.Y10      0.13    0.00 0.14   0.00  0.02  0.08  0.18  0.50  3135    1
#> X1.Y10      0.12    0.00 0.13   0.00  0.02  0.08  0.18  0.48  3964    1
#> X0.Y01      0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3969    1
#> X1.Y01      0.13    0.00 0.13   0.00  0.03  0.08  0.18  0.48  3640    1
#> X0.Y11      0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.49  3854    1
#> X1.Y11      0.12    0.00 0.13   0.00  0.03  0.08  0.18  0.50  3773    1
#> lp__       -7.59    0.05 1.67 -11.75 -8.46 -7.23 -6.37 -5.42  1004    1
#> 
#> Samples were drawn using NUTS(diag_e) at Tue Mar 26 22:39:43 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).
grab(model, object = "type_prior")
#> Prior distribution added to model
#> Summary statistics of causal type prior distributions:
#> Dimensions: 4000 rows (draws) by 8 cols (types) 
#> 
#> Summary: 
#> 
#>         mean    sd
#> X0.Y00 0.126 0.134
#> X1.Y00 0.128 0.135
#> X0.Y10 0.122 0.132
#> X1.Y10 0.122 0.130
#> X0.Y01 0.124 0.135
#> X1.Y01 0.127 0.136
#> X0.Y11 0.124 0.131
#> X1.Y11 0.126 0.132
grab(model, object = "type_posterior")
#> Posterior draws of causal types (transformed parameters)
#> Dimensions: 4000 rows (draws) by 8 cols (types) 
#> 
#> Summary: 
#> 
#>         mean    sd
#> X0.Y00 0.126 0.133
#> X1.Y00 0.128 0.136
#> X0.Y10 0.127 0.139
#> X1.Y10 0.122 0.132
#> X0.Y01 0.124 0.130
#> X1.Y01 0.125 0.133
#> X0.Y11 0.124 0.132
#> X1.Y11 0.124 0.133

# Example of arguments passed on to helpers
grab(model,
  object = "event_probabilities",
  parameters = c(.6, .4, .1, .1, .7, .1))
#> 
#> The probability of observing a given combination of data 
#> realizations for a given set of parameter values.
#> 
#>      event_probs
#> X0Y0        0.48
#> X1Y0        0.08
#> X0Y1        0.12
#> X1Y1        0.32

# }