The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
if(!requireNamespace("fabricatr", quietly = TRUE)) {
install.packages("fabricatr")
}
library(CausalQueries)
library(fabricatr)
library(knitr)
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
<- make_model("X -> Y")
xy_model <- make_model("Z -> X -> Y <-> X") iv_model
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.
|> grab("parameters_df") |> kable() xy_model
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 |> set_restrictions(decreasing('Z', 'X')) iv_model
Here is an example of setting priors (see ?set_priors
for more):
<-
iv_model |> set_priors(distribution = "jeffreys")
iv_model #> No specific parameters to alter values for specified. Altering all parameters.
Simulation: Data can be drawn from a model like this:
<- make_data(iv_model, n = 4)
data
|> kable() data
Z | X | Y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 1 | 0 |
1 | 1 | 0 |
Updating: Update using update_model
.
You can pass all rstan
arguments to
update_model
.
<- fabricatr::fabricate(N = 100, X = rbinom(N, 1, .5), Y = rbinom(N, 1, .25 + X*.5))
df
<-
xy_model |>
xy_model update_model(df, refresh = 0)
Inspecting: You can access the posterior distribution on model parameters directly thus:
|> grab("posterior_distribution") |>
xy_model head() |> kable()
X.0 | X.1 | Y.00 | Y.10 | Y.01 | Y.11 |
---|---|---|---|---|---|
0.5827775 | 0.4172225 | 0.0233412 | 0.0701335 | 0.7357021 | 0.1708232 |
0.5642018 | 0.4357982 | 0.0235635 | 0.0810704 | 0.7693509 | 0.1260152 |
0.4762298 | 0.5237702 | 0.0531399 | 0.1344577 | 0.7478659 | 0.0645365 |
0.5994208 | 0.4005792 | 0.1813373 | 0.0074846 | 0.6458922 | 0.1652859 |
0.5107444 | 0.4892556 | 0.1725456 | 0.0276954 | 0.5015323 | 0.2982266 |
0.5321877 | 0.4678123 | 0.0033239 | 0.2271665 | 0.6855110 | 0.0839986 |
where each row is a draw of parameters.
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.2538711 | 0.1942515 | 0.0081513 | 0.7246379 |
Y[X=1] > Y[X=0] | - | posteriors | FALSE | 0.6842948 | 0.0774336 | 0.5148700 | 0.8156070 |
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.5009433 | 0.2870361 | 0.0253237 | 0.9714109 |
Y[X=1] > Y[X=0] | X==1 & Y == 1 | posteriors | FALSE | 0.8540662 | 0.0835568 | 0.6756953 | 0.9895221 |
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.4963668 | 0.2899275 | 0.0269070 | 0.9737914 |
Y[X=1] > Y[X=0] | Y[X=1] != Y[X=0] | posteriors | FALSE | 0.8956273 | 0.0597769 | 0.7776668 | 0.9945446 |
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.