# Define Functions and Edges

## Start by Defining Causal Structure

Start by defining the causal structure as a data frame of edges:

library(rcausim)
# Load predefined edge data
data(edges)
from to
B A
A D
A C
B C
D C
A E
C E

Assist in setting up functions based on these edges:

# Generate function setups from edge definitions
functions <- function_from_edge(edges)
print(functions)
## 0 / 5  vertices have functions.
## B = function(n)
## A = function(B)
## D = function(A)
## C = function(A,B,D)
## E = function(A,C)

Define specific functions:

# Define a function for vertex B
function_B <- function(n){ rnorm(n, mean = 90, sd = 5) }
functions <- define(functions, 'B', function_B)
print(functions)
## 1 / 5  vertices have functions.
## A = function(B)
## D = function(A)
## C = function(A,B,D)
## E = function(A,C)

## Start by Defining Functions

You can also start by defining functions directly:

# Define a function for vertex B
function_B <- function(n){ rnorm(n, mean = 90, sd = 5) }

# Define a function for vertex A
function_A <- function(B){ ifelse(B>=95, 1, 0) }

# Combine functions in a list
functions <- list(A = function_A, B = function_B)
functions <- function_from_user(functions)

Ensure the causal structure is a directed acyclic graph (DAG):

library(igraph)
# Set up edges based on functions
edges <- edge_from_function(functions)

# Check if the resulting edges form a DAG
g <- graph_from_data_frame(edges, directed = TRUE)
is_dag(g)
## [1] TRUE

# Data Simulation

Generate simulated data based on the predefined functions:

# Assume completed functions setup
data(functions)

# Generate simulated data
set.seed(1)
simulated_data <- data_from_function(functions, n = 100)
B A D C E
86.86773 0 0.0 0.0000000 0
90.91822 0 0.0 0.0000000 0
85.82186 0 0.0 0.0000000 0
97.97640 0 0.0 0.0000000 0
91.64754 0 0.0 0.0000000 0
85.89766 0 0.0 0.0000000 0
92.43715 0 0.0 0.0000000 0
93.69162 0 0.0 0.0000000 0
92.87891 0 0.0 0.0000000 0
88.47306 0 0.0 0.0000000 0
126.75281 1 0.3 1.0000000 1
91.94922 0 0.0 0.0000000 0
86.89380 0 0.0 0.0000000 0
119.72160 1 0.3 0.9958240 1
95.62465 0 0.0 0.0000000 0
89.77533 0 0.0 0.0000000 0
89.91905 0 0.0 0.0000000 0
115.22897 1 0.3 0.9284346 1
106.05957 1 0.3 0.7908935 1
92.96951 0 0.0 0.0000000 0
94.59489 0 0.0 0.0000000 0
93.91068 0 0.0 0.0000000 0
90.37282 0 0.0 0.0000000 0
80.05324 0 0.0 0.0000000 0
93.09913 0 0.0 0.0000000 0
89.71936 0 0.0 0.0000000 0
89.22102 0 0.0 0.0000000 0
82.64624 0 0.0 0.0000000 0
87.60925 0 0.0 0.0000000 0
92.08971 0 0.0 0.0000000 0
96.79340 0 0.0 0.0000000 0
89.48606 0 0.0 0.0000000 0
91.93836 0 0.0 0.0000000 0
89.73097 0 0.0 0.0000000 0
83.11470 0 0.0 0.0000000 0
87.92503 0 0.0 0.0000000 0
88.02855 0 0.0 0.0000000 0
89.70343 0 0.0 0.0000000 0
95.50013 0 0.0 0.0000000 0
93.81588 0 0.0 0.0000000 0
89.17738 0 0.0 0.0000000 0
88.73319 0 0.0 0.0000000 0
93.48482 0 0.0 0.0000000 0
92.78332 0 0.0 0.0000000 0
86.55622 0 0.0 0.0000000 0
86.46252 0 0.0 0.0000000 0
91.82291 0 0.0 0.0000000 0
93.84266 0 0.0 0.0000000 0
89.43827 0 0.0 0.0000000 0
97.68810 0 0.0 0.0000000 0
91.99053 0 0.0 0.0000000 0
103.87212 1 0.3 0.7580817 0
91.70560 0 0.0 0.0000000 0
84.35318 0 0.0 0.0000000 0
97.16512 0 0.0 0.0000000 0
99.90200 0 0.0 0.0000000 0
88.16389 0 0.0 0.0000000 0
84.77933 0 0.0 0.0000000 0
92.84860 0 0.0 0.0000000 0
135.00043 1 0.3 1.0000000 1
102.00809 1 0.3 0.7301213 0
89.80380 0 0.0 0.0000000 0
93.44870 0 0.0 0.0000000 0
90.14001 0 0.0 0.0000000 0
86.28363 0 0.0 0.0000000 0
90.94396 0 0.0 0.0000000 0
80.97521 0 0.0 0.0000000 0
97.32777 0 0.0 0.0000000 0
90.76627 0 0.0 0.0000000 0
100.86306 1 0.3 0.7129459 0
92.37755 0 0.0 0.0000000 0
86.45027 0 0.0 0.0000000 0
93.05363 0 0.0 0.0000000 0
85.32951 0 0.0 0.0000000 0
83.73183 0 0.0 0.0000000 0
91.45723 0 0.0 0.0000000 0
87.78354 0 0.0 0.0000000 0
90.00553 0 0.0 0.0000000 0
90.37171 0 0.0 0.0000000 0
87.05240 0 0.0 0.0000000 0
87.15666 0 0.0 0.0000000 0
89.32411 0 0.0 0.0000000 0
95.89043 0 0.0 0.0000000 0
82.38217 0 0.0 0.0000000 0
92.96973 0 0.0 0.0000000 0
91.66475 0 0.0 0.0000000 0
95.31550 0 0.0 0.0000000 0
88.47908 0 0.0 0.0000000 0
91.85009 0 0.0 0.0000000 0
91.33549 0 0.0 0.0000000 0
87.28740 0 0.0 0.0000000 0
96.03934 0 0.0 0.0000000 0
110.68100 1 0.3 0.8602150 1
93.50107 0 0.0 0.0000000 0
97.93417 0 0.0 0.0000000 0
92.79243 0 0.0 0.0000000 0
83.61704 0 0.0 0.0000000 0
87.13367 0 0.0 0.0000000 0
83.87694 0 0.0 0.0000000 0
87.63300 0 0.0 0.0000000 0