Learn R Programming

ccdrAlgorithm (version 0.0.6)

generate_mvn_data: Generate data from a DAG

Description

Given a Gaussian DAG, generate data from the underlying distribution. Equivalently, generate data from a multivariate normal distribution given one of its SEM. Can generate both observational and intervention data.

Usage

generate_mvn_data(graph, params, n = 1, ivn = NULL, ivn.rand = TRUE)

Arguments

graph

DAG in edgeList format.

params

Vector of parameters. Last p elements correspond to variances (p = number of nodes in graph), initial elements correspond to edge weights.

n

Number of samples to draw.

ivn

List of interventions (see sparsebnData). Must be a list with exactly n components.

ivn.rand

If TRUE, random N(0,1) values will be drawn for each intervention. Otherwise, these values need to supplied manually in ivn.

Details

If ivn = NULL, then n observational samples are drawn. For each component of ivn that is not NULL, interventional samples will be drawn with the values of each node specified in the component.

Examples

Run this code
# NOT RUN {
### Generate observational data
gr <- sparsebnUtils::random.graph(5, 5) # use sparsebnUtils package to generate a random graph
gr.params <- runif(10) # there are 5 coefficients + 5 variances
data.obs <- ccdrAlgorithm::generate_mvn_data(graph = gr,
                                             n = 100,
                                             params = gr.params)

### Generate experimental data
ivn <- as.list(c(rep("V1", 50), rep("V2", 50))) # 50 interventions on V1, 50 interventions on V2
data.ivn <- ccdrAlgorithm::generate_mvn_data(graph = gr,
                                             n = 100,
                                             params = gr.params,
                                             ivn = ivn)

### Use pre-specified values for interventions
###  In this toy example, we assume that all intervened nodes were fixed to
###  to the value 1, although this can be any number of course.
ivn.vals <- lapply(ivn, function(x) sapply(x, function(x) 1)) # replace all entries with a 1
data.ivn <- ccdrAlgorithm::generate_mvn_data(graph = gr,
                                             n = 100,
                                             params = gr.params,
                                             ivn = ivn.vals,
                                             ivn.rand = FALSE)

### If ivn.rand = FALSE, you must specify values
###  The code below will fail because ivn does not contain any values
### (compare to ivn.vals above).
# }
# NOT RUN {
data.ivn <- ccdrAlgorithm::generate_mvn_data(graph = gr,
                                             n = 100,
                                             params = gr.params,
                                             ivn = ivn,
                                             ivn.rand = FALSE)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab