| SCM {R6causal} | R Documentation |
R6 Class for structural causal models
Description
R6 Class for structural causal models
R6 Class for structural causal models
Details
An R6 class for structural causal models (SCM) with latent variables and missing data mechanism. There are methods for defining, printing, plotting, intervening and simulating SCMs.
Active bindings
vflistList of the structural functions of observed variables.
vnamesList of the names of observed variables.
vstarnamesList of the names of observed variables with NA's.
vfsymbList of the arguments of structural functions of observed variables.
uflistList of the structural functions of unobserved variables.
unamesList of the names of unobserved variables.
unames_dedicatedList of the names of unobserved variables that have only one child.
unames_confounderList of the names of unobserved variables that have two or more children.
dedicated_uNamed list of the names of unobserved variables that have only one child which is the name of the element.
is_linear_gaussianLogical, does the SCM have linear functions and Gaussian background variables?
rflistList of the structural functions of missingness indicators.
rfsymbList of the names of missingness indicators.
rprefixPrefix used to mark missingness indicators.
starsuffixSuffix used to mark variables with missing data.
simdataData table containing data simulated from the SCM.
simdata_obsData table containing data simulated from the SCM where missing values are indicated by
NA.igraphThe graph of the SCM in the
igraphform (without the missing data mechanism).igraph_nodedicatedThe graph of the SCM in the
igraphform (without the dedicated U variables and the missing data mechanism).igraph_bidirectedThe graph of the SCM in the
igraphform where latent variables are presented by bidirected arcs.igraph_mdThe graph of the SCM in the
igraphform including the missing data mechanism.toporderA vector giving the topological order of variables.
topordervA vector giving the topological order of observed variables.
graphtextA character string that gives the edges of the graph of the SCM (without the missing data mechanism).
graphtext_mdA character string that gives the edges of the graph of the SCM including the missing data mechanism.
nameThe name of the SCM.
Methods
Public methods
Method new()
Create a new SCM object.
Usage
SCM$new( name = "An SCM", uflist = NULL, vflist = NULL, rflist = NULL, rprefix = "R_", starsuffix = "_md" )
Arguments
nameName.
uflistA named list containing the functions for the background variables.
vflistA named list containing the functions for the observed variables.
rflistA named list containing the functions for missingness indicators.
rprefixThe prefix of the missingness indicators.
starsuffixThe suffix for variables with missing data.
Returns
A new 'SCM' object.
Examples
backdoor <- SCM$new("backdoor",
uflist = list(
uz = function(n) {return(stats::runif(n))},
ux = function(n) {return(stats::runif(n))},
uy = function(n) {return(stats::runif(n))}
),
vflist = list(
z = function(uz) {
return(as.numeric(uz < 0.4))},
x = function(ux, z) {
return(as.numeric(ux < 0.2 + 0.5*z))},
y = function(uy, z, x) {
return(as.numeric(uy < 0.1 + 0.4*z + 0.4*x))}
)
)
Method print()
Print a summmary of the SCM object.
Usage
SCM$print()
Examples
backdoor
Method plot()
Plot the DAG of the SCM object.
Usage
SCM$plot(subset = "uvr", method = "igraph", ...)
Arguments
subsetVariable groups to be plotted: "uvr", "u2vr","vr","uv", "u2v" or "v".
methodPlotting method: "qgraph" or "igraph".
...other parameters passed to the plotting method
Examples
backdoor$plot()
backdoor$plot("v")
Method tikz()
Return a TikZ code for drawing the DAG of the SCM object in LaTeX.
Usage
SCM$tikz( subset = "uvr", layoutfunction = igraph::layout_with_lgl, labels = NULL, settings = list(force = FALSE, borders = TRUE, shape = "circle", size = 5, scale = 2), ... )
Arguments
subsetVariable groups to be plotted: "uvr","vr","uv", or "v".
layoutfunctionA layout function from igraph package.
labelsA named list that gives the names of vertices in TikZ.
settingsA list with the following elements:
...Arguments to be passed to layoutfunction
Method pa()
Return the parents of a set of vertices.
Usage
SCM$pa(vnames, includeself = TRUE)
Arguments
vnamesA vector of vertex names
includeselfLogical, should
vnamesto be included in the results (defaults TRUE)
Method ch()
Return the children of a set of vertices.
Usage
SCM$ch(vnames, includeself = TRUE)
Arguments
vnamesA vector of vertex names
includeselfLogical, should
vnamesto be included in the results (defaults TRUE)
Method an()
Return the ancestors of a set of vertices.
Usage
SCM$an(vnames, includeself = TRUE)
Arguments
vnamesA vector of vertex names
includeselfLogical, should
vnamesto be included in the results (defaults TRUE)
Method de()
Return the descendants of a set of vertices.
Usage
SCM$de(vnames, includeself = TRUE)
Arguments
vnamesA vector of vertex names
includeselfLogical, should
vnamesto be included in the results (defaults TRUE)
Method add_variable()
Add a new variable to the SCM object.
Usage
SCM$add_variable( vfnew = NULL, ufnew = NULL, rfnew = NULL, rprefixnew = NULL, starsuffixnew = NULL )
Arguments
vfnewNULL or a named list containing the functions for the new observed variables.
ufnewNULL or a named list containing the functions for the new latent variables.
rfnewNULL or a named list containing the functions for the new missingness indicators.
rprefixnewNULL or the prefix of the missingness indicators.
starsuffixnewNULL orthe suffix for variables with missing data.
Examples
backdoor2 <- backdoor$clone()
backdoor2$add_variable(
vfnew = list(
w = function(uw, x) {
return(as.numeric(uw < 0.4 + 0.3*x))}),
ufnew = list(
uw = function(n) {return(stats::runif(n))})
)
Method remove_variable()
Remove variables from the SCM object.
Usage
SCM$remove_variable(variablenames)
Arguments
variablenamesNames of the variables to be removed.
Examples
backdoor2 <- backdoor$clone()
backdoor2$remove_variable(c("uy","y"))
#' @include R6causal.R R6causal_examples.R
NULL
Method causal.effect()
Is a causal effect identifiable from observational data?
Calls the implementation of ID algorithm from package causaleffect.
See the documentation of causal.effect for the details.
Usage
SCM$causal.effect(y, x, ...)
Arguments
yA vector of character strings specifying target variable(s).
xA vector of character strings specifying intervention variable(s).
...Other parameters passed to
causal.effect.
Returns
An expression for the joint distribution of the set of variables (y) given the intervention on the set of variables (x) conditional on (z) if the effect is identifiable. Otherwise an error is thrown describing the graphical structure that witnesses non-identifiability. @examples backdoor$causal.effect(y = "y", x = "x")
Method dosearch()
Is a causal effect or other query identifiable from given data sources?
Calls dosearch from the package dosearch.
See the documentation of dosearch for the details.
Usage
SCM$dosearch( data, query, transportability = NULL, selection_bias = NULL, missing_data = NULL, control = list() )
Arguments
dataCharacter string specifying the data sources.
queryCharacter string specifying the query of interest.
transportabilityOther parameters passed to
dosearch().selection_biasOther parameters passed to
dosearch().missing_dataOther parameters passed to
dosearch().controlList of control parameters passed to
dosearch().
Returns
An object of class dosearch::dosearch.
Examples
backdoor$dosearch(data = "p(x,y,z)", query = "p(y|do(x))")
Method cfid()
Is a counterfactual query identifiable from given data sources?
Calls identifiable from the package cfid.
See the documentation of cfid for the details.
Usage
SCM$cfid(gamma, ...)
Arguments
gammaAn R object that can be coerced into a
cfid::counterfactual_conjunctionobject that represents the counterfactual causal query....Other arguments passed to
cfid::identifiable.
Returns
An object of class cfid::query.
Examples
backdoor$cfid(gamma = cfid::conj(cfid::cf("Y",0), cfid::cf("X",0, c(Z=1))) )
Method intervene()
Apply an intervention to the SCM object.
Usage
SCM$intervene(target, ifunction)
Arguments
targetName(s) of the variables (in vflist, uflist or rflist) to be intervened.
ifunctionEither numeric value(s) or new structural function(s) for the target variables.
Examples
# A simple intervention
backdoor_x1 <- backdoor$clone() # making a copy
backdoor_x1$intervene("x",1) # applying the intervention
backdoor_x1$plot() # to see that arrows incoming to x are cut
# An intervention that redefines a structural equation
backdoor_yz <- backdoor$clone() # making a copy
backdoor_yz$intervene("y",
function(uy, z) {return(as.numeric(uy < 0.1 + 0.8*z ))}) # making y a function of z only
backdoor_yz$plot() # to see that arrow x -> y is cut
Method simulate()
Simulate data from the SCM object.
Returns simulated data as a data.table and/or creates or updates simdata in the SCM object.
If no_missing_data = FALSE, creates or updates also simdata_obs
Usage
SCM$simulate( n = 1, no_missing_data = FALSE, seed = NULL, fixedvars = NULL, store_simdata = TRUE, return_simdata = FALSE )
Arguments
nNumber of observations to be generated.
no_missing_dataLogical, should the generation of missing data skipped? (defaults FALSE).
seedNULL or a number for
set.seed.fixedvarsList of variable names that remain unchanged or a data table/frame that contains the values of the fixed variables.
store_simdataLogical, should the simulated data to be stored in the SCM object (defaults TRUE)
return_simdataLogical, should the simulated data to be returned as the output (defaults FALSE)
Examples
backdoor$simulate(8, return_simdata = TRUE, store_simdata = FALSE) backdoor$simulate(10) backdoor$simdata
Method clone()
The objects of this class are cloneable with this method.
Usage
SCM$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
## ------------------------------------------------
## Method `SCM$new`
## ------------------------------------------------
backdoor <- SCM$new("backdoor",
uflist = list(
uz = function(n) {return(stats::runif(n))},
ux = function(n) {return(stats::runif(n))},
uy = function(n) {return(stats::runif(n))}
),
vflist = list(
z = function(uz) {
return(as.numeric(uz < 0.4))},
x = function(ux, z) {
return(as.numeric(ux < 0.2 + 0.5*z))},
y = function(uy, z, x) {
return(as.numeric(uy < 0.1 + 0.4*z + 0.4*x))}
)
)
## ------------------------------------------------
## Method `SCM$print`
## ------------------------------------------------
backdoor
## ------------------------------------------------
## Method `SCM$plot`
## ------------------------------------------------
backdoor$plot()
backdoor$plot("v")
## ------------------------------------------------
## Method `SCM$add_variable`
## ------------------------------------------------
backdoor2 <- backdoor$clone()
backdoor2$add_variable(
vfnew = list(
w = function(uw, x) {
return(as.numeric(uw < 0.4 + 0.3*x))}),
ufnew = list(
uw = function(n) {return(stats::runif(n))})
)
## ------------------------------------------------
## Method `SCM$remove_variable`
## ------------------------------------------------
backdoor2 <- backdoor$clone()
backdoor2$remove_variable(c("uy","y"))
#' @include R6causal.R R6causal_examples.R
NULL
## ------------------------------------------------
## Method `SCM$dosearch`
## ------------------------------------------------
backdoor$dosearch(data = "p(x,y,z)", query = "p(y|do(x))")
## ------------------------------------------------
## Method `SCM$cfid`
## ------------------------------------------------
backdoor$cfid(gamma = cfid::conj(cfid::cf("Y",0), cfid::cf("X",0, c(Z=1))) )
## ------------------------------------------------
## Method `SCM$intervene`
## ------------------------------------------------
# A simple intervention
backdoor_x1 <- backdoor$clone() # making a copy
backdoor_x1$intervene("x",1) # applying the intervention
backdoor_x1$plot() # to see that arrows incoming to x are cut
# An intervention that redefines a structural equation
backdoor_yz <- backdoor$clone() # making a copy
backdoor_yz$intervene("y",
function(uy, z) {return(as.numeric(uy < 0.1 + 0.8*z ))}) # making y a function of z only
backdoor_yz$plot() # to see that arrow x -> y is cut
## ------------------------------------------------
## Method `SCM$simulate`
## ------------------------------------------------
backdoor$simulate(8, return_simdata = TRUE, store_simdata = FALSE)
backdoor$simulate(10)
backdoor$simdata