Learn R Programming

abn (version 2.7-5)

simulateAbn: Simulate from an ABN Network

Description

Simulate one or more responses from an ABN network corresponding to a fitted object using a formula statement or an adjacency matrix.

Usage

simulateAbn(data.dists = NULL,
                       data.param = NULL,
                       data.param.var = NULL,
                       data.param.mult = NULL,
                       n.chains =10,
                       n.adapt = 1000,
                       n.thin = 100,
                       n.iter = 10000,
                       bug.file=NULL,
                       verbose=TRUE,
                       simulate=TRUE,
                       keep.file=FALSE,
                       seed=42)

Value

A data frame containing simulated data.

Arguments

data.dists

named list giving the distribution for each node in the network, see ‘Details’.

data.param

named matrix, which have to be square with as many entries as the number of variables, each element is the coefficient (for specifications see ‘Details’) used in the glm to simulate responses.

data.param.var

optional matrix, which should be square and having as many entries as number of variables, which contains the precision values for gaussian nodes. Default is set to 1.

data.param.mult

optional matrix, which should be square and having as many entries as number of variables, which contains the multinomial coefficient.

n.chains

number of parallel chains for the model.

n.thin

number of parallel chains for the model.

n.iter

number of iteration to monitor.

n.adapt

number of iteration for adaptation. If n.adapt is set to zero, then no adaptation takes place.

bug.file

name of the user specific bug file. If missing "model.bug" will be used. See ‘Details’.

verbose

logical. Default TRUE. Should R report extra information on progress?

simulate

logical. Default TRUE. If set to FALSE, no simulation will be run only creation of the bug file.

keep.file

logical. Default FALSE. If set to TRUE, the bug file generated will be kept afterwards.

seed

by default set to 42.

Author

Gilles Kratzer

Details

This function use rjags to simulate data from a DAG. It first creates a bug file, in the actual repository, then use it to simulate the data. This function output a data frame. The bug file can be run using rjags separately.

The coefficients given in the data.param are: the logit of the probabilities for binomial nodes, the means of the gaussian nodes, and the log of the Poison parameter. Additionally, a matrix data.param.var could give precision values for gaussian nodes (default is set to 1).

Binary and multinomial variables must be declared as factors, and the argument data.dists must be a list with named arguments, one for each of the variables in data.df (except a grouping variable - if present), where each entry is either "poisson", "binomial", "multinomial" or "gaussian", see examples below. The Poisson distributed variables use log and Binomial and multinomial distributed ones the logit link functions. Note that "binomial" here actually means only binary, one Bernoulli trial per row in data.df.

The number of simulated data (rows of the outputted data frame) is given by n.iter divided by n.thin.

The bug file contains a description of the model in the JAGS dialect of the BUGS language. It is possible to only generate this file and to reuse this for later or other purposes. If a bug file name is specified and the file exists, it will be used. If the file does not exist or bug.file is missing, the bug file is created. Default name is "model.bug".

The verbose argument is passed appropriately to the JAGS functions.

References

Further information about abn can be found at:
http://r-bayesian-networks.org

Examples

Run this code
## Define set of distributions:
dist<-list(a="gaussian", b="gaussian", c="gaussian", d="gaussian",
           e="binomial", f="binomial")

## Define parameter matrix:
data.param <- matrix(c(1,2,0.5,0,20,0,
                       0,1,3,10,0, 0.8,
                       0,0,1,0,0,0,
                       0,0,0,1,0,0,
                       0,0,0,0,0.5,1,
                       0,0,0,0,0,0), nrow = 6L, ncol = 6L, byrow = TRUE)

## Define precision matrix:
data.param.var <- matrix(0, nrow = 6L, ncol = 6L)
diag(data.param.var) <- c(10,20,30,40,0,0)

## Plot the dag
plotAbn(dag = ~a|b:c:e+b|c:d:f+e|f, data.dists = dist)

## Simulate the data
out <- simulateAbn(data.dists=dist, n.chains=1, n.thin=1, n.iter=1000,
            data.param=data.param, data.param.var=data.param.var)

Run the code above in your browser using DataLab