inst/doc/introduction.R

## ----eval=FALSE, citation-----------------------------------------------------
# @article{guo2024average,
#   title={Average Causal Effect Estimation in DAGs with Hidden Variables: Extensions of Back-Door and Front-Door Criteria},
#   author={Guo, Anna and Nabi, Razieh},
#   journal={arXiv preprint arXiv:2409.03962},
#   year={2024}
# }

## ----eval=FALSE, citation_frontdoor-------------------------------------------
# @article{guo2023targeted,
#   title={Targeted Machine Learning for Average Causal Effect Estimation Using the Front-Door Functional},
#   author={Guo, Anna and Benkeser, David and Nabi, Razieh},
#   journal={arXiv preprint arXiv:2312.10234},
#   year={2023}
# }

## ----load-pkg, warning=F, message=F-------------------------------------------

library(flexCausal) # load the package
data(data_example_a) # load the simulated dataset for example (a)
head(data_example_a) # take a glance of the data, which is a simulated dataset under above figure (a).


## ----eval=T, warning=F, message=F---------------------------------------------

# create a graph object for the ADMG in example (a)
graph_a <- make.graph(vertices=c('A','M','L','Y','X'), # specify the vertices
                bi_edges=list(c('A','Y')), # specify the bi-directed edges
                di_edges=list(c('X','A'), c('X','M'), c('X','L'),c('X','Y'), c('M','Y'), c('A','M'), c('A','L'), c('M','L'), c('L','Y')), # specify the directed edges, with each pair of variables indicating an directed edge from the first variable to the second. For example, c('X', 'A') represents a directed edge from X to A.
                multivariate.variables = list(M=c('M.1','M.2'))) # specify the components of the multivariate variable M


## ----eval=T, warning=F, message=F---------------------------------------------

f.adj_matrix(graph_a) # get the adjacency matrix of the ADMG in example (a)


## ----quick-start, warning=F, message=T----------------------------------------

est <- estADMG(a=c(1,0),
                data=data_example_a, 
                graph=graph_a,
                treatment='A', outcome='Y')

## ----quick-start-option2, warning=F, message=T--------------------------------

est <- estADMG(a=c(1,0),data=data_example_a, 
                vertices=c('A','M','L','Y','X'), # specify the vertices
                bi_edges=list(c('A','Y')), # specify the bi-directed edges
                di_edges=list(c('X','A'), c('X','M'), c('X','L'),c('X','Y'), c('M','Y'), c('A','M'), c('A','L'), c('M','L'), c('L','Y')), # specify the directed edges
                multivariate.variables = list(M=c('M.1','M.2')), # specify the components of the multivariate variable M
                treatment='A', outcome='Y')

## ----onestep-eg, warning=F, message=F, eval=F---------------------------------
# 
# est <- estADMG(a=c(1,0),
#                 data=data_example_a,
#                 graph = graph_a,
#                treatment='A', outcome='Y',
#                 lib.seq = c("SL.glm", "SL.earth", "SL.ranger", "SL.mean"),
#                 lib.Y = c("SL.glm", "SL.earth", "SL.ranger", "SL.mean"),
#                 lib.A = c("SL.glm", "SL.earth", "SL.ranger", "SL.mean"),
#                 crossfit = TRUE,
#                 K=5)

## ----tmle-eg, warning=F, message=F, eval=F------------------------------------
# 
# est <- estADMG(a=c(1,0),
#                 data=data_example_a,
#                 graph = graph_a,
#                 ratio.method.M = "dnorm")

## ----output, eval=F-----------------------------------------------------------
# 
# est <- estADMG(a=1,
#                 data=data_example_a,
#                 graph = graph_a,
#                 treatment='A', outcome='Y')
# 
# # TMLE and Onestep estimator
# est$TMLE # a list contains the estimation result from TMLE estimator
# est$Onestep # a list contains the estimation result from Onestep estimator
# 
# # For either the TMLE or Onestep estimator, the output is a list that contains the following elements:
# est$TMLE$EYa # the estimated average counterfactual outcome
# est$TMLE$lower.ci # the lower bound of the 95% confidence interval
# est$TMLE$upper.ci # the upper bound of the 95% confidence interval
# est$TMLE$EIF # the estimated efficient influence function
# est$TMLE.Ya$EIF.Y # the estimated efficient influence function at the tangent space associated with the outcome
# est$TMLE.Ya$EIF.A # the estimated efficient influence function at the tangent space associated with the treatment
# est$TMLE.Ya$EIF.v # the estimated efficient influence function at the tangent space associated with the rest of the variables
# est$TMLE.Ya$p.a1.mpA # the estimated propensity score for treatment
# est$TMLE.Ya$mu.next.A # the estimated sequential regression associated with variable that comes right after the treatment according to the topological ordering of the ADMG
# est$TMLE.Ya$EDstar # mean of the estimated efficient influence function
# est$TMLE.Ya$iter # iterations take for TMLE estimator to converge
# est$TMLE.Ya$EDstar.record # the mean of the estimated efficient influence function at each iteration
# 

## ----make-graph, warning=F, message=F, eval=F---------------------------------
# 
# graph_a <- make.graph(vertices=c('A','M','L','Y','X'), # specify the vertices
#                 bi_edges=list(c('A','Y')), # specify the bi-directed edges
#                 di_edges=list(c('X','A'), c('X','M'), c('X','L'),c('X','Y'), c('M','Y'), c('A','M'), c('A','L'), c('M','L'), c('L','Y')), # specify the directed edges, with each pair of variables indicating an directed edge from the first variable to the second. For example, c('X', 'A') represents a directed edge from X to A.
#                 multivariate.variables = list(M=c('M.1','M.2'))) # specify the components of the multivariate variable M
# 

## ----f-adj-matrix, warning=F, message=F, eval=F-------------------------------
# f.adj_matrix(graph_a)

## ----f-top-order, warning=F, message=F, eval=F--------------------------------
# f.top_order(graph_a)

## ----f-parents, warning=F, message=F, eval=F----------------------------------
# f.parents(graph_a, 'Y')

## ----f-children, warning=F, message=F, eval=F---------------------------------
# f.children(graph_a, 'A')
# 

## ----f-descendants, warning=F, message=F, eval=F------------------------------
# f.descendants(graph_a, 'A')
# 

## ----f-district, warning=F, message=F, eval=F---------------------------------
# f.district(graph_a, 'A')
# 

## ----f-markov-blanket, warning=F, message=F, eval=F---------------------------
# f.markov_blanket(graph_a, 'A')
# 

## ----f-markov-pillow, warning=F, message=F, eval=F----------------------------
# f.markov_pillow(graph_a, 'A')
# 

## ----is-fix, warning=F, message=F, eval=F-------------------------------------
# is.fix(graph_a, 'A')

## ----is-p-fix, warning=F, message=F, eval=F-----------------------------------
# is.p.fix(graph_a, 'A')

## ----is-np-saturated, warning=F, message=F, eval=F----------------------------
# is.np.saturated(graph_a)
# 

## ----is-mb-shielded, warning=F, message=F, eval=F-----------------------------
# is.mb.shielded(graph_a)
# 

Try the flexCausal package in your browser

Any scripts or data that you put into this service are public.

flexCausal documentation built on March 29, 2026, 5:08 p.m.