if(!requireNamespace("fabricatr", quietly = TRUE)) {
install.packages("fabricatr")
}
library(CausalQueries)
library(fabricatr)
library(knitr)
Make a model
Generating: To make a model you need to provide a
DAG statement to make_model
.
For instance
"X->Y"
-
"X -> M -> Y <- X"
or
-
"Z -> X -> Y <-> X"
.
# examples of models
xy_model <- make_model("X -> Y")
iv_model <- make_model("Z -> X -> Y <-> X")
Graphing: Once you have made a model you can inspect the DAG:
plot(iv_model)
Inspecting: The model has a set of parameters and a default distribution over these.
param_names | node | gen | param_set | nodal_type | given | param_value | priors |
---|---|---|---|---|---|---|---|
X.0 | X | 1 | X | 0 | 0.50 | 1 | |
X.1 | X | 1 | X | 1 | 0.50 | 1 | |
Y.00 | Y | 2 | Y | 00 | 0.25 | 1 | |
Y.10 | Y | 2 | Y | 10 | 0.25 | 1 | |
Y.01 | Y | 2 | Y | 01 | 0.25 | 1 | |
Y.11 | Y | 2 | Y | 11 | 0.25 | 1 |
Tailoring: These features can be edited using
set_restrictions
, set_priors
and
set_parameters
. Here is an example of setting a
monotonicity restriction (see ?set_restrictions
for
more):
Here is an example of setting a monotonicity restriction (see
?set_restrictions
for more):
iv_model <-
iv_model |> set_restrictions(decreasing('Z', 'X'))
Here is an example of setting priors (see ?set_priors
for more):
iv_model <-
iv_model |> set_priors(distribution = "jeffreys")
#> No specific parameters to alter values for specified. Altering all parameters.
Simulation: Data can be drawn from a model like this:
Z | X | Y |
---|---|---|
0 | 0 | 1 |
0 | 1 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
Model updating
Updating: Update using update_model
.
You can pass all rstan
arguments to
update_model
.
df <- fabricatr::fabricate(N = 100, X = rbinom(N, 1, .5), Y = rbinom(N, 1, .25 + X*.5))
xy_model <-
xy_model |>
update_model(df, refresh = 0)
Inspecting: You can access the posterior distribution on model parameters directly thus:
X.0 | X.1 | Y.00 | Y.10 | Y.01 | Y.11 |
---|---|---|---|---|---|
0.4583398 | 0.5416602 | 0.0902439 | 0.0259462 | 0.7277952 | 0.1560146 |
0.5012633 | 0.4987367 | 0.0670226 | 0.1051066 | 0.7410940 | 0.0867768 |
0.5174689 | 0.4825311 | 0.1416548 | 0.0045211 | 0.6543231 | 0.1995009 |
0.5032772 | 0.4967228 | 0.0884517 | 0.0612466 | 0.7021519 | 0.1481497 |
0.5075592 | 0.4924408 | 0.1240017 | 0.1007875 | 0.5510330 | 0.2241778 |
0.5008444 | 0.4991556 | 0.2080583 | 0.0739082 | 0.5439208 | 0.1741127 |
where each row is a draw of parameters.
Query model
Querying: You ask arbitrary causal queries of the model.
Examples of unconditional queries:
xy_model |>
query_model("Y[X=1] > Y[X=0]", using = c("priors", "posteriors")) |>
kable()
query | given | using | case_level | mean | sd | cred.low | cred.high |
---|---|---|---|---|---|---|---|
Y[X=1] > Y[X=0] | - | priors | FALSE | 0.2522700 | 0.1950491 | 0.0097360 | 0.7221671 |
Y[X=1] > Y[X=0] | - | posteriors | FALSE | 0.6637382 | 0.0808784 | 0.4953843 | 0.8047836 |
Examples of conditional queries:
xy_model |>
query_model("Y[X=1] > Y[X=0]", using = c("priors", "posteriors"),
given = "X==1 & Y == 1") |>
kable()
query | given | using | case_level | mean | sd | cred.low | cred.high |
---|---|---|---|---|---|---|---|
Y[X=1] > Y[X=0] | X==1 & Y == 1 | priors | FALSE | 0.5076127 | 0.2914743 | 0.0260412 | 0.9806968 |
Y[X=1] > Y[X=0] | X==1 & Y == 1 | posteriors | FALSE | 0.8131268 | 0.0916832 | 0.6312169 | 0.9761565 |
Queries can even be conditional on counterfactual quantities. Here the probability of a positive effect given some effect:
xy_model |>
query_model("Y[X=1] > Y[X=0]", using = c("priors", "posteriors"),
given = "Y[X=1] != Y[X=0]") |>
kable()
query | given | using | case_level | mean | sd | cred.low | cred.high |
---|---|---|---|---|---|---|---|
Y[X=1] > Y[X=0] | Y[X=1] != Y[X=0] | priors | FALSE | 0.5022578 | 0.2884026 | 0.0232768 | 0.9738952 |
Y[X=1] > Y[X=0] | Y[X=1] != Y[X=0] | posteriors | FALSE | 0.8902401 | 0.0613625 | 0.7676404 | 0.9912963 |