Learn R Programming

⚠️There's a newer version (1.3.3) of this package.Take me there.

CausalQueries

CausalQueries is a package that lets you declare binary causal models, update beliefs about causal types given data and calculate arbitrary estimands. Model definition is implemented via a dagitty style syntax. Updating is implemented in stan.

  • See vignettes for a guide to getting started.

  • See here for a guide to using CausalQueries along with many examples of causal models

  • See website for a comprehensive overview of CausalQueries

Installation

To install the latest stable release of CausalQueries:

install.packages("CausalQueries")

To install the latest development release :

install.packages("devtools")
devtools::install_github("integrated-inferences/CausalQueries")

Helping Out & Contributing

CausalQueries is an open and active developer community. We welcome contributions and are excited you are keen to get involved. Please refer to CONTRIBUTING.md to get started.

Causal Models

Causal models are defined by:

  • A directed acyclic graph (DAG), which provides the set of variables, a causal ordering between them, and a set of assumptions regarding conditional independence. If there is no arrow from A to B then a change in A never induces a change in B.
  • Functional forms. Functional forms describe the causal relationships between nodes. You often have to make strong assumptions when you specify a functional form; fortunately however if variables are categorical we do not need functional forms in the usual sense. The DAG implies a set of "causal types." Units can be classed together as of the same causal type if they respond to the same way to other variables. For instance, a type might be the set of units for which X=1 and for which Y=1 if and only if X=1. The set of causal types grows rapidly with the number of nodes and the number of nodes pointing into any given node. In this setting imposing functional forms is the same as placing restrictions on causal types: such restrictions reduce complexity but require substantive assumptions. An example of a restriction might be "Y is monotonic in X."
  • Priors. In the standard case, the DAG plus any restrictions imply a set of parameters that combine to form causal types. These are the parameters we want to learn about. To learn about them we first provide priors over the parameters. With priors specified the causal model is complete (it is a "probabilistic causal model") and we are ready for inference. Setting priors is done using the set_priors function and many examples can be seen by typing ? set_priors.R.

A wrinkle:

  • It is possible that nodes are related in ways not captured by the DAG. In such cases dotted curves are sometimes placed between nodes on a graph. It is possible to specify such possible unobservable confounding in the causal model. This has implications for the parameter space.

Inference

Our goal is to form beliefs over parameters but also over more substantive estimands:

  • With a causal model in hand and data available about some or all of the nodes, it is possible to make use of a generic stan model that generates posteriors over the parameter vector.

  • Given updated (or prior) beliefs about parameters it is possible to calculate causal estimands of inference from a causal model. For example "What is the probability that X was the cause of Y given X=1, Y=1 and Z=1."

Credits etc

The approach used in CausalQueries is developed in Humphreys and Jacobs 2023 drawing on work on probabilistic causal models described in Pearl's Causality (Pearl, 2009). We thank Ben Goodrich who provided generous insights on using stan for this project. We thank Alan M Jacobs for key work developing the framework underlying the package. Our thanks to Jasper Cooper for contributions generating a generic function to create Stan code, to Clara Bicalho who helped figure out the syntax for causal statements, to Julio S. Solís Arce who made many key contributions figuring out how to simplify the specification of priors, and to Merlin Heidemanns who figured out the rstantools integration and made myriad code improvements.

Copy Link

Version

Install

install.packages('CausalQueries')

Monthly Downloads

709

Version

1.2.1

License

MIT + file LICENSE

Maintainer

Till Tietz

Last Published

November 6th, 2024

Functions in CausalQueries (1.2.1)

data_helpers

Data helpers
add_dots

Helper to fill in missing do operators in causal expression
construct_commands_other_args

make_par_values
construct_commands_param_names

make_par_values
get_type_distributions

helper to get type distributions
get_event_probabilities

Draw event probabilities
make_model

Make a model
get_query_types

Look up query types
make_par_values

make_par_values
check_args

helper to check arguments
make_par_values_stops

make_par_values_stops
make_data_single

Generate full dataset
construct_commands_alter_at

make_par_values
get_parameter_matrix

Get parameter matrix
inspection

Helpers for inspecting causal models
institutions_data

Institutions and growth: Data for replication of analysis in *Integrated Inferences*
print.causal_model

Print a short summary for a causal model
print.model_query

Print a tightened summary of model queries
make_parameters_df

function to make a parameters_df from nodal types
draw_causal_type

Draw a single causal type given a parameter vector
CausalQueries

'CausalQueries'
set_confound

Set confound
perm

Produces the possible permutations of a set of nodes
make_prior_distribution

Make a prior distribution from priors
democracy_data

Development and Democratization: Data for replication of analysis in *Integrated Inferences*
CausalQueries_internal_inherit_params

Create parameter documentation to inherit
query_distribution

Calculate query distribution
get_all_data_types

Get all data types
parents_to_int

Helper to turn parents_list into a list of data_realizations column positions
set_parameter_matrix

Set parameter matrix
prep_stan_data

Prepare data for 'stan'
expand_nodal_expression

Helper to expand nodal expression
plot_model

Plots a DAG in ggplot style using a causal model input
query_helpers

Query helpers
summary.causal_model

Summarizing causal models
summary.model_query

Summarizing model queries
list_non_parents

Returns a list with the nodes that are not directly pointing into a node
interpret_type

Interpret or find position in nodal type
get_estimands

helper to get estimands
lipids_data

Lipids: Data for Chickering and Pearl replication
make_dag

Helper to run a causal statement specifying a DAG into a data.frame of pairwise parent child relations between nodes specified by a respective edge.
observe_data

Observe data, given a strategy
realise_outcomes

Realise outcomes
reveal_outcomes

Reveal outcomes
prior_setting

Setting priors
queries_to_types

helper to get types from queries
update_model

Fit causal model using 'stan'
parameter_setting

Setting parameters
set_prior_distribution

Add prior distribution draws
set_restrictions

Restrict a model
query_model

Generate estimands data frame
query_to_expression

Helper to turn query into a data expression
clean_statement

Helper to clean and check the validity of causal statements specifying a DAG. This function isolates nodes and edges specified in a causal statements and makes them processable by make_dag