-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path6.ABCRFcont.Rmd
executable file
·173 lines (130 loc) · 6.06 KB
/
6.ABCRFcont.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
---
######################################
# Click on "Run Document" in RStudio #
######################################
title: "ABC with Random Forests"
author: "Miguel de Navascués"
output: learnr::tutorial
runtime: shiny_prerendered
bibliography: src/references.bib
---
These learning resources are available at [GitHub](https://github.com/mnavascues/ABCRFtutorial) & [Zenodo](http://doi.org/10.5281/zenodo.1435503).
## Setup (tl;dr)
```{r setup, include=TRUE}
library(learnr)
# functions for ABCRF
library(abcrf)
# function for weighted histogram
suppressMessages(library(weights, quietly=T))
# functions for CARTs
library(tree)
```
## Classification and regression trees
Random forests are a machine learning method for regression and classification. Its name comes from the fact of being constituted of "trees". These trees are *classification and regression trees* or CARTs. So first we will make some exercises to better understand what CARTs are.
### Exercise 1
Explore CARTs using `tree()` function from `tree` package. First make a regression tree for parameter $\theta_1$ from Tajima's $D$ and $\pi$. Then make a classification tree for model from Tajima's $D$ and Fu and Li's $D$.
```{r eval=T, echo=T}
ref_table_2 <- readRDS(file="data/ref_table_2.RDS")
```
```{r cart_regression, exercise=TRUE, exercise.lines=30}
regression_tree <- tree(theta1 ~ TD + PI, data=ref_table_2)
plot(regression_tree)
text(regression_tree,cex=0.75)
par(bg = 'aliceblue')
plot(ref_table_2$TD,
ref_table_2$PI,
xlab="Tajima's D",
ylab=expression(pi),
col=grey(1-ref_table_2$theta1/max(ref_table_2$theta1)),
pch=20)
# for whatever reason this part gives an error here but runs fine in an R console, try there
partition.tree(regression_tree,
ordvars=c("TD","PI"),
add=T,cex=1)
```
```{r eval=T, echo=T}
ref_table_1 <- readRDS(file="data/ref_table_1.RDS")
ref_table_2 <- readRDS(file="data/ref_table_2.RDS")
num_of_sims <- 10000
model <- c(rep("constant population size",num_of_sims),rep("population size change",num_of_sims))
sumstats <- rbind(ref_table_1[seq_len(num_of_sims),c("S","PI","NH","TD","FLD")],
ref_table_2[seq_len(num_of_sims),c("S","PI","NH","TD","FLD")])
ref_table <-cbind(model,sumstats)
```
```{r cart_classification, exercise=TRUE, exercise.lines=30}
classification_tree <- tree(model ~ TD + FLD, data=ref_table)
plot(classification_tree)
text(classification_tree,cex=0.75)
plot(ref_table$TD[which(model=="population size change")],
ref_table$FLD[which(model=="population size change")],
xlab="Tajima's D",
ylab="Fu and Li's D",
pch=20)
points(ref_table$TD[which(model=="constant population size")],
ref_table$FLD[which(model=="constant population size")],
col="grey",pch=20)
# for whatever reason this part gives an error here but runs fine in an R console, try there
partition.tree(classification_tree,
ordvars=c("TD","FLD"),
add=T,cex=1.5,col="red")
```
## Forest
### Exercise 2
```{r eval=T, echo=T}
ref_table_1 <- readRDS(file="data/ref_table_1.RDS")
```
```{r many_trees, exercise=TRUE, exercise.lines=30}
ref_table_1 <- ref_table_1[ref_table_1$PI!=0,] # just for plotting convenience (log scale)
regression_tree <- tree(log10(theta) ~ PI, data=ref_table_1)
plot(regression_tree)
text(regression_tree,cex=0.75)
plot(ref_table_1$PI,
log10(ref_table_1$theta),
xlab=expression(pi),
ylab=expression(log[10]*theta),
pch=20, log="x")
# for whatever reason this part gives an error here but runs fine in an R console, try there
partition.tree(regression_tree,
ordvars=c("PI","theta"),
add=T,cex=1.5,col="red",lwd=2)
plot(ref_table_1$PI,
log10(ref_table_1$theta),
xlab=expression(pi),
ylab=expression(log[10]*theta),
pch=20,log="x")
for (i in 1:100){
random_sample <- sample(nrow(ref_table_1),size=500,replace=T)
ref_table_random_sample <- ref_table_1[random_sample,]
regression_tree_random_sample <- tree(log10(theta) ~ PI, data=ref_table_random_sample)
partition.tree(regression_tree_random_sample,
ordvars=c("PI","theta"),
add=T,cex=1.5,col=7,lwd=1)
}
```
## ABC with Random Forest
As we have seen above, Random Forest allows you to classify your observation in a category (i.e. model choice) and obtain an estimate of a quantitative variable (i.e. parameter estimation). But these values are not associated to posterior probabilities. The package `abcrf` [Marin et al., 2017] implements the development that allow to get these probabilities using random forest. For model choice, random forest provides you with the number of votes supporting each of the models (number of votes = number of trees leading to a given model). However, the number of votes is not necessary a good quantitative measure of the incertitude of the model choice. Pudlo et al. [2016] developed the approach to estimate the posterior probability. First, a random forest is grown for model choice:
### Exercise 3
```{r eval=T, echo=T}
target <- readRDS(file="data/target.RDS")
ref_table_1 <- readRDS(file="data/ref_table_1.RDS")
ref_table_2 <- readRDS(file="data/ref_table_2.RDS")
```
```{r abcrf_model_choice, exercise=TRUE, exercise.lines=30}
num_of_sims <- 10000
model <- c(rep("constant population size",num_of_sims),rep("population size change",num_of_sims))
sumstats <- rbind(ref_table_1[seq_len(num_of_sims),c("S","PI","NH","TD","FLD")],
ref_table_2[seq_len(num_of_sims),c("S","PI","NH","TD","FLD")])
ref_table <-cbind(model,sumstats)
model_RF <- abcrf(formula = model~.,
data = ref_table,
ntree = 1000,
lda=F,paral = T)
plot(model_RF, training=ref_table)
model_selection_result_RF <- predict(object= model_RF,
obs = target,
training = ref_table,
ntree = 1000,
paral = T,
paral.predict = T)
(model_selection_result_RF)
```