dag_plate: Create a plate representation for repeated nodes.

View source: R/dag_plate.R

dag_plateR Documentation

Create a plate representation for repeated nodes.

Description

[Experimental]

Given a graph object of class causact_graph, create collections of nodes that should be repeated i.e. represent multiple instances of a random variable, random vector, or random matrix. When nodes are on more than one plate, graph rendering will treat each unique combination of plates as separate plates.

Usage

dag_plate(
  graph,
  descr,
  label,
  nodeLabels,
  data = as.character(NA),
  addDataNode = FALSE,
  rhs = NA
)

Arguments

graph

a graph object of class dgr_graph created using dag_create().

descr

a longer more descriptive label for the cluster/plate.

label

a short character string to use as an index. Any . in the names is automatically replaced by ⁠_⁠ for interoperability with Python.

nodeLabels

a character vector of node labels or descriptions to include in the list of nodes.

data

a vector representing the categorical data whose unique values become the plate index. To use with addDataNode = TRUE, this vector should represent observations of a variable that can be coerced to a factor.

addDataNode

a logical value. When addDataNode = TRUE, the code attempts to add a node of observed data that is used as an index for extracting the correct parameter from parent nodes that are on the newly created plate. Verify the graphical model using dag_render() to ensure correct behavior.

rhs

Optional rhs expression for when addDataNode = TRUE. This can be either a distribution such as ⁠uniform, normal, lognormal, bernoulli,⁠ etc. or an R expression. Distribution arguments are optional. Valid values include normal(mu,sigma), normal, and normal(6,2). R computation/expression examples include alpha+beta*x. 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.

Value

an expansion of the input causact_graph object with an added plate representing the repetition of nodeLabels for each unique value of data.

Examples

# single plate example
graph = 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") %>%
  dag_node("Car Model","x",
           data = carModelDF$carModel,
           child = "y") %>%
  dag_plate("Car Model","x",
            data = carModelDF$carModel,
            nodeLabels = "theta")
graph %>% dag_render()

# multiple plate example
library(dplyr)
poolTimeGymDF = gymDF %>%
mutate(stretchType = ifelse(yogaStretch == 1,
                            "Yoga Stretch",
                            "Traditional")) %>%
group_by(gymID,stretchType,yogaStretch) %>%
  summarize(nTrialCustomers = sum(nTrialCustomers),
            nSigned = sum(nSigned))
graph = dag_create() %>%
  dag_node("Cust Signed","k",
           rhs = binomial(n,p),
           data = poolTimeGymDF$nSigned) %>%
  dag_node("Probability of Signing","p",
           rhs = beta(2,2),
           child = "k") %>%
  dag_node("Trial Size","n",
           data = poolTimeGymDF$nTrialCustomers,
           child = "k") %>%
  dag_plate("Yoga Stretch","x",
            nodeLabels = c("p"),
            data = poolTimeGymDF$stretchType,
            addDataNode = TRUE) %>%
  dag_plate("Observation","i",
            nodeLabels = c("x","k","n")) %>%
  dag_plate("Gym","j",
            nodeLabels = "p",
            data = poolTimeGymDF$gymID,
            addDataNode = TRUE)
graph %>% dag_render()

causact documentation built on Sept. 8, 2023, 5:46 p.m.