-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_start_example.Rmd
153 lines (113 loc) · 6.42 KB
/
quick_start_example.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
title: "Quick Start Example"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Quick Start Example}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
```{r setup}
library(abn)
```
This vignette covers the whole process of Bayesian network structure learning to parameter estimation and data simulation.
# Find the best fitting graphical structure using an exact search algorithm
## Basic workflow with the `abn` package
The package `abn` is a collection of functions for modelling of additive Bayesian networks.
It contains routines to score Bayesian Networks based on Bayesian (default) or information-theoretic formulation of generalized linear models.
Depending on the type of data, the package supports a possible mixture of continuous, discrete, and count data.
The following table shows which of distribution types are supported by for each method of estimation:
| Distribution type | `method = "bayes"` | `method = "mle"` |
| :---------------- | :----------------: | :--------------: |
| Gaussian | ✅ | ✅ |
| Binomial | ✅ | ✅ |
| Poisson | ✅ | ✅ |
| Multinomial | ❌ | ✅ |
Structure learning of additive Bayesian networks with `abn` is a three-step process.
Based on a set of model specifications (data, maximal number of possible parent nodes, restricted or enforced arcs, etc.), `abn` calculates in a first step the score of the data given the model (`buildScoreCache()`). This list of scores is then used to estimate the most probable Bayesian network structure ("structure learning") and to infer the network structure in a third step (`fitAbn()`).
Four structure-learning algorithms have been implemented in `abn`: the hill-climbing algorithm, the "exact search" algorithm, the simulated annealing algorithm and tabu search algorithm.
With the network structure inferred, the package provides routines to estimate the parameters of the network and to simulate data from the fitted additive Bayesian network model.
The following example shows how to find the best fitting graphical structure using an exact search algorithm.
## Model specification
### Load the example dataset `ex1.dag.data`
This artificial data set comes with `abn` and contains 10000 observations of 10 variables.
The variables are a mixture of continuous (`gaussian`), binary (`binomial`), and count (`poisson`) data.
The data set is a simulated data set from a known network structure.
```{r}
mydat <- ex1.dag.data
str(mydat)
```
### Set up distribution list for each node
`abn` requires a list of the type of distribution for each node in the data set.
```{r}
mydists <- list(b1="binomial",
p1="poisson",
g1="gaussian",
b2="binomial",
p2="poisson",
b3="binomial",
g2="gaussian",
b4="binomial",
b5="binomial",
g3="gaussian")
```
### Set the parent limits node-wise
The `max.par` argument sets the maximum number of parent nodes for each node in the data set.
It can be set to a single value for all nodes or to a list with the node names as keys and the maximum number of parent nodes as values.
This is a crucial parameter to speed up the model estimation in `abn` as it limits the number of possible combinations.
```{r}
# max.par <- list("b1"=1,"p1"=2,"g1"=3,"b2"=4,"p2"=1,"b3"=2,"g2"=3,"b4"=4,"b5"=1,"g3"=2) # set different max parents for each node
max.par <- 4 # set the same max parents for all nodes
```
## Build the score cache
The score cache is a list of scores for each possible parent combination for each node in the data set.
It is used to learn the structure of the Bayesian network in the next step.
```{r}
mycache <- buildScoreCache(data.df = mydat,
data.dists = mydists,
method = "bayes", # the default method is "bayes"
max.parents = max.par)
```
The minimal number of input arguments for `buildScoreCache()` is the data set and the distribution list.
By default, the function uses the Bayesian score which is based on the posterior probability of the model given the data.
To use the Log-Likelihood score, Akaike Information Criterion (AIC) or Bayesian Information Criterion (BIC) instead, the `method` argument can be set to `"mle"`.
The function `buildScoreCache()` also accepts a list of banned and retained arcs, which can be used to enforce or restrict the presence of certain arcs in the network structure.
This can be useful if prior knowledge about the network structure is available, e.g. from expert knowledge or from previous analyses it is known that certain arcs must be present or have to be absent.
The `max.parents` argument sets the maximum number of parent nodes for each node in the data set and together with the `dag.banned` and `dag.retained` arguments, it restricts the model search space and can speed up the model estimation in `abn`.
## Structure learning
The next step is to find the best fitting graphical structure of the Bayesian network.
In this example, we use the exact search algorithm to find the most probable Bayesian network structure given the score cache from the previous step.
We supply the score cache as `abnCache` object from the previous step to the structure learning function.
```{r}
mp.dag <- mostProbable(score.cache = mycache)
```
The `mostProbable()` function returns an object of class `abnLearned` which contains the most probable Bayesian network structure and the score of the model given the data.
### Plot the best fitting graphical structure
The best fitting graphical structure can be plotted using the `plotAbn()` function.
```{r}
plot(mp.dag)
```
The `plot()` function requires the `Rgraphviz` package to be installed.
## Estimate the parameters of the network
The parameters of the network can be estimated using the `fitAbn()` function.
```{r}
myfit <- fitAbn(object = mp.dag)
```
The `fitAbn()` function returns an object of class `abnFit` which contains the estimated parameters of the network.
```{r}
summary(myfit)
plot(myfit)
```
## Simulate data from the fitted model
The `simulateAbn()` function can be used to simulate data from the fitted model.
```{r}
simdat <- simulateAbn(object = myfit,
n.iter = 10000L)
summary(simdat)
```