solution

We print out the inference table and then provide a helper function to let you quickly see the conditional inferences.

You can also see the solutions in Chapter 12 or by using the shiny.

1 Set up and helper function

Code
library(CausalQueries)
library(knitr)
library(tidyverse)

data_consistent <- function(my_table, seen = c("S1", "Y0")) {
  for(j in seen){
    my_table <- 
    my_table[c(names(my_table)[grepl(j,  names(my_table))], "in_query",   "priors")]
  }

  my_table <- my_table |> filter(
    my_table |> select(-in_query, -priors) |> apply(1, sum) > 0)

  my_table |> select(-in_query, -priors)
  
  pDQ <- my_table |> filter(in_query) |> pull(priors) |> sum()
  pD  <- my_table |> pull(priors) |> sum()
  print(c("prob of Data and Query:", pDQ))
  print(c("prob of Data:", pD))
  print(c("prob Query | Data:", pDQ/pD))
  
  print(my_table)

}  

2 Model parameters

Check that the model has the parameters we want!

Code
model <- 
  make_model("S -> C -> Y <- R <- X; X -> C -> R") |>
  set_restrictions(labels = list(C = c('1110', '1111'), 
                                 R = c('0000', '0001'), 
                                 Y = '0001'),
                 keep = TRUE)

query <- "Y[X=1] > Y[X=0]"

model$parameters_df |> kable()
param_names node gen param_set nodal_type given param_value priors
1 S.0 S 1 S 0 0.5 1
2 S.1 S 1 S 1 0.5 1
3 X.0 X 2 X 0 0.5 1
4 X.1 X 2 X 1 0.5 1
12 C.1110 C 3 C 1110 0.5 1
20 C.1111 C 3 C 1111 0.5 1
21 R.0000 R 4 R 0000 0.5 1
29 R.0001 R 4 R 0001 0.5 1
45 Y.0001 Y 5 Y 0001 1.0 1
Code
my_table <-
  model  |>
  grab(what = "ambiguities_matrix") |>
  data.frame() |>
  mutate(
    in_query = get_query_types(model, query)$types,
    priors   = CausalQueries:::get_type_prob(model)) 

3 Look up table for conditional inferences

Code
my_table |> kable()
S1X1C0R0Y0 S0X0C1R0Y0 S1X0C1R0Y0 S0X1C1R0Y0 S1X1C1R0Y0 S0X1C1R1Y1 S1X1C1R1Y1 in_query priors
S0X0C1110R0000Y0001 0 1 0 0 0 0 0 FALSE 0.0625
S1X0C1110R0000Y0001 0 0 1 0 0 0 0 FALSE 0.0625
S0X1C1110R0000Y0001 0 0 0 1 0 0 0 FALSE 0.0625
S1X1C1110R0000Y0001 1 0 0 0 0 0 0 FALSE 0.0625
S0X0C1111R0000Y0001 0 1 0 0 0 0 0 FALSE 0.0625
S1X0C1111R0000Y0001 0 0 1 0 0 0 0 FALSE 0.0625
S0X1C1111R0000Y0001 0 0 0 1 0 0 0 FALSE 0.0625
S1X1C1111R0000Y0001 0 0 0 0 1 0 0 FALSE 0.0625
S0X0C1110R0001Y0001 0 1 0 0 0 0 0 TRUE 0.0625
S1X0C1110R0001Y0001 0 0 1 0 0 0 0 FALSE 0.0625
S0X1C1110R0001Y0001 0 0 0 0 0 1 0 TRUE 0.0625
S1X1C1110R0001Y0001 1 0 0 0 0 0 0 FALSE 0.0625
S0X0C1111R0001Y0001 0 1 0 0 0 0 0 TRUE 0.0625
S1X0C1111R0001Y0001 0 0 1 0 0 0 0 TRUE 0.0625
S0X1C1111R0001Y0001 0 0 0 0 0 1 0 TRUE 0.0625
S1X1C1111R0001Y0001 0 0 0 0 0 0 1 TRUE 0.0625

4 Examples of conditional inferences

(using a helper funciton to display the subset of the data we care about)

An inference:

4.1 I observe \(S=0, Y=0\) only:

Code
  data_consistent(my_table, seen = c("S0", "Y0"))
[1] "prob of Data and Query:" "0.125"                  
[1] "prob of Data:" "0.375"        
[1] "prob Query | Data:" "0.333333333333333" 
                    S0X0C1R0Y0 S0X1C1R0Y0 in_query priors
S0X0C1110R0000Y0001          1          0    FALSE 0.0625
S0X1C1110R0000Y0001          0          1    FALSE 0.0625
S0X0C1111R0000Y0001          1          0    FALSE 0.0625
S0X1C1111R0000Y0001          0          1    FALSE 0.0625
S0X0C1110R0001Y0001          1          0     TRUE 0.0625
S0X0C1111R0001Y0001          1          0     TRUE 0.0625

4.2 I observe \(S = 1, X = 1, C = 0\)

Code
data_consistent(my_table, seen = c("S1", "X1", "C0"))
[1] "prob of Data and Query:" "0"                      
[1] "prob of Data:" "0.125"        
[1] "prob Query | Data:" "0"                 
                    S1X1C0R0Y0 in_query priors
S1X1C1110R0000Y0001          1    FALSE 0.0625
S1X1C1110R0001Y0001          1    FALSE 0.0625