dag_node {causact}R Documentation

Add a node to an existing causact_graph object

Description

[Stable]

Add a node to an existing causact_graph object. The graph object should be of class causact_graph and created using dag_create().

Usage

dag_node(
  graph,
  descr = as.character(NA),
  label = as.character(NA),
  rhs = NA,
  child = as.character(NA),
  data = NULL,
  obs = FALSE,
  keepAsDF = FALSE,
  extract = as.logical(NA),
  dec = FALSE,
  det = FALSE
)

Arguments

graph

a graph object of class causact_graph. An initial object gets created using dag_create().

descr

a longer more descriptive character label for the node.

label

a shorter character label for referencing the node (e.g. "X","beta"). Labels with . in the name will be replaced by ⁠_⁠ to ensure inter-operability with Python. Additionally, Python reserved words, like lambda should not be used.

rhs

either a distribution such as ⁠uniform, normal, lognormal, bernoulli,⁠ etc. or an R expression. Valid values include normal(mu,sigma), normal, and normal(6,2). R computation/expression examples include alpha+beta*x,c(int, coefs), or 1 / exp(-(alpha + beta * x)). Concatenation using c() is NOT supported. If a distribution is given, this is a random/stochastic node, if a formula is given it is a deterministic node once given the values of its parents. Quotes should not be used as all function/computations should consist of R objects, functions, and constants. Common R arithmetic and geometric operators are supported, but less common R expressions may yield errors when running dag_numpyro().

child

an optional character vector of existing node labels. Directed edges from the newly created node to the supplied nodes will be created.

data

a vector or data frame (with observations in rows and variables in columns).

obs

a logical value indicating whether the node is observed. Assumed to be TRUE when data argument is given.

keepAsDF

a logical value indicating whether the data argument should be split into one random variable node per column or kept together as a random matrix for matrix computation. Defaults to creating one node per column of the data frame.

extract

a logical value. When TRUE, child nodes will try to extract an indexed value from this node. When FALSE, the entire random object (e.g. scalar, vector, matrix) is passed to children nodes. Only use this argument when overriding default behavior seen using dag_render().

dec

a logical value indicating whether the node is a decision node. Used to show nodes as rectangles instead of ovals when using dag_render().

det

a logical value indicating whether the node is a deterministic function of its parents Used to draw a double-line (i.e. peripheries = 2) around a shape when using dag_render(). Assumed to be TRUE when rhs is a formula.

Value

a graph object of class causact_graph with an additional node(s).

Examples

# Create an empty graph and add 2 nodes by using
# the `dag_node()` function twice
graph2 = dag_create() %>%
  dag_node("Get Card","y",
         rhs = bernoulli(theta),
         data = carModelDF$getCard) %>%
  dag_node(descr = "Card Probability by Car",label = "theta",
           rhs = beta(2,2),
           child = "y")
graph2 %>% dag_render()


# The Eight Schools Example from Gelman et al.:

schools_dat <- data.frame(y = c(28,  8, -3,  7, -1,  1, 18, 12),
sigma = c(15, 10, 16, 11,  9, 11, 10, 18), schoolName = paste0("School",1:8))

graph = dag_create() %>%
  dag_node("Treatment Effect","y",
           rhs = normal(theta, sigma),
           data = schools_dat$y) %>%
  dag_node("Std Error of Effect Estimates","sigma",
           data = schools_dat$sigma,
           child = "y") %>%
  dag_node("Exp. Treatment Effect","theta",
           child = "y",
           rhs = avgEffect + schoolEffect) %>%
  dag_node("Pop Treatment Effect","avgEffect",
           child = "theta",
           rhs = normal(0,30)) %>%
  dag_node("School Level Effects","schoolEffect",
           rhs = normal(0,30),
           child = "theta") %>%
  dag_plate("Observation","i",nodeLabels = c("sigma","y","theta")) %>%
  dag_plate("School Name","school",
            nodeLabels = "schoolEffect",
            data = schools_dat$schoolName,
            addDataNode = TRUE)

graph %>% dag_render()
## Not run: 
# below requires Tensorflow installation
drawsDF = graph %>% dag_numpyro(mcmc=TRUE)
tidyDrawsDF %>% dagp_plot()

## End(Not run)

[Package causact version 0.5.4 Index]