Learn R Programming

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

CausalQueries

https://integrated-inferences.github.io/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 makes use of dagitty functionality. 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

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")

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 a generalization of the biqq models described in "Mixing Methods: A Bayesian Approach" (Humphreys and Jacobs, 2015). The conceptual extension makes use of work on probabilistic causal models described in Pearl's Causality (Pearl, 2009). The approach to generating a generic stan function that can take data from arbitrary models was developed in key contributions by Jasper Cooper and Georgiy Syunyaev. Lily Medina did the magical work of pulling it all together and developing approaches to characterizing confounding and defining estimands. Julio Solis has done wonders to simplify the specification of priors.

Copy Link

Version

Install

install.packages('CausalQueries')

Monthly Downloads

1,351

Version

1.0.2

License

MIT + file LICENSE

Maintainer

Till Tietz

Last Published

January 15th, 2024

Functions in CausalQueries (1.0.2)

decreasing

Make monotonicity statement (negative)
get_parmap

Get parmap: a matrix mapping from parameters to data types
default_stan_control

default_stan_control
clean_param_vector

Clean parameter vector
collapse_data

Make compact data with data strategies
expand_wildcard

Expand wildcard
get_param_dist

Get a distribution of model parameters
clean_params

Check parameters sum to 1 in param_set; normalize if needed; add names if needed
get_type_distributions

helper to get type distributions
get_parameter_matrix

Get parameter matrix
get_causal_types

Get causal types
construct_commands_other_args

make_par_values
get_estimands

helper to get estimands
get_query_types

Look up query types
expand_nodal_expression

Helper to expand nodal expression
get_ambiguities_matrix

Get ambiguities matrix
get_type_prob_multiple

Draw matrix of type probabilities, before or after estimation
get_event_prob

Draw event probabilities
get_type_prob_c

generates one draw from type probability distribution for each type in P
make_data

Make data
get_data_families

get_data_families
collapse_nodal_types

collapse nodal types
construct_commands_param_names

make_par_values
get_nodal_types

Get list of types for nodes in a DAG
increasing

Make monotonicity statement (positive)
get_type_prob

Get type probabilities
get_type_names

Get type names
make_prior_distribution

Make a prior distribution from priors
minimal_data

Creates a data frame for case with no data
is_a_model

Check whether argument is a model
make_data_single

Generate full dataset
lipids_data

Lipids: Data for Chickering and Pearl replication
make_parameter_matrix

Make parameter matrix
get_prior_distribution

Get a prior distribution from priors
make_ambiguities_matrix

Make ambiguities matrix
make_par_values_stops

make_par_values_stops
nodes_in_statement

Identify nodes in a statement
complements

Make statement for complements
list_non_parents

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

Make statement for any interaction
non_decreasing

Make monotonicity statement (non negative)
institutions_data

Institutions and growth: Data for replication of analysis in *Integrated Inferences*
restrict_by_query

Reduce nodal types using statement
interpret_type

Interpret or find position in nodal type
make_parameters_df

function to make a parameters_df from nodal types
set_parmap

Set parmap: a matrix mapping from parameters to data types
set_restrictions

Restrict a model
set_parameter_matrix

Set parameter matrix
reveal_outcomes

Reveal outcomes
restrict_by_labels

Reduce nodal types using labels
make_parmap

Make parmap: a matrix mapping from parameters to data types
realise_outcomes

Realise outcomes
make_nodal_types

Make nodal types
prep_stan_data

Prepare data for 'stan'
democracy_data

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

Add prior distribution draws
prior_setting

Setting priors
substitutes

Make statement for substitutes
draw_causal_type

Draw a single causal type given a parameter vector
get_parents

Get list of parents of all nodes in a model
construct_commands_alter_at

make_par_values
non_increasing

Make monotonicity statement (non positive)
get_parameter_names

Get parameter names
observe_data

Observe data, given a strategy
st_within

Get string between two regular expression patterns
get_type_prob_multiple_c

generates n draws from type probability distribution for each type in P
gsub_many

Recursive substitution
make_events

Make data in compact form
perm

Produces the possible permutations of a set of nodes
make_par_values

make_par_values
plot_dag

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

Calculate query distribution
query_model

Generate estimands dataframe
te

Make treatment effect statement (positive)
update_model

Fit causal model using 'stan'
type_matrix

Generate type matrix
update_causal_types

Update causal types based on nodal types
query_to_expression

Helper to turn query into a data expression
queries_to_types

helper to get types from queries
uncollapse_nodal_types

uncollapse nodal types
make_model

Make a model
unpack_wildcard

Unpack a wild card
minimal_event_data

Creates a compact data frame for case with no data
parameter_setting

Setting parameters
set_ambiguities_matrix

Set ambiguity matrix
parents_to_int

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

n_check
set_confound

Set confound
set_sampling_args

set_sampling_args From 'rstanarm' (November 1st, 2019)
simulate_data

simulate_data is an alias for make_data
CausalQueries_internal_inherit_params

Create parameter documentation to inherit
all_data_types

All data types
CausalQueries-package

'CausalQueries'
check_query

Warn about improper query specification and apply fixes
add_wildcard

Adds a wildcard for every missing parent
add_dots

Helper to fill in missing do operators in causal expression
causal_type_names

Names for causal types
clean_condition

Clean condition
check_string_input

Check string_input
check_args

helper to check arguments
data_to_data

helper to generate a matrix mapping from names of M to names of A
drop_empty_families

Drop empty families
expand_data

Expand compact data object to data frame
data_type_names

Data type names