counterfactual {R6causal}R Documentation

Counterfactual inference via simulation

Description

Counterfactual inference via simulation

Usage

counterfactual(
  scm,
  situation,
  n,
  target = NULL,
  ifunction = NULL,
  method = NULL,
  returnscm = FALSE,
  control = NULL
)

Arguments

scm

An SCM object

situation

A list or a character string. The list has the following elements:

  • do : NULL or a list containing named elements 'target' and 'ifunction' that specify the intervention carried out in the situation

  • dolist : NULL or a list of lists containing named elements 'target' and 'ifunction' that specify the intervention carried out in each parallel world

  • condition : either a string that gives an SQL query ( e.g. "select x,y,z from DATA where" ) or a data.table consisting of the valid rows ( e.g. data.table::data.table( x = 0, y = 0))

  • condition_type : (required only if method == "u_find") A character vector giving the type ("continuous" or "discrete") of every variable in situation$condition

n

The number of rows in the data to be simulated

target

NULL or a vector of variable names that specify the target variable(s) of the counterfactual intervention.

ifunction

NULL or a list of functions for the counterfactual intervention.

method

The simulation method, "u_find", "rejection" or "analytic_linear_gaussian"

returnscm

A logical, should the internally created twin SCM or parallel world SCM returned?

control

List of parameters to be passed to the simulation method:

  • batchsize: (u_find, rejection) The size of data from n observations are resampled (default n)

  • max_iterations: (u_find) The maximum number of iterations for the binary search (default 50)

  • minu: (u_find) A scalar or a named list that specifies the lower starting value for the binary search (default -10)

  • maxu: (u_find) A scalar or a named list that specifies the upper starting value for the binary search (default 10)

  • sampling_replace: (u_find) Logical, resampling with replacement? (default TRUE)

  • nonunique_jittersd: (u_find) Standard deviation of the noise to be added to the output (default NULL meaning no noise)

  • maxbatchs: (u_find, rejection) The maximum number of batches for rejection sampling (for discrete variables)

  • weightfunction: (u_find) A function or a named list of functions to be applied to dedicated error terms to obtain the resampling weights (default stats::dnorm)

Value

A data table representing the situation after the counterfactual intervention

Examples

cfdata <- counterfactual(backdoor,
                         situation = list(
                             do = list(target = "x", ifunction = 0),
                             condition = data.table::data.table( x = 0, y = 0)),
                         target = "x",
                         ifunction = 1,
                         method = "rejection",
                         n = 1000)
mean(cfdata$y)

backdoor_parallel <- ParallelWorld$new(backdoor,
                                       dolist=list(
                                         list(target = "x", ifunction = 0),
                                         list(target = list("z","x"), ifunction = list(1,0))
                                       )
)
cfdata2 <- counterfactual(backdoor_parallel,
                         situation = list(
                             do = NULL,
                             condition = data.table::data.table( y = 0, y_1 = 0, y_2 = 0)),
                         target = "x",
                         ifunction = 1,
                         method = "rejection",
                         n = 1000)
mean(cfdata2$y)

[Package R6causal version 0.8.3 Index]