CausalTables.jl Full API
CausalTables.AllOrderStatistics
— TypeAllOrderStatistics <: NetworkSummary
A NetworkSummary which computes all ordered values of the target variable among each unit's connected neighbors in the adjacency matrix
.
Fields
target::Symbol
: A key denoting the target variable to be summarized in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the target variable to be summarized in thedata
attribute of aCausalTable
.matrix::Symbol
: A key denoting the adjacency matrix over which summary is computed in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the key of the adjacency matrix in thearrays
attribute of aCausalTable
.weights::Union{Symbol, Nothing}
: An optional variable by which each unit may be weighted in the summary.
CausalTables.DataGeneratingProcess
— Typemutable struct DataGeneratingProcess
A struct representing a data generating process.
Fields
names
: An array of symbols representing the names of the variables.types
: An array of symbols representing the types of the variables.funcs
: An array of functions representing the generating functions for each variable.
CausalTables.Friends
— Typemutable struct Friends <: NetworkSummary
A NetworkSummary counting the number of connected individuals in an adjacency matrix, also known as the number of "friends".
Fields
matrix::Symbol
: A key denoting the adjacency matrix over which summary is computed in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the key of the adjacency matrix in thearrays
attribute of aCausalTable
.
CausalTables.KOrderStatistics
— TypeKOrderStatistics <: NetworkSummary
A NetworkSummary which computes the top K ordered values of the target variable among each unit's connected neighbors in the adjacency matrix
.
Fields
target::Symbol
: A key denoting the target variable to be summarized in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the target variable to be summarized in thedata
attribute of aCausalTable
.matrix::Symbol
: A key denoting the adjacency matrix over which summary is computed in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the key of the adjacency matrix in thearrays
attribute of aCausalTable
.weights::Union{Symbol, Nothing}
: An optional variable by which each unit may be weighted in the summary.
CausalTables.Mean
— TypeMean <: NetworkSummary
A NetworkSummary which computes the mean of the target variable among each unit connected in the adjacency matrix
.
Fields
target::Symbol
: A key denoting the target variable to be summarized in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the target variable to be summarized in thedata
attribute of aCausalTable
.matrix::Symbol
: A key denoting the adjacency matrix over which summary is computed in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the key of the adjacency matrix in thearrays
attribute of aCausalTable
.weights::Union{Symbol, Nothing}
: An optional variable by which each unit may be weighted in the summary.
CausalTables.NetworkSummary
— Typeabstract type NetworkSummary
Abstract type representing a summary of a network.
CausalTables.StructuralCausalModel
— Typestruct StructuralCausalModel
A struct representing a structural causal model (SCM). This includes a DataGeneratingProcess
Arguments
dgp::DataGeneratingProcess
: The data generating process from which random data will be drawn.treatment::Vector{Symbol}
: The variables representing the treatment.response::Vector{Symbol}
: The variables representing the response.causes::Union{NamedTuple, Nothing}
: A NamedTuple of Vectors labeling the causes of relevant variables in the data-generating process. Ifnothing
, will assume that all variables not contained intreatment
orresponse
are common causes of both.arraynames
: Names of auxiliary variables used in the DataGeneratingProcess that are not included as "tabular" variables. Most commonly used to denote names of adjacency matrices used to compute summary functions of previous steps.
CausalTables.Sum
— TypeSum <: NetworkSummary
A NetworkSummary which sums the values of the target variable for each unit connected in the adjacency matrix
of a StructuralCausalModel
or CausalTable
Fields
target::Symbol
: A key denoting the target variable to be summarized in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the target variable to be summarized in thedata
attribute of aCausalTable
.matrix::Symbol
: A key denoting the adjacency matrix over which summary is computed in theDataGeneratingProcess
of theStructuralCausalModel
; or, alternatively, the key of the adjacency matrix in thearrays
attribute of aCausalTable
.weights::Union{Symbol, Nothing}
: An optional variable by which each unit may be weighted in the summary.
Base.rand
— Methodrand(scm::StructuralCausalModel, n::Int)
Generate random data from a Structural Causal Model (SCM) using the specified number of samples.
Arguments
scm::StructuralCausalModel
: The Structural Causal Model from which to generate data.n::Int
: The number of samples to generate.
Returns
A CausalTable
object containing the generated data.
Base.replace
— Methodreplace(o::CausalTable; kwargs...)
Replace the fields of a CausalTable
object with the provided keyword arguments.
Arguments
o::CausalTable
: TheCausalTable
object to be replaced.kwargs...
: Keyword arguments specifying the new values for the fields.
Returns
A new CausalTable
object with the specified fields replaced.
CausalTables.additive_mtp
— Methodadditive_mtp(δ)
Constructs a function that adds a constant (or constant vector) δ to the treatment variable(s) in a CausalTable
object. This function is intended to be used as an argument to ape
.
Arguments
- δ: The "additive shift" to be applied to the treatment variable of a
CausalTable
.
Returns
- A function that takes a
CausalTable
object as input and returns a column table of treatments that have been shifted by δ units.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Normal(L)),
Y ~ @.(Normal(A + 2 * L + 1))
)
scm = StructuralCausalModel(dgp, :A, :Y)
ape(scm, additive_mtp(0.5))
CausalTables.adjacency_matrix
— Methodadjacency_matrix(o::CausalTable)
Generate the adjacency matrix induced by the summaries
and arrays
attributes of a CausalTable
object. This matrix denotes which units are causally dependent upon one another: an entry of 1 in cell (i,j) indicates that some variable in unit i exhibits a causal relationship to some variable in unit j.
Arguments
o::CausalTable
: TheCausalTable
object for which the adjacency matrix is to be generated.
Returns
A boolean matrix representing the adjacency relationships in the CausalTable
.
CausalTables.ape
— Methodape(scm::StructuralCausalModel, intervention::Function; samples = 10^6)
Approximate the average policy effect for a given structural causal model (SCM), along with its efficiency bound. This is also known as the causal effect of a modified treatment policy, and is approximated using Monte Carlo sampling. Note that unless intervention
is piecewise smooth invertible, the estimated statistical quantity may not have a causal interpretation; see Haneuse and Rotnizky (2013). Mathematically, this is
\[E(Y(d(a) - Y(a))\]
where $d(a)$ represents the intervention on the treatment variable(s) $A$, $Y(d(a))$ represents the counterfactual $Y$ under treatment $d(a)$, and $Y(a)$ represents the counterfactual outcome under the naturally observed value of treatment. This statistical quantity is approximated using Monte Carlo sampling.
Convenience functions for generating intervention
functions include additive_mtp
and multiplicative_mtp
, which construct functions that respectively add or multiply a constant (or constant vector) to the treatment variable(s). One can also implement their own intervention function; this function must take as input a CausalTable
object and return a NamedTuple object with each key indexing a treatment variable that has been modified according to the intervention. Also see cast_matrix_to_table_function
for a convenience function for constructing interventions.
Arguments
scm::StructuralCausalModel
: The SCM from which data is to be simulated.intervention::Function
: The intervention function to apply to the SCM.samples
: The number of samples to draw fromscm
for Monte Carlo approximation (default is 10^6). This controls the precision of the approximation.
Returns
A named tuple containing:
μ
: The APE approximation.
Example
using Distributions
dgp = CausalTables.@dgp(
L ~ Beta(2, 4),
A ~ @.(Normal(L)),
Y ~ @.(Normal(A + 2 * L + 1))
)
scm = CausalTables.StructuralCausalModel(dgp, :A, :Y)
ape(scm, additive_mtp(0.5))
ape(scm, multiplicative_mtp(2.0))
# example of a custom intervention function
custom_intervention = cast_matrix_to_table_function(x -> exp.(x))
ape(scm, custom_intervention)
CausalTables.ate
— Methodate(scm::StructuralCausalModel; samples = 10^6)
Approximate the average treatment effect (ATE) for a given structural causal model (SCM), along with its efficiency bound, for a univariate binary treatment. Mathematically, this is
\[E(Y(1) - Y(0))\]
where $Y(a)$ represents the counterfactual $Y$ had the treatment $A$ been set to $a$. This statistical quantity is approximated using Monte Carlo sampling.
Arguments
scm::StructuralCausalModel
: The SCM from which data is to be simulated.samples
: The number of samples to draw fromscm
for Monte Carlo approximation (default is 10^6). This controls the precision of the approximation.
Returns
A named tuple containing:
μ
: The ATE approximation.eff_bound
: The variance of the counterfactual response, which is equal to the efficiency bound for IID data. If observations are correlated, this may not have a meaningful interpretation.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
ate(scm)
CausalTables.att
— Methodatt(scm::StructuralCausalModel; samples = 10^6)
Approximate the average treatment effect among the treated (ATT) for a given structural causal model (SCM), along with its efficiency bound, for a univariate binary treatment. Mathematically, this is
\[E(Y(1) - Y(0) \mid A = 1)\]
where $Y(a)$ represents the counterfactual $Y$ had the treatment $A$ been set to $a$. This statistical quantity is approximated using Monte Carlo sampling.
Arguments
scm::StructuralCausalModel
: The SCM from which data is to be simulated.samples
: The number of samples to draw fromscm
for Monte Carlo approximation (default is 10^6). This controls the precision of the approximation.
Returns
A named tuple containing:
μ
: The ATT approximation.eff_bound
: The variance of the counterfactual response, which is equal to the efficiency bound for IID data. If observations are correlated, this may not have a meaningful interpretation.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
att(scm)
CausalTables.atu
— Methodatu(scm::StructuralCausalModel; samples = 10^6)
Approximate the average treatment effect among the untreated (ATU) for a given structural causal model (SCM), along with its efficiency bound, for a univariate binary treatment. Mathematically, this is
\[E(Y(1) - Y(0) \mid A = 0)\]
where $Y(a)$ represents the counterfactual $Y$ had the treatment $A$ been set to $a$. This statistical quantity is approximated using Monte Carlo sampling.
Arguments
scm::StructuralCausalModel
: The SCM from which data is to be simulated.samples
: The number of samples to draw fromscm
for Monte Carlo approximation (default is 10^6). This controls the precision of the approximation.
Returns
A named tuple containing:
μ
: The ATU approximation.eff_bound
: The variance of the counterfactual response, which is equal to the efficiency bound for IID data. If observations are correlated, this may not have a meaningful interpretation.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
atu(scm)
CausalTables.cast_matrix_to_table_function
— Methodcast_matrix_to_table_function(func::Function)
Wraps a given function func
that operates on a matrix and returns a new function that operates on a CausalTable
object. The returned function converts the CausalTable
's treatment matrix to a table, applies func
to this matrix, and then converts the result back to a column table with the same header as the original treatment matrix.
Arguments
func::Function
: A function that takes a matrix as input and returns a matrix.
Returns
- A function that takes a
CausalTable
object as input and returns a column table.
Example
custom_intervention = cast_matrix_to_table_function(x -> exp.(x))
CausalTables.cfdiff
— Methodcfdiff(scm::StructuralCausalModel, intervention1::Function, intervention2::Function; samples = 10^6)
Approximate the difference between two counterfactual response means – that under intervention1
having been applied to the treatment, and that under intervention2
– for a given structural causal model (SCM), along with its efficiency bound. Mathematically, this is
\[E(Y(d_1(a)) - Y(d_2(a)))\]
where $d_1$ and $d_2$ represent intervention1
and intervention2
being applied on the treatment variable(s) $A$. This statistical quantity is approximated using Monte Carlo sampling.
Arguments
scm::StructuralCausalModel
: The SCM from which data is to be simulated.intervention1::Function
: The first intervention function to be contrasted.intervention2::Function
: The second intervention function to be contrasted.samples
: The number of samples to draw fromscm
for Monte Carlo approximation (default is 10^6). This controls the precision of the approximation.
Returns
A named tuple containing:
μ
: The mean difference in counterfactual outcomes.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
cfdiff(scm, treat_all, treat_none)
CausalTables.cfmean
— Methodcfmean(scm::StructuralCausalModel, intervention::Function; samples = 10^6)
Approximate the counterfactual mean of the response had intervention
been applied to the treatment, along with its efficiency bound, for a given structural causal model (SCM). Mathematically, this estimand is
\[E(Y(d(a)))\]
where $d(a)$ represents an intervention on the treatment variable(s) $A$. This statistical quantity is approximated using Monte Carlo sampling.
Arguments
scm::StructuralCausalModel
: The SCM from which data is to be simulated.intervention::Function
: The intervention function to apply to the SCM.samples
: The number of samples to draw fromscm
for Monte Carlo approximation (default is 10^6). This controls the precision of the approximation.
Returns
A named tuple containing:
μ
: The mean of the counterfactual outcomes.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
cfmean(scm, treat_all)
cfmean(scm, treat_none)
CausalTables.condensity
— Methodcondensity(scm::StructuralCausalModel, ct::CausalTable, name::Symbol)
Compute the conditional density of variable name
in CausalTable ct
that has been drawn from StructuralCausalModel scm
.
Arguments
scm::StructuralCausalModel
: The StructuralCausalModel representing the data generating process.ct::CausalTable
: The CausalTable containing the observed data.name::Symbol
: The variable for which to compute the conditional density.
Returns
The conditional density of the variable var
given the observed data.
CausalTables.confoundernames
— Methodconfoundernames(o::CausalTable, x::Symbol, y::Symbol)
Outputs the names of the confounders of the causal relationship between x
and y
from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the confounder names.x::Symbol
,y::Symbol
: The two variables whose confounders should be selected.
Returns
A Vector of Symbols containing the names of the confounders between x and y.
CausalTables.confoundernames
— Methodconfoundernames(o::CausalTable)
Outputs the confounder names of each response-treatment pair from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the confounder names of each treatment-response pair.
Returns
A matrix of Vectors containing the confounder names of each treatment-response pair.
CausalTables.confounders
— Methodconfounders(o::CausalTable, x::Symbol, y::Symbol)
Selects the common causes for a specific pair of variables (x,y) from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the confounders.x::Symbol
,y::Symbol
: The two variables whose confounders should be selected.
Returns
A new CausalTable
containing only the confounders of both x and y.
CausalTables.confounders
— Methodconfounders(o::CausalTable; collapse_parents = true)
Selects the confounders of each response-treatment pair from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the confounder variables of each treatment-response pair.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleCausalTable
object if there is either only one treatment-response pair or all pair share the same set of confounders. Defaults totrue
.
Returns
A new CausalTable
containing only the confounders (if a single response, or all responses share the same set of causes); otherwise, a Matrix of CausalTable objects containing the confounders of each treatment-response pair, where rows represent responses and columns represent treatments.
CausalTables.confoundersmatrix
— Methodconfoundersmatrix(o::CausalTable; collapse_parents = true)
Outputs the treatment-variable confounders from the given CausalTable
object as a matrix (or matrix of matrices, if multiple treatment-response pairs are present).
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the confounders of each treatment-response pair.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleMatrix
object if there is either only one treatment-response pair or all pair share the same set of confounders. Defaults totrue
.
Returns
A matrix containing only the confounders.
CausalTables.conmean
— Methodconmean(scm::StructuralCausalModel, ct::CausalTable, name::Symbol)
Compute the conditional mean of variable name
in CausalTable ct
that has been drawn from StructuralCausalModel scm
.
Arguments
scm::StructuralCausalModel
: The StructuralCausalModel object representing the data generating process.ct::CausalTable
: The CausalTable object representing the data.name::Symbol
: The variable for which to compute the conditional mean.
Returns
An array of conditional means for the specified variable.
CausalTables.convar
— Methodconvar(scm::StructuralCausalModel, ct::CausalTable, name::Symbol)
Compute the conditional variance of variable name
in CausalTable ct
that has been drawn from StructuralCausalModel scm
.
Arguments
scm::StructuralCausalModel
: The StructuralCausalModel object representing the data generating process.ct::CausalTable
: The CausalTable object representing the data.name::Symbol
: The variable for which to compute the conditional mean.
Returns
An array of conditional variances for the specified variable.
CausalTables.convolve
— Methodconvolve(ds::Vector{T}) where {T <: UnivariateDistribution}
A convolve
function that works on a vector of UnivariateDistribution
.
Arguments
ds::Vector{T}
: A vector ofUnivariateDistribution
objects.
Returns
output
: The result of convolving all the distributions inds
. Ifds
is empty, will returnBinomial(0, 0.5)
denoting a point mass at 0.
CausalTables.data
— Methoddata(o::CausalTable)
Retrieve the data stored in a CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
from which to retrieve the data.
Returns
The data stored in the CausalTable
object.
CausalTables.dependency_matrix
— Methoddependency_matrix(o::CausalTable)
Generate the dependency matrix induced by the summaries
and arrays
attributes of a CausalTable
object. This matrix stores which units are statistically dependent upon one another: an entry of 1 in cell (i,j) indicates that the data of unit i is correlated with the data in unit j. Two units are correlated if they either are causally dependent (neighbors in the adjacency matrix) or share a common cause (share a neighbor in the adjacency matrix).
Arguments
o::CausalTable
: TheCausalTable
object for which the dependency matrix is to be generated.
Returns
A boolean matrix representing the relationships in the CausalTable
.
CausalTables.draw_counterfactual
— Methoddraw_counterfactual(scm::StructuralCausalModel, parents::CausalTable, intervention::Function) -> Vector
Generate counterfactual responses based on a given structural causal model (SCM), a table of response parents, and an intervention function. That is, sample the responses that would have occurred had some intervention been applied to the treatment specified by the structural causal model.
Arguments
scm::StructuralCausalModel
: The structural causal model used to generate counterfactual outcomes.parents::CausalTable
: A table containing the variables causally preceding the response variable.intervention::Function
: A function that defines the intervention to be applied to the parent variables. Usecast_matrix_to_table_function
to convert a function acting on a treatment vector or matrix to a function that acts on aCausalTable
.
Returns
A vector of counterfactual responses.
CausalTables.instrumentnames
— Methodinstrumentnames(o::CausalTable, x::Symbol, y::Symbol)
Outputs the names of the instruments of the causal relationship between x
and y
from the given CausalTable
object; that is, variables that are associated with x
but do not cause y
.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the mediator names.x::Symbol
,y::Symbol
: The two variables whose mediators should be selected.
Returns
A Vector of Symbols containing the names of the mediators between x and y.
CausalTables.instrumentnames
— Methodinstrumentnames(o::CausalTable)
Outputs the instrument names of each treatment-response pair from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the instrument names of each treatment-response pair.
Returns
A matrix of Vectors containing the instrument names of each treatment-response pair.
CausalTables.instruments
— Methodinstruments(o::CausalTable, x::Symbol, y::Symbol)
Selects the instruments for a specific pair of variables (x,y) from the given CausalTable
object; that is, variables that are associated with x
but do not cause y
.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the instruments.x::Symbol
,y::Symbol
: The two variables whose instruments should be selected.
Returns
A new CausalTable
containing only the instruments of both x and y.
CausalTables.instruments
— Methodinstruments(o::CausalTable; collapse_parents = true)
Selects the instruments of each treatment-response pair from the given CausalTable
object; that is, variables that are associated with the treatment but do not cause the response.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the instrumental variables of each treatment-response pair.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleCausalTable
object if there is either only one treatment-response pair or all pair share the same set of instruments. Defaults totrue
.
Returns
A new CausalTable
containing only the instruments (if a single response, or all responses share the same set of instruments); otherwise, a Matrix of CausalTable objects containing the instruments of each treatment-response pair, where rows represent responses and columns represent treatments.
CausalTables.instrumentsmatrix
— Methodinstrumentsmatrix(o::CausalTable; collapse_parents = true)
Outputs the treatment-variable instruments from the given CausalTable
object as a matrix (or matrix of matrices, if multiple treatment-response pairs are present).
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the instruments of each treatment-response pair.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleMatrix
object if there is either only one treatment-response pair or all pair share the same set of instruments. Defaults totrue
.
Returns
A matrix containing only the confounders.
CausalTables.intervene
— Methodintervene(ct::CausalTable, intervention::Function)
Applies intervention
to the treatment vector(s) within a CausalTable, and outputs a new CausalTable with the intervened treatment.
Arguments
ct::CausalTable
: The data on which treatment should be intervenedintervention::Function
: A function that defines the intervention to be applied to the parent variables. Usecast_matrix_to_table_function
to convert a function acting on a treatment vector or matrix to a function that acts on aCausalTable
.
Returns
A CausalTable
containing the same data as ct
, but with the treatment variable(s) modified accoding to intervention
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
ct = rand(scm, 100)
intervene(ct, treat_all)
CausalTables.mediatornames
— Methodmediatornames(o::CausalTable, x::Symbol, y::Symbol)
Outputs the names of the mediators of the causal relationship between x
and y
from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the mediator names.x::Symbol
,y::Symbol
: The two variables whose mediators should be selected.
Returns
A Vector of Symbols containing the names of the mediators between x and y.
CausalTables.mediatornames
— Methodmediatornames(o::CausalTable)
Outputs the mediator names of each response-treatment pair from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the mediator names of each treatment-response pair.
Returns
A matrix of Vectors containing the mediator names of each treatment-response pair.
CausalTables.mediators
— Methodmediators(o::CausalTable, x::Symbol, y::Symbol)
Selects the mediators for a specific pair of variables (x,y) from the given CausalTable
object; that is, the variables that are caused by x and cause y.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the mediators.x::Symbol
,y::Symbol
: The two variables whose mediators should be selected.
Returns
A new CausalTable
containing only the mediators of both x and y.
CausalTables.mediators
— Methodmediators(o::CausalTable; collapse_parents = true)
Selects the mediators of each treatment-response pair from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the mediator variables of each treatment-response pair.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleCausalTable
object if there is either only one treatment-response pair or all pair share the same set of mediators. Defaults totrue
.
Returns
A new CausalTable
containing only the mediators (if a single response, or all responses share the same set of mediators); otherwise, a Matrix of CausalTable objects containing the mediators of each treatment-response pair, where rows represent responses and columns represent treatments.
CausalTables.mediatorsmatrix
— Methodmediatorsmatrix(o::CausalTable; collapse_parents = true)
Outputs the treatment-variable confounders from the given CausalTable
object as a matrix (or matrix of matrices, if multiple treatment-response pairs are present).
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the mediators of each treatment-response pair.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleMatrix
object if there is either only one treatment-response pair or all pair share the same set of mediators. Defaults totrue
.
Returns
A matrix containing only the confounders.
CausalTables.multiplicative_mtp
— Methodmultiplicative_mtp(δ)
Constructs a function that scales the treatment variable(s) in a CausalTable
object by a constant δ. This function is intended to be used as an argument to ape
.
Arguments
- δ: The "multiplicative shift" to be applied to the treatment variable of a
CausalTable
.
Returns
- A function that takes a
CausalTable
object as input and returns a column table of treatments that have been scaled by δ units.
Example
using Distributions
dgp = CausalTables.@dgp(
L ~ Beta(2, 4),
A ~ @.(Normal(L)),
Y ~ @.(Normal(A + 2 * L + 1))
)
scm = CausalTables.StructuralCausalModel(dgp, :A, :Y)
ape(scm, multiplicative_mtp(2.0))
CausalTables.parents
— Methodparents(o::CausalTable, symbol)
Selects the variables that precede symbol
causally from the CausalTable o
, based on the causes
attribute. Note that if symbol
is not contained within o.causes
, this function will output an empty CausalTable
.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the parent variables ofsymbol
.symbol
: The variable for which to extract the parent variables.
Returns
A new CausalTable
containing only the parents of symbol
CausalTables.propensity
— Methodpropensity(scm::StructuralCausalModel, ct::CausalTable, name::Symbol)
Compute the (generalized) propensity score of variable name
in CausalTable ct
that has been drawn from StructuralCausalModel scm
.
Arguments
scm::StructuralCausalModel
: The StructuralCausalModel object representing the data generating process.ct::CausalTable
: The CausalTable object representing the data.name::Symbol
: The variable for which to compute the conditional mean.
Returns
An array of conditional probabilities for the specified variable (or densities, if the specified variable is continuous).
CausalTables.reject
— Methodreject(o::CausalTable, symbols)
Removes the columns specified by symbols
from the CausalTable
object o
.
Arguments
o::CausalTable
: TheCausalTable
object from which symbols will be rejected.symbols
: A collection of symbols to be rejected from theCausalTable
.
Returns
A new CausalTable
object with the specified symbols removed from its data.
CausalTables.response
— Methodresponse(o::CausalTable)
Selects the response column(s) from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to select the response column(s).
Returns
A new CausalTable
containing only the response column(s).
CausalTables.responsematrix
— Methodresponsematrix(o::CausalTable)
Outputs the response column(s) from the given CausalTable
object as a matrix.
Arguments
o::CausalTable
: TheCausalTable
object from which to select the response column(s).
Returns
A matrix containing only the response column(s)
CausalTables.responseparents
— Methodresponseparents(o::CausalTable)
Selects the parents of each response variable from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the parent variables of each response.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleCausalTable
object if there is either only one response or all response have the same parents. Defaults totrue
.
Returns
A new CausalTable
containing only the causes of the responses (if a single response, or all responses share the same set of causes); otherwise, a Vector of CausalTable objects containing the causes of each response.
CausalTables.select
— Methodselect(o::CausalTable, symbols)
Selects specified columns from a CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which columns are to be selected.symbols
: A list of symbols representing the columns to be selected.
Returns
- A new
CausalTable
object with only the selected columns.
CausalTables.set_treatment_value
— Methodset_treatment_value(ct::CausalTable, value::Float64)
Sets all treatments present in the data of a CausalTable
to a specified value. This function is primarily used for interventions where the treatment value is set to a constant, such as in the case of binary treatments.
Arguments
ct::CausalTable
: The causal table object containing treatment information and data.value::Float64
: (Currently unused) A float value intended to represent the treatment value to set.
Returns
NamedTuple
: A named tuple mapping each treatment variable to a vector of ones.
CausalTables.summarize
— Methodsummarize(o::CausalTable)
Summarizes the data in a CausalTable
object according to the NetworkSummary objects stored in its summaries
attribute.
Arguments
o::CausalTable
: TheCausalTable
object to be summarized.
Returns
- A new
CausalTable
object with the original data merged with the summarized data.
CausalTables.treat_all
— Methodtreat_all(ct::CausalTable)
Intervenes on a CausalTable
object by setting all treatment variables to 1.
Arguments
ct::CausalTable
: ACausalTable
object with a univariate binary treatment.
Returns
A NamedTuple
object with the same header as the treatment matrix in ct
, where each treatment variable is set to 1.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
data = rand(scm, 100)
treat_all(data)
CausalTables.treat_none
— Methodtreat_none(ct::CausalTable)
Intervenes on a CausalTable
object by setting all treatment variables to 0.
Arguments
ct::CausalTable
: ACausalTable
object with a univariate binary treatment.
Returns
A NamedTuple
object with the same header as the treatment matrix in ct
, where each treatment variable is set to 0.
Example
using Distributions
dgp = @dgp(
L ~ Beta(2, 4),
A ~ @.(Bernoulli(L)),
Y ~ @.(Normal(A + L))
)
scm = StructuralCausalModel(dgp, :A, :Y)
data = rand(scm, 100)
treat_none(data)
CausalTables.treatment
— Methodtreatment(o::CausalTable)
Selects the treatment column(s) from the given CausalTable
object. treatment
Arguments
o::CausalTable
: TheCausalTable
object from which to select the treatment column(s).
Returns
A new CausalTable
containing only the treatment column(s)
CausalTables.treatmentmatrix
— Methodtreatmentmatrix(o::CausalTable)
Outputs the treatment column(s) from the given CausalTable
object as a matrix.
Arguments
o::CausalTable
: TheCausalTable
object from which to select the treatment column(s).
Returns
A matrix containing only the treatment column(s)
CausalTables.treatmentparents
— Methodtreatmentparents(o::CausalTable)
Selects the parents of each treatment variable from the given CausalTable
object.
Arguments
o::CausalTable
: TheCausalTable
object from which to extract the parent variables of each treatment.collape_parents::Bool
: Optional parameter, whether to collapse the output to a singleCausalTable
object if there is either only one treatment or all treatments have the same parents. Defaults totrue
.
Returns
A new CausalTable
containing only the causes of the treatment (if a single treatment, or all treatments share the same set of causes); otherwise, a Vector of CausalTable objects containing the causes of each treatment.