| SCM | R Documentation |
R6 Class for structural causal models
R6 Class for structural causal models
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.
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 igraph form
(without the missing data mechanism).
igraph_nodedicatedThe graph of the SCM in the igraph form
(without the dedicated U variables and the missing data mechanism).
igraph_bidirectedThe graph of the SCM in the igraph form where
latent variables are presented by bidirected arcs.
igraph_mdThe graph of the SCM in the igraph form 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.
new()Create a new SCM object.
SCM$new( name = "An SCM", uflist = NULL, vflist = NULL, rflist = NULL, rprefix = "R_", starsuffix = "_md" )
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.
A new 'SCM' object.
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))}
)
)
print()Print a summmary of the SCM object.
SCM$print()
backdoor
plot()Plot the DAG of the SCM object.
SCM$plot(subset = "uvr", method = "igraph", ...)
subsetVariable groups to be plotted: "uvr", "u2vr","vr","uv", "u2v" or "v".
methodPlotting method: "qgraph" or "igraph".
...other parameters passed to the plotting method
backdoor$plot()
backdoor$plot("v")
tikz()Return a TikZ code for drawing the DAG of the SCM object in LaTeX.
SCM$tikz( subset = "uvr", layoutfunction = igraph::layout_with_lgl, labels = NULL, settings = list(force = FALSE, borders = TRUE, shape = "circle", size = 5, scale = 2), ... )
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
pa()Return the parents of a set of vertices.
SCM$pa(vnames, includeself = TRUE)
vnamesA vector of vertex names
includeselfLogical, should vnames to be included in the results (defaults TRUE)
ch()Return the children of a set of vertices.
SCM$ch(vnames, includeself = TRUE)
vnamesA vector of vertex names
includeselfLogical, should vnames to be included in the results (defaults TRUE)
an()Return the ancestors of a set of vertices.
SCM$an(vnames, includeself = TRUE)
vnamesA vector of vertex names
includeselfLogical, should vnames to be included in the results (defaults TRUE)
de()Return the descendants of a set of vertices.
SCM$de(vnames, includeself = TRUE)
vnamesA vector of vertex names
includeselfLogical, should vnames to be included in the results (defaults TRUE)
add_variable()Add a new variable to the SCM object.
SCM$add_variable( vfnew = NULL, ufnew = NULL, rfnew = NULL, rprefixnew = NULL, starsuffixnew = NULL )
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.
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))})
)
remove_variable()Remove variables from the SCM object.
SCM$remove_variable(variablenames)
variablenamesNames of the variables to be removed.
backdoor2 <- backdoor$clone()
backdoor2$remove_variable(c("uy","y"))
#' @include R6causal.R R6causal_examples.R
NULL
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.
SCM$causal.effect(y, x, ...)
yA vector of character strings specifying target variable(s).
xA vector of character strings specifying intervention variable(s).
...Other parameters passed to causal.effect.
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")
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.
SCM$dosearch( data, query, transportability = NULL, selection_bias = NULL, missing_data = NULL, control = list() )
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().
An object of class dosearch::dosearch.
backdoor$dosearch(data = "p(x,y,z)", query = "p(y|do(x))")
cfid()Is a counterfactual query identifiable from given data sources?
Calls identifiable from the package cfid.
See the documentation of cfid for the details.
SCM$cfid(gamma, ...)
gammaAn R object that can be coerced into a cfid::counterfactual_conjunction object that represents the counterfactual causal query.
...Other arguments passed to cfid::identifiable.
An object of class cfid::query.
backdoor$cfid(gamma = cfid::conj(cfid::cf("Y",0), cfid::cf("X",0, c(Z=1))) )
intervene()Apply an intervention to the SCM object.
SCM$intervene(target, ifunction)
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.
# 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
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
SCM$simulate( n = 1, no_missing_data = FALSE, seed = NULL, fixedvars = NULL, store_simdata = TRUE, return_simdata = FALSE )
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)
backdoor$simulate(8, return_simdata = TRUE, store_simdata = FALSE) backdoor$simulate(10) backdoor$simdata
clone()The objects of this class are cloneable with this method.
SCM$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------
## 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.