-
Notifications
You must be signed in to change notification settings - Fork 1
/
mle2bfd.Rmd
214 lines (153 loc) · 4.54 KB
/
mle2bfd.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
---
title: "MLE To Bayes Factors"
author: "Bradley T. Martin"
date: "April 4, 2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# BFD MLE to Bayes Factors
This is a workflow to generate Bayes Factors from marginal likelihood estimates (MLE) obtained via BFD/SNAPP and BEAST2 Path Sampling.
## Input BFD Data
Input: The input is a CSV file containing the following columns (order does not matter):
1. "MLE"
+ Can have NA values; they will be excluded.
2. "rank"
+ Should be blank (NA).
3. "BFD_run"
+ Can be integer or string.
4. "model"
+ Can be integer or string.
5. "BF"
+ Should be blank (NA).
First, load appropriate libraries:
```{r load.libraries, results="hide"}
library("knitr")
library("kableExtra")
```
## Loading and Summarizing the CSV file.
```{r csv2df}
file <- file.choose()
df <- read.csv(file = file, header = TRUE, sep = ",", stringsAsFactors = FALSE)
```
Produce a summary table of the ESS (Effective Samples Sizes) for each model:
```{r summarizeDF}
# Combine BFD_run and model columns for better
# display.
df["BFD Model"] <-
paste0("run",
df[, "BFD_run"],
", ",
df[, "model"])
# Combine Median, Min, and Max columns for better
# display.
df["MLE Med (Min, Max)"] <-
paste0(df[, "MLE_ESS_median"],
" (",
df[, "MLE_ESS_min"],
", ",
df[, "MLE_ESS_max"], ")")
# Get the summary columns into a new DataFrame.
run.summary <- df[, c("BFD Model",
"MLE_ESS_avg",
"MLE Med (Min, Max)",
"MLE", "MLE_SD")]
# Rename the summary columns for Table
colnames(run.summary) <-
c("BFD Model",
"ESS Mean",
"ESS Median (Min, Max)",
"MLE", "Std. Dev.")
# Make the table using knitr::kable.
kable(run.summary,
format = "latex",
align = "l",
row.names = FALSE,
digits = 2,
caption = "BFD* ESS Summary",
booktabs = TRUE) %>%
kable_styling(latex_options =
c("scale_down",
"hold_position")) %>%
row_spec(row = 0,
bold=TRUE)
```
Now remove missing data for runs that haven't finished yet:
## Remove missing data
```{r dropNA}
# This removes rows with NA in the MLE column.
completeVec <-
complete.cases(df[, "MLE"])
# Make a new dataframe without unfinished BFD runs.
df2 <- df[completeVec, ]
```
## Calculating the Bayes Factors
The below chunk of code first finds the best model based on having the higest MLE. Then it ranks them and caluculates Bayes Factors for each one like so:
BF = (2 * (model1 - model2))
A negative value indicates support for model 1.
A positive value indicates support for model 2.
Support for the models is as shown from Kass and Raftery (1995) and Adam Leache's BFD Tutorial:
0 < BF < 2 is not worth more than a bare mention
2 < BF < 6 is positive evidence
6 < BF < 10 is strong support
and BF > 10 is decisive
I made model1 the allSplit model (run20) for all comparisons.
```{r calcBF}
# Rank the MLE's. Highest = best model.
df2$rank <- rank(x = abs(df2$MLE))
# Get the MLE of rank 1.
for (i in 1:nrow(df2)) {
if (df2$rank[i] == 1)
{
best.mle <- df2$MLE[i]
}
}
# calculate the Bayes Factors.
df2[, "BF"] <- (2 * (best.mle - df2[, "MLE"]))
# Set BF of rank 1 to NA.
for (i in 1:nrow(df2)) {
if (df2$rank[i] == 1)
{
df2$BF[i] <- NA
}
}
# Sort the dataframe rows by rank column.
df2 <- df2[order(df2$rank), ]
```
Now make a nice table for the BF data:
```{r makeBFDtable}
# Make the NA go away when comparing model1 to
# itself.
options(knitr.kable.NA = "-")
# Just get columns of interest.
bfd.tabdf <-
df2[, c("BFD Model",
"MLE",
"K",
"rank",
"BF")]
# Rename columns.
colnames(bfd.tabdf) <-
c("BFD Model",
"MLE",
"K",
"Rank",
"BF")
# Write to table.
kable(bfd.tabdf,
format = "latex",
booktabs = TRUE,
digits = 2,
align = c("l", "c", "c", "c"),
row.names = FALSE,
caption = "BFD* Bayes Factors") %>%
kable_styling(full_width = TRUE,
latex_options =
"hold_position") %>%
column_spec(column = 1,
width = "9cm") %>%
row_spec(row = 0,
bold=TRUE)
```
Now it's done!