---
title: "Quick Start"
author:
  - name: Herdiantri Sufriyana
    affiliation:
    - &gibi Graduate Institute of Biomedical Informatics, College of Medical
      Science and Technology, Taipei Medical University, Taipei, Taiwan. 
    - Department of Medical Physiology, Faculty of Medicine, Universitas
      Nahdlatul Ulama Surabaya, Surabaya, Indonesia. 
    email: herdi@tmu.edu.tw
  - name: Emily Chia-Yu Su
    affiliation:
    - *gibi
    - Clinical Big Data Research Center, Taipei Medical University
      Hospital, Taipei, Taiwan. 
    - Research Center for Artificial Intelligence in Medicine, Taipei Medical
      University, Taipei, Taiwan. 
date: "2024-05-11"
output: 
  html_document:
    toc: true
    toc_depth: 3
    toc_float:
      collapsed: false
      smooth_scroll: true
vignette: >
  %\VignetteIndexEntry{Quick Start}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Define Functions and Edges

## Start by Defining Causal Structure

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

```{r echo=TRUE, results='hide', message=FALSE, warning=FALSE}
library(rcausim)
```

```{r include=FALSE}
library(knitr)
library(kableExtra)
```

```{r}
# Load predefined edge data
data(edges)
```

```{r echo=FALSE}
edges %>%
  kable() %>%
  kable_classic()
```

Assist in setting up functions based on these edges:

```{r}
# Generate function setups from edge definitions
functions <- function_from_edge(edges)
print(functions)
```

Define specific functions:

```{r}
# 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:

```{r}
# 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):

```{r echo=TRUE, results='hide', message=FALSE, warning=FALSE}
library(igraph)
```

```{r}
# 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:

```{r}
# Assume completed functions setup
data(functions)

# Generate simulated data
set.seed(1)
simulated_data <- data_from_function(functions, n = 100)
```

```{r echo=FALSE}
simulated_data %>%
  kable() %>%
  kable_classic()
```






