Learn R Programming

CausalQueries (version 1.4.3)

make_model: Make a model

Description

make_model uses causal statements encoded as strings to specify the nodes and edges of a graph. Implied causal types are calculated and default priors are provided under the assumption of no confounding. Models can be updated with specification of a parameter matrix, P, by providing restrictions on causal types, and/or by providing informative priors on parameters.

Usage

make_model(statement = "X -> Y", add_causal_types = TRUE, nodal_types = NULL)

Value

An object of class causal_model.

An object of class "causal_model" is a list containing at least the following components:

statement

A character vector of the statement that defines the model

dag

A data.frame with columns `parent`and `children` indicating how nodes relate to each other.

nodes

A named list with the nodes in the model

parents_df

A data.frame listing nodes, whether they are root nodes or not, and the number of parents they have

nodal_types

Optional: A named list with the nodal types in the model. List should be ordered according to the causal ordering of nodes. If NULL nodal types are generated. If FALSE, a parameters data frame is not generated.

parameters_df

A data.frame with descriptive information of the parameters in the model

causal_types

A data.frame listing causal types and the nodal types that produce them

By default a causal model has flat (uniform) priors and parameters that put equal weight on each parameter within each parameter set. The parameter ranges (range of the nodal types) can be adjusted with set_restrictions. The priors can be adjusted with set_priors. Specific parameter values can be adjusted with set_parameters.

Arguments

statement

character string. Statement describing causal relations between nodes. Directed relations can be specified using '->' or '<-' and can be combined. For instance "X -> Y", "Y <- X" or "X1 -> Y <- X2; X1 -> X2". Confounded relations can be specified using a double headed arrow, "X <-> Y", to indicate unobserved confounding between X and Y.

add_causal_types

Logical. Whether to create and attach causal types to model. Defaults to `TRUE`.

nodal_types

List of nodal types associated with model nodes

See Also

summary.causal_model provides summary method for output objects of class causal_model

Examples

Run this code
make_model(statement = "X -> Y")
modelXKY <- make_model("X -> K -> Y; X -> Y")

# Example where a cyclical dag is attempted
if (FALSE) {
 modelXKX <- make_model("X -> K -> X")
}

# Examples with confounding
model <- make_model("X->Y; X <-> Y")
inspect(model, "parameter_matrix")
model <- make_model("Y2 <- X -> Y1; X <-> Y1; X <-> Y2")
dim(inspect(model, "parameter_matrix"))
inspect(model, "parameter_matrix")
model <- make_model("X1 -> Y <- X2; X1 <-> Y; X2 <-> Y")
dim(inspect(model, "parameter_matrix"))
inspect(model, "parameters_df")

# A single node graph is also possible
model <- make_model("X")

# Unconnected nodes not allowed
if (FALSE) {
 model <- make_model("X <-> Y")
}

nodal_types <-
  list(
    A = c("0","1"),
    B = c("0","1"),
    C = c("0","1"),
    D = c("0","1"),
    E = c("0","1"),
    Y = c(
      "00000000000000000000000000000000",
      "01010101010101010101010101010101",
      "00110011001100110011001100110011",
      "00001111000011110000111100001111",
      "00000000111111110000000011111111",
      "00000000000000001111111111111111",
      "11111111111111111111111111111111" ))

make_model("A -> Y; B ->Y; C->Y; D->Y; E->Y",
          nodal_types = nodal_types) |>
 inspect("parameters_df")

nodal_types = list(Y = c("01", "10"), Z = c("0", "1"))
make_model("Z -> Y", nodal_types = nodal_types) |>
 inspect("parameters_df")

Run the code above in your browser using DataLab