View source: R/node_identity.r
| node_identity | R Documentation |
This node type may be used to generate a new node given a regular R expression that may include function calls or any other valid R syntax. This may be useful to combine components of a node which need to be simulated with separate node calls, or just as a convenient shorthand for some variable transformations. Also allows calculation of just the linear predictor and generation of intermediary variables using the enhanced formula syntax.
node_identity(data, parents, formula, kind="expr",
betas, intercept, var_names=NULL,
name=NULL, dag=NULL)
data |
A |
parents |
A character vector specifying the names of the parents that this particular child node has. When using this function as a node type in |
formula |
A |
kind |
A single character string specifying how the |
betas |
Only used internally when |
intercept |
Only used internally when |
var_names |
Only used when |
name |
A single character string, specifying the name of the node. Passed internally only. See |
dag |
The |
When using kind="expr", custom functions and objects can be used without issues in the formula, but they need to be present in the global environment, otherwise the underlying eval() function call will fail. Using this function outside of node or node_td is essentially equal to using with(data, eval(formula)) (without the ~ in the formula). If kind!="expr", this function cannot be used outside of a defined DAG.
Please note that when using identity nodes with kind="data" and multiple terms in formula, the printed structural equations and plots of a dag object may not be correct.
Returns a numeric vector of length nrow(data).
Robin Denz
empty_dag, node, node_td, sim_from_dag, sim_discrete_time
library(simDAG)
set.seed(12455432)
#### using kind = "expr" ####
# define a DAG
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5) +
node("something", type="identity", formula= ~ age + sex + 2)
sim_dat <- sim_from_dag(dag=dag, n_sim=100)
head(sim_dat)
# more complex alternative
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5) +
node("something", type="identity",
formula= ~ age / 2 + age^2 - ifelse(sex, 2, 3) + 2)
sim_dat <- sim_from_dag(dag=dag, n_sim=100)
head(sim_dat)
#### using kind = "linpred" ####
# this would work with both kind="expr" and kind="linpred"
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5) +
node("pred", type="identity", formula= ~ 1 + age*0.2 + sex*1.2,
kind="linpred")
sim_dat <- sim_from_dag(dag=dag, n_sim=10)
head(sim_dat)
# this only works with kind="linpred", due to the presence of a special term
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5, output="numeric") +
node("pred", type="identity", formula= ~ 1 + age*0.2 + sex*1.2 + age:sex*-2,
kind="linpred")
sim_dat <- sim_from_dag(dag=dag, n_sim=10)
head(sim_dat)
#### using kind = "data" ####
# simply return the transformed data, useful if the terms are used
# frequently in multiple nodes in the DAG to save computation time
# using only a single interaction term
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5, output="numeric") +
node("age_sex_interact", type="identity", formula= ~ age:sex, kind="data")
sim_dat <- sim_from_dag(dag=dag, n_sim=10)
head(sim_dat)
# using multiple terms
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5, output="numeric") +
node("name_not_used", type="identity", formula= ~ age:sex + I(age^2),
kind="data", var_names=c("age_sex_interact", "age_squared"))
sim_dat <- sim_from_dag(dag=dag, n_sim=10)
head(sim_dat)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.