knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
if(!requireNamespace("fabricatr", quietly = TRUE)) {
  install.packages("fabricatr")
}

library(CausalQueries)
library(fabricatr)
library(knitr)
library(ggplot2)
library(rstan)
library(bayesplot)
rstan_options(refresh = 0)

Accessing the posterior

When you update a model using CausalQueries, CausalQueries generates and updates a stan model and saves the posterior distribution over parameters in the model.

The basic usage is:

data <- data.frame(X = rep(c(0:1), 10), Y = rep(c(0:1), 10))

model <- make_model("X -> Y") |> 
  update_model(data)
data <- data.frame(X = rep(c(0:1), 10), Y = rep(c(0:1), 10))

model <- make_model("X -> Y") |> 
  update_model(data, refresh = 0)

The posterior over parameters can be accessed thus:

grab(model, "posterior_distribution")

When querying a model you can request use of the posterior distribution with the using argument:

model |> 
  query_model(
    query = "Y[X=1] > Y[X=0]",
    using = c("priors", "posteriors")) |>
  kable(digits = 2)

Summary of stan performance

You can access a summary of the parameter values and convergence information as produced by stan thus:

grab(model, "stan_summary")

This summary provides information on the distribution of parameters as well as convergence diagnostics, summarized in the Rhat column. In the printout above the first 6 rows show the distribution of the model parameters; the next 8 rows show the distribution over transformed parameters, here the causal types. The last row shows the unnormalized log density on Stan's unconstrained space which, as described in Stan documentation is intended to diagnose sampling efficiency and evaluate approximations.

See stan documentation for further details.

Advanced diagnostics

If you are interested in advanced diagnostics of performance you can save and access the raw stan output.

model <- make_model("X -> Y") |> 
  update_model(data, keep_fit = TRUE)
model <- make_model("X -> Y") |> 
  update_model(data, refresh = 0, keep_fit = TRUE)

Note that the summary for this raw output shows the labels used in the generic stan model: lambda for the vector of parameters, corresponding to the parameters in the parameters dataframe (grab(model, "parameters_df")), and , if saved, a vector types for the causal types (see grab(model, "causal_types")) and w for the event probabilities (grab(model, "event_probabilities")).

model |> grab("stan_fit")

You can then use diagnostic packages such as bayesplot.

model |> grab("stan_fit") |>
  bayesplot::mcmc_pairs(pars = c("lambdas[3]", "lambdas[4]", "lambdas[5]", "lambdas[6]"))
np <- model |> grab("stan_fit") |> bayesplot::nuts_params()
head(np) |> kable()

model |> grab("stan_fit") |>
  bayesplot::mcmc_trace(pars = "lambdas[5]", np = np) 


macartan/gbiqq documentation built on April 28, 2024, 10:07 p.m.