Quick Start

Define Functions and Edges

Start by Defining Causal Structure

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

library(rcausim)
library(knitr)
library(kableExtra)
# Load predefined edge data
data(edges)
edges %>%
  kable() %>%
  kable_classic()

Assist in setting up functions based on these edges:

# Generate function setups from edge definitions
functions <- function_from_edge(edges)
print(functions)

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)

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)

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)
simulated_data %>%
  kable() %>%
  kable_classic()


Try the rcausim package in your browser

Any scripts or data that you put into this service are public.

rcausim documentation built on June 24, 2024, 5:06 p.m.