This repository was archived by the owner on Jan 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tex
More file actions
1520 lines (1386 loc) · 75.3 KB
/
Copy pathmain.tex
File metadata and controls
1520 lines (1386 loc) · 75.3 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[a4paper,man,12pt,apacite,floatsintext]{apa6} % man => jou, draftfirst
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{url}
\usepackage{tikz}
\usepackage{listings}
\usepackage{framed}
\usepackage{etoolbox}
\usepackage{float}
\usepackage{todonotes}
\usepackage{minted}
\usemintedstyle{autumn}
\floatstyle{boxed}
\restylefloat{figure}
\BeforeBeginEnvironment{verbatim}{\def\baselinestretch{1}\noindent}
\tikzset{
treenode/.style = {align=center, inner sep=2pt, text centered,
font=\sffamily, minimum size=0.5cm},
arn_c/.style = {treenode, rectangle, draw=black},
arn_d/.style = {treenode, ellipse, draw=black}
}
\title{Random Forests: Introduction, Parts, Variants}
\shorttitle{Random Forests: Introduction, Parts, Variants}
\author{Tobias Ammann}
\authornote{Tobias Ammann\\Alte Landstrasse 39\\8802 Kilchberg\\tag@adnm.ch}
\keywords{ensemble methods, introduction, random forests}
\affiliation{Literature Study at the Workgroup for Psychological Methods,\\Evaluation and Statistics, Department of Psychology.\\Supervised by Prof. Dr. Carolin Strobl}
\note{\today}
\keywords{bagging, boosting, decision trees, ensemble methods, error estimates, fuzzy random forests, introduction, optimality criterion, out-of-bag data, overfitting, PERT, random forests, regression, rotation forests, statistics, variable importance}
\abstract{This report discusses a family of machine learning algorithms called
random forests, their structure, their problems, and a number of suggested
improvements in order to give the reader a general introduction, starting with the motivation
for machine learning, and the statistical as well as historical background to machine learning and random forests.
The aim of this work is to introduce the different parts of random forest algorithms
by giving an overview over their limitations and existing suggestions on overcoming these limitations.
These components are illustrated with source code and comparisons between
different random forest algorithms.
This report also puts forward a few ideas on how to make random forests better
and more user-friendly.}
\begin{document}
\addtocounter{page}{-1} % start page counter after title page
\thispagestyle{empty} % remove page number on title page
\maketitle
\tableofcontents
\newpage
\section{Declaration of Independence}
"Hiermit versichere ich, dass ich die vorgelegte Arbeit selbständig und
ohne unerlaubte Hilfsmittel verfasst habe.
Andere als die angegebenen Hilfsmittel habe ich nicht verwendet."
\\[3cm]
Tobias Ammann\\Date: \today
\newpage
\section{Introduction}
\subsection{Motivation}
Psychology has come a long way as a science, with psychological research
more and more following the \emph{scientific method}.
Research works by the assumption that if we disprove just enough
alternative theories, we can eventually tell which theory is probably
true, despite positive proof being impossible without complete knowledge.
So, the scientific method really is nothing but the use of countless
attempts to disprove alternative theories, until only a single such theory
remains.
Since there is a large number of theories to disprove, and every
researcher likes to see the results of their work as soon as possible, a
more speedy method is usually employed, although this comes with a caveat.
The speedier method makes the researcher compare his favoured theory against
the null hypothesis to show the superiority of his favoured theory.
The null hypothesis is either an alternative theory or a chance effect.
This is way more efficient than comparing the thousands of
theories that researchers have come up with, and continue to come up with.
The caveat is commonly called confirmation bias, because researchers focus
on immunising their theory instead of disproving those of others.
Therefore, the result only has significance in the experimental set-up
being tested.
On the greater scale of things, i.e. reality, the results might well be
completely bogus.
Nonetheless, most of the subjects being studied are sufficiently constant
or change predictably enough to allow researchers to generalise from the
results of an experiment to the world at large while likely remaining correct.
This likelihood depends on the size of the effects measured in the
experiment, the amount of experimental data collected,
and on the properties of the statistical methods involved.
In psychological research, where large effects are rare and
experiments often only study a handful of psychology students, who
notwithstanding research ethics are often forced to participate, it is
vital to use powerful statistical methods, because this is the only parameter
remaining for researchers to tweak in their favour.
Recently, researchers in psychology began to turn to a new breed of
statistical methods, in hope of ever better results. This new breed of
statistical methods is called \emph{machine learning}.
In this paper, I aim to introduce the reader to \emph{random forests},
which are just one family of machine learning algorithms.
I first give the reader some context to random forests in hope that a
broader view will help in developing an intuition for random forests.
In the next sections I introduce the reader to machine learning,
the differences between the traditional statistical approach and the
machine learning approach, and random forests in particular.
I then delve into some improvements to random forests that have been
suggested in the literature, call for a more user-friendly random forest variant, and finish
with a few words on the method of writing this paper, and a discussion.
\subsection{Machine Learning}
In order to understand random forests, it helps to set
the stage by briefly discussing machine learning in general.
Machine Learning is both a part of predictive statistics and the
artificial intelligence branch of computer science.
\emph{Predictive statistics} is the sub-field of statistics that is
concerned with making predictions based on past observations.
Its probably most widely known method is \emph{linear regression}
that associates two variables \(y\) and \(x\) in such a way
that they describe a straight line: \(y = \alpha \cdot x + \beta \), but
linear regression is not limited to two variables \cite{borzlinreg}.
Linear regression is widely used in psychology because it
allows the researcher to tease apart the influence of a number of variables on an
outcome variable by making assumptions of the form
\( Y = \alpha + \beta \cdot X + \epsilon \), where \(Y\) is a vector of
the outcomes for all participants, \(\alpha\) is a constant,
\(\beta\) is a vector of weights, \(X\) is a matrix with the values of each influence variable for
every participant, and \(\epsilon\) is a vector of random differences.
Sometimes this is written as an equation for a single participant:
\( y_{i} = \beta_{0} + \beta_{1} \cdot x_{i1} + \dotsb + \beta_{n} \cdot x_{in} + \epsilon \).
This is a standard approach in personality questionnaires.
\emph{Artificial intelligence} is a field commonly associated with the
computer sciences, where it began with the advent of higher-order
programming languages based on mathematical foundations around the \emph{1960s} \cite{wpHOPL}.
Its ultimate aim is to give computers human-like capabilities,
so that they can assist us with human intelligence, better rationality
and knowledge that surpasses that of every human being.
It includes things like \emph{logic programming} \cite{wpLP, qai},
\emph{expert systems} \cite{wpES, qai}, \emph{database management systems} \cite{wpDB} and
\emph{neural networks} \cite{haykin, qai}, which all represent some form of
storing and querying knowledge.
Unfortunately, early computers back then did not have the speed and memory
required for artificial intelligence to solve many real problems.
This and overly optimistic predictions led to disappointment and the abandonment of AI research,
and the field has been largely dormant since, a state widely called \emph{AI winter} \cite{qai, wpHOAI}.
Only the rather recent coexistence of powerful computers and massive amounts
of stored data, sometimes called \emph{big data}, revived artificial
intelligence as an important field of research.
In fact, the revival and spread of machine learning has made its application
commonplace enough that I could listen to a banker telling a friend how
he uses random forests for algorithmic Bitcoin trading in his leisure time
in Starbucks.
\emph{Machine learning} is the part of artificial intelligence which
is concerned with the computer's learning of facts about the world.
These facts can then be stored and queried later on.
As such, machine learning is concerned with making statements based
on past observations and is therefore close to predictive statistics
\cite{wpML}.
\subsection{The Machine Learning Life Cycle}
This section discusses the difference between machine learning and traditional
statistical methods, as well as commonly used terminology.
The following section relies on knowledge gained from personal projects a few
years back, a university course \cite{psymeth}, as well as \citeA{barberMLC}
and \citeA{wpML}.
Authors differ in how they call the different parts of a dataset.
The part of a dataset that is being derived or partitioned is commonly called \emph{outcome},
\emph{output}, \emph{output variable}, \emph{output data}, \emph{class}, or \emph{response}.
Some authors also use names that reflect the type of data represented,
e.g. \emph{likelihood of smoking} or \emph{cancer risk}.
Common names for the parts used to derive output variables are either
generic names, such as \emph{variables}, \emph{input variables}, \emph{input}, \emph{input data}, \emph{features}, or
problem specific names, such as \emph{median income} or \emph{skin conductance}.
This paper will try to be consistent in the names being used.
It assumes datasets
to be in table form, with columns representing variables and rows representing
measurements or participants.
The paper uses the word \emph{variable} to refer to columns, and \emph{data}
to refer to the whole or parts of the dataset, i.e. rows and columns.
In most cases these two terms are interchangeable, but sometimes one is more
illustrative or more commonly used in a particular context.
Traditionally, the statistical methods used in psychology
take a model (or a family of models) of how the world works, a set of data, and return one of
the following things.
They either return a probability of how likely an improvement in prediction
can be observed at random, or how likely a difference in measurement can be
observed at random, or an estimate of what might be observed in different data,
or, building on the first two, the model in a family of models that best fits the data.
Notice, that these methods do not derive the model on their own.
\emph{Regression methods} try to derive the values of
one variable in the dataset from the other variables in the dataset using a formula the
researcher specifies.
They then compare the actual values and the prediction of the model under
different assumptions, and calculate how probable an improvement in this
comparison is to show up due to random variations.
These calculations are called \emph{tests}, and can be calculated for the
entire model, as well as for individual coefficients. \cite{wpRA}
\emph{Analysis of variance methods} partition the dataset
according to all but one variable.
They then calculate the probability with
which the variation in the one variable among the groups could be due to
random variations in the dataset.
The aim is of course to show that a difference in the values of the outcome
variable is unlikely due to chance, and hence dependent on certain
input variables \cite{wpAOV}.
The probabilities that the methods output are usually called \(p\).
Statisticians usually define a target \emph{significance level},
e.g. 5\%, and compare it with \(p\).
If \(p\) is less than the targeted significance level, then
the outcomes predicted by the model and the outcomes predicted by chance
differ significantly.
For example, a model with \(p \le 5\%\) is better than chance and its
predictions will only show up by chance in 5\% of all experiments.
However, it is important to distinguish between \(p\) and \(\hat{p}\).
The first denotes the probability found in the dataset, while the latter
indicates an estimation for \(p\).
In most cases the dataset represents a sample of a population and not the
population itself, thus the error probability can only be estimated,
hence \(\hat{p}\).
The most striking difference between traditional statistical methods and
machine learning methods is that the researcher cannot specify their model of
how the world works, other than through the selection of a machine learning
algorithm.
Because of this, machine learning algorithms are sometimes described as
\emph{black boxes}, meaning that the user can only see what is going into the
algorithm, and what is coming out.
This is unlike the statistical methods, where the researcher supplies a
formula or model.
In machine learning, algorithms derive the formula on their own.
This is what the "learning" in machine learning means.
The second difference is what the algorithms return.
Since machine learning algorithms build their models,
they cannot just output coefficients and probabilities for a given model,
but also the model itself.
In short, machine learning provides the researcher with a generic model
that adapts to the world.
The predictions of such a model can then be calculated for data for which
the values of the output variable are known, but which have not
been included during the \emph{learning phase},
to calculate the \emph{prediction accuracy}.
The problem with this flexibility is, that one cannot really tell what the
model looks like, that is, the model is not in a human-readable form.
As will be pointed out later, decision trees, the underlying mechanism in
random forests, are quite easily understandable, but random forests
consist of dozens to thousands of such trees, so that a human can hardly
tell what they mean.
It is therefore very important to find ways to condense this complexity
into something that can be more easily interpreted.
The \emph{variable importance measure} of random forests, which will be introduced
later, is one such way.
\subsection{Classification and Regression}
The \emph{classification problem} is the problem of classifying new data,
i.e. grouping it based on some criteria.
It is called a "problem", because in statistics and machine learning the
rules to do so are unknown and have to be inferred from an example dataset.
The \emph{regression problem} is the related problem, where an
\emph{output variable} has to be calculated based on \emph{input variables}.
For practical purposes, the difference is just the type of the output variable.
Classification usually uses linguistic labels, e.g. "high", "medium" and "low,
while regression returns a numeric value of arbitrary precision.
One might describe classification as regression with discreet output values,
and regression as classification with an infinite number of labels.
Output variables are commonly called \emph{classes}, while input variables
are called \emph{features}, as has already been discussed above, but
other descriptions are also common, sometimes depending on what the dataset
represents \cite{strobl2009introduction}.
\subsection{Randomness}
Many machine learning algorithms consume random numbers in different places.
While computer generated random numbers are not truly random, they still
cause the outcome of the algorithm to change slightly between different
executions, due to the fact that random number generators are customarily
initialised with the current time at the start of the program.
These changes might unnerve a novice, but do not change the outcome of the
algorithm systematically, as long as the algorithm is used correctly.
Still, for publication purposes it can make sense to set and publish the
random number generator's \emph{seed}, i.e. the value the generator is being
initialised with.
However, doing so during the experiment
is a serious mistake.
Indeed, it is good practice to run the analysis multiple times to ensure
that these random variations do not change the outcome unexpectedly.
\newpage
\section{Random Forests}
\subsection{Introduction}
This part of the paper discusses random forests.
“Random forests are a combination of tree predictors such that each tree
depends on the values of a random vector sampled independently and with
the same distribution for all trees in the forest” \cite{breiman2001random}.
Leo Breiman developed random forests with Adele Cutler, building on work
by Ho, Amit, Geman, and Dietterich \cite{wpRF}.
Although the name random forests is usually taken to refer to the random
forests as defined by \citeA{breiman2001random}, the large number of
variants that have been derived from the original forests, e.g.
\emph{Forest-rk} \cite{bernard2008forest}, \emph{RFW} \cite{maudes2012random},
\emph{DRF} \cite{bernard2012dynamic}, \emph{Fuzzy random forests} \cite{bonissone2008fuzzy},
\emph{Rotation forests} \cite{rodriguez2006rotation}, random forests that are
more random, e.g. \citeA{geurts2006extremely}, \citeA{liu2005maximising},
\citeA{cutler2001pert}, and various other improvements by
\citeA{banfield2007comparison, robnik2004improving, strobl2009introduction, zhang2012bias},
show that it
is better to think of random forests as being a framework instead of being
a single method \cite{wpRF}.
To understand this framework, it is best to look at the different aspects
of random forests, first in a top-down view, and later part by part.
The top-down view is based on \citeA{breiman2001random},
while the part by part discussion will also go into tweaking random forests,
which is where the work of other authors will be discussed.
Random forests are \emph{ensemble learning} methods where the ensemble consists
of \emph{decision trees}.
Every decision tree is constructed on a sample of the input dataset,
that has been drawn using bootstrapping with replacement from the original
dataset and is equally large.
Every node split in the decision tree is an optimal two-way split selected
from a random subset of all input variables.
The number of randomly selected variables for each split is commonly called
\texttt{mtry}.
If the number of input variables is small, additional input variables can be
derived as linear combinations of input variables.
The decision trees are grown maximally without pruning,
and new trees are generated until the ensemble of decision trees reaches its
target size, usually called \texttt{ntree}.
Random forests feature error estimates using out-of-bag data.
Out-of-bag data are the rows in the dataset that were not included during
the bootstrap aggregation, and make up approximately one third of the dataset.
Random forests also feature variable importance measures,
which are calculated by reclassifying the out-of-bag data,
but shuffling the variable under consideration.
The variable importance of the shuffled variable is the increase in
prediction error.
The reference implementation of random forests is written in Fortran,
but a package for the statistical software framework R \cite{rproject2012},
exists under the name \texttt{randomForest} \cite{liaw2002classification}.
An alternative within the R framework, which includes improvements
to correct a bias found in variable
importance measures is available under the name \texttt{party}
\cite{strobl2008conditional}.
A third implementation using Java is available in the WEKA machine learning
suite \cite{hall2009weka}.
For illustration purposes I include a source code example taken from
\citeA{strobl2008conditional}:
\begin{figure}[H]
\caption{R example using the party package.}
\begin{minted}[baselinestretch=1]{r}
load("dat_genes.rda")
library("party")
mycontrols <- cforest_unbiased(ntree=1000, mtry=20, minsplit=5)
mycforest <- cforest(status ~ ., data=dat_genes, controls=mycontrols)
\end{minted}
\end{figure}
Random forests are suitable for a wide range of applications.
Random forests have been used in applications from psychology and
computational biology as is outlined in \citeA{strobl2009introduction},
to customer churn prediction \cite{xie2009customer},
to software testing \cite{guo2004robust}, to internet security
\cite{zhang2005network}, and to natural language applications
\cite{xu2004random, kobylinski2008definition}.
The reasons why random forests are such a widely used method,
are their prediction accuracy, which is comparable to other state-of-the-art
machine learning algorithms like Adaboost as demonstrated
by \citeA{breiman2001random},
their ability to handle “small n large p” datasets \cite{strobl2009introduction},
their practical built-in error estimate, and their variable importance measures.
The last of which, random forests' variable importance measures,
might be their most useful feature.
Many domains do not require accurate predictions as much as a model that can
be understood by humans.
While ensemble methods are unsuitably complex,
random forests' variable importance measures can be used to select variables
for use in simpler models, e.g.
generalised linear models such as logit and probit models,
which are more easily interpreted \cite{strobl2009introduction}.
\subsection{Ensemble Learning Methods}
As mentioned above, random forests build on quite a rich collection of
previous work, most of which also concerns ensemble learning methods.
In fact, random forests can be seen as a composition of elements from
different other ensemble learning methods, e.g.
random subspaces \cite{ho1998random} and bagging \cite{breiman1996bagging}.
Although detailed knowledge of these methods is not a requirement to
understand random forests, it is important to understand what
ensemble learning is.
Ensemble learning is a supervised learning algorithm: its task is to take
input and output data, and find a hypothesis that connects
the two.
This hypothesis can then be used to predict the output data that
corresponds to new input data.
In theory this problem can be solved by a construct called
Bayes optimal classifier, which considers every possible hypothesis,
but which unfortunately cannot be implemented except for trivial problems.
The principle behind this is, that the combination of hypotheses becomes
a stronger hypothesis, because it can represent more functions
than every component hypothesis alone could.
In short, ensemble methods rely on the principle that the ensemble is more
than the sum of its parts.
The usual wording of this is, that ensemble learning turns a set of
\emph{weak classifiers} into one \emph{strong classifier}.
Weak also stands for unstable, meaning that the underlying classifier is
susceptible to even small variations in the input data.
Ensemble learning methods can be called meta algorithms,
because they rely on other simpler classifier algorithms,
and it is theoretically possible to create an ensemble learner for any
supervised learning algorithm. \cite{wpEL, Polikar:2009}
\subsubsection{Bagging}
The ensemble learning algorithm that most prominently underlies
random forests is bagging. Bagging stands for bootstrap aggregation.
Bootstrapping is a term that was used to describe
“[...] the process by which lumberjacks hoist themselves up trees [...]”
\cite{wpBOOT}.
In statistics it refers to the process of deriving additional samples
by resampling the original sample,
essentially simulating drawing additional samples from the population.
Bootstrap aggregation is an ensemble learning algorithm,
which trains each of its underlying weak classifiers on a different set
of input data created by bootstrapping.
It is another algorithm developed by \citeA{breiman1996bagging}.
Bagging typically uses bootstrapping with replacement,
which leads to the inclusion of on average two thirds of the original sample
in the derived sample.
The remaining third is then used as out-of-bag data.
This is a common feature of bagging and random forests \cite{breiman2001random}.
\subsubsection{Boosting}
Boosting is an ensemble method, where every weak classifiers gets to train
on the original sample, but the sample is extended with importance weights.
These weights are different for every instance of the weak classifier.
They are lower for rows in the dataset that are correctly predicted by
the classifiers already in the ensemble,
and higher for rows that are classified wrongly.
In short, boosting focuses on eliminating one classification error after
another, until all data is being classified correctly
or a targeted ensemble size is reached.
This behavior makes boosting algorithms very fast learners,
but susceptible to errors in the dataset \cite{long2010random}.
Boosting is not used in random forests as introduced by \citeA{breiman2001random},
but there are variants that do use boosting, e.g.
dynamic random forests \cite{bernard2012dynamic}.
Adaboost \cite{freund1995decision} is probably the most popular
boosting algorithm, and the algorithm random forests are most commonly
compared to in terms of performance, e.g. \citeA{breiman2001random},
\citeA{banfield2007comparison} and \citeA{rodriguez2006rotation}.
The second study compared eight ensemble of decision tree classifiers in a
large study on 57 publicly available datasets and concluded
“[...] that boosting, random forests and randomised trees are
statistically significantly better than bagging.”
\subsection{Decision Trees}
The underlying algorithm of random forests is the decision tree classifier,
which can be either a regression tree, or a classification tree.
The difference between the two types of trees is the type of output produced.
Classifications trees work with discreet output values
while regression trees output a continuous numeric value.
For classification the most commonly chosen output value among an ensemble
of decision trees is assumed to be the right prediction.
For regression purposes the output of the ensemble is calculated by averaging
the outcomes of all trees.
As the name indicates, a decision tree is a tree data structure, i.e.
a set of nodes that are connected in a forward manner allowing branching
but not cross links and circles.
Decision trees usually are binary trees, meaning that each node either has
two child nodes, or is a leaf node.
Each node represents a decision criterion, each edge a criterion match or
a mismatch, and each leaf node represents an outcome of the decision tree.
Tree data structures are typically drawn from top to bottom.
For example, the following decision tree takes two numeric features
\texttt{a}, and \texttt{b} to predict two values of a discreet output variable
called \texttt{class}.
\begin{figure}
\caption{A small decision tree.}
\begin{center}
\begin{tikzpicture}[level/.style={sibling distance = 7cm/#1}]
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\node [arn_d] {variable a}
child{ node [arn_c] {class = x} edge from parent node[left] {\(a \le 4.56\)} }
child{ node [arn_d] {variable b}
child{ node [arn_d] {variable a}
child{ node [arn_c] {class = x} edge from parent node[left] {\(a \le 9.11\)} }
child{ node [arn_c] {class = y} edge from parent node[right] {\(a > 9.11\)} }
edge from parent node[left] {\(b \le 3.1\)}
}
child{ node [arn_c] {class = x} edge from parent node[right] {\(b > 3.1\)} }
edge from parent node[right] {\(a > 4.56\)}
}
;
\end{tikzpicture}
\end{center}
\end{figure}
The decision trees used in random forests are constructed by randomly
selecting a certain number of input variables as configured by the parameter
\texttt{mtry} for each node.
The algorithm then selects the best split on one of these input
variables as the node's criterion, using what is called an optimality criterion.
This process is repeated for every child node until a node is reached
which satisfies a stop criterion, e.g.
only matches rows in the dataset with equal output values.
This node becomes a leaf node.
This is described as growing a tree maximally.
It ensures that each decision tree has a high strength,
meaning no decision tree outputs wrong predictions for the subsets of data
it matches. \cite{breiman2001random}
As a component classifier to an ensemble learning algorithm,
every decision tree should be different from the other decision trees in
the ensemble, i.e. the decision trees should be diverse enough
to cover a wide range of possible interactions between the input data,
i.e. hypotheses.
This is commonly described as the trees being uncorrelated.
Tree strength and inter-tree correlation are what the prediction accuracy
of random forests depends on. \cite{breiman2001random}
The procedure by which decision trees are grown is modified in some
variants of random forests.
The PERT (perfect random tree) algorithm \cite{cutler2001pert} for example does not perform a
search for the best split on a random selection of input variables,
but selects variable and threshold at random.
The RFW (random feature weights) algorithm \cite{maudes2012random} searches all variables but attaches random
weights to them, and the DRF (dynamic random forests) algorithm \cite{bernard2012dynamic} influences the
decision tree creation by a procedure inspired by boosting.
Other variants, e.g. \citeA{van2012accelerating}, change this procedure to
grow smaller or more balanced trees to meet performance or
hardware requirements.
\subsection{Random Forests}
As has been described in the sections on ensemble learning and decision trees,
random forests build ensembles of decision trees.
This process depends on two parameters: the ensemble size, commonly called
\texttt{ntree}, and the randomisation parameter \texttt{mtry}.
Actually, random forests depend on another parameter,
the state of a random number generator, but being random,
it does not and usually should not be specified.
The following is an example implementation of the random forest algorithm
roughly following the original version by \citeA{breiman2001random}.
I wrote this implementation to illustrate the simplicity of random forests,
as well as its components.
The implementation uses entropy instead of the Gini index as the
optimality criterion, a naive stop criterion only capable of handling
perfectly partition-able data, and lacks variable importance measures.
In reference to the higher level programming languages mentioned
in the introduction, the following source code is written in a Lisp
dialect called Clojure \cite{wpCLOJURE}, and uses a functional data structure
called \emph{zipper} \cite{huet1997zipper} for efficient tree construction.
For every tree in a random forest ensemble, the algorithm first draws a
bootstrap sample from the dataset.
The following \texttt{bootstrap} function takes a vector of rows as input
and returns an equally large bootstrap sample and the corresponding out-of-bag data.
\begin{figure}[H]
\caption{The \texttt{bootstrap} function.}
\begin{minted}[baselinestretch=1]{clojure}
(defn bootstrap [dataset]
(let [n (count dataset)
s (repeatedly n #(rand-int n))
d (set (map #(nth dataset %) s))
o (set (filter (comp not d) dataset))]
{:dataset d :oob o}))
\end{minted}
\end{figure}
For each node in a tree the algorithm first has to select the rows in the dataset
that match all criteria up to the given node.
The following \texttt{select} function takes a vector of rows, and a tuple of a variable name
and a split value as input, and returns lists of the matching and the not matching rows.
\begin{figure}[H]
\caption{The \texttt{select} function.}
\begin{minted}[baselinestretch=1]{clojure}
(defn select [[v s] dataset]
(let [l (reduce (fn [acc t]
(if (<= (get t v) s) (conj acc t) acc))
#{} dataset)
r (set (filter (comp not l) dataset))]
[l r]))
\end{minted}
\end{figure}
For each node the algorithm then progresses to check whether the node should be split,
and if so, selects the best split out of a random selection of split variables.
The \texttt{splits} function takes a dataset and \texttt{mtry} as parameters and
returns all splits as tuples with a variable name and a split value.
The \texttt{best-split} function calculates the entropy for every split and applies
the best split to two datasets at that node (sample data and out-of-bag data) simultaneously.
This is done because both sample and out-of-bag data are stored in each node to
simplify the decision tree construction and the error estimate calculation.
Since Clojure features immutable data structures this increases memory usage only negligibly.
\begin{figure}[H]
\caption{The functions \texttt{splits} and \texttt{best-split}.}
\begin{minted}[baselinestretch=1]{clojure}
(defn splits [dataset mtry]
(let [vs (filter (partial not= :class) (keys (first dataset)))
n (count vs)
vs (if (<= mtry n) (take mtry (shuffle vs)) vs)]
(reduce (fn [acc v]
(reduce (fn [acc t]
(conj acc [v (get t v)])) acc dataset))
#{} vs)))
(defn best-split [dataset oob mtry]
(let [[_ s l r]
(first (sort (map (fn [s] (let [[l r] (select s dataset)]
[(entropy l) s l r]))
(splits dataset mtry))))]
(let [[oobl oobr] (select s oob)]
[s (set l) (set r) (set oobl) (set oobr)])))
\end{minted}
\end{figure}
The \texttt{extend-node} function uses \texttt{leaf?} to check whether the node
should be split, does so if necessary using the \texttt{best-split} function, and
hands down the appropriate datasets to the child nodes.
\begin{figure}[H]
\caption{The stop criterion \texttt{leaf?} and the node splitting function \texttt{extend-node}.}
\begin{minted}[baselinestretch=1]{clojure}
(defn leaf? [dataset] (= (entropy dataset) 0.0))
(defn extend-node [{:keys [dataset oob] :as node} mtry]
(if (leaf? dataset)
(merge node
{:class (or (first (first (sort-by val >
(frequencies (map :class dataset)))))
:unknown)})
(let [[s l r oobl oobr] (best-split dataset oob mtry)]
(merge node
{:criterion s :left {:dataset l :oob oobl}
:right {:dataset r :oob oobr}}))))
\end{minted}
\end{figure}
The following \texttt{rf-train} function is the top-level function responsible for
building the ensemble of decision trees.
It creates \texttt{ntree} decision tree root nodes, each with its own bootstrapped sample
and out-of-bag datasets, walks through each tree using a zipper,
and invokes the \texttt{extend-node} for each node in every tree.
The different trees in the ensemble are constructed in parallel using the Clojure built-in \texttt{pmap},
distributing the work among all available processor cores.
\begin{figure}[H]
\caption{The \texttt{rf-train} function.}
\begin{minted}[baselinestretch=1]{clojure}
(defn rf-train [dataset ntree mtry]
(set (pmap (fn [sample]
(loop [loc (zip/zipper
(fn [node] true)
#(if (:left %)
(seq [(:left %) (:right %)]))
(fn [node children]
(with-meta
(merge node
{:left (first children)
:right (second children)})
(meta node)))
sample)]
(if (zip/end? loc) (zip/root loc)
(recur
(zip/next (zip/edit loc extend-node mtry)))))
(repeatedly ntree #(bootstrap dataset)))))
\end{minted}
\end{figure}
The \texttt{rf-predict} is another top-level function.
It takes a random forest and a row of input values, and returns the outcome as
predicted by the random forest.
Note that part of this function also runs in parallel, distributing the work among all
available processor cores.
In fact, the ability to be parallelised is one of the advantages of random forests.
\begin{figure}[H]
\caption{The \texttt{rf-predict} function.}
\begin{minted}[baselinestretch=1]{clojure}
(defn rf-predict [forest features]
(let [eval-tree
(fn eval-tree [tree]
(if (:class tree) (:class tree)
(if (let [[v s] (:criterion tree)]
(<= (get features v) s))
(eval-tree (:left tree))
(eval-tree (:right tree)))))]
(first (first
(sort-by
val > (frequencies
(filter (partial not= :unknown)
(pmap eval-tree forest))))))))
\end{minted}
\end{figure}
The following top-level function \texttt{rf-error} calculates the random forest's
error estimate by invoking \texttt{tree-error} for each tree in the ensemble.
The \texttt{tree-error} function uses the out-of-bag data stored in each leaf node
and compares it to the predicted outcome of that leaf.
Since this implementation only handles noiseless datasets, the error estimate
returned by \texttt{rf-error} will only ever be zero.
\begin{figure}[H]
\caption{The \texttt{rf-error} and \texttt{tree-error} functions calculate the error estimate.}
\begin{minted}[baselinestretch=1]{clojure}
(defn tree-error [tree]
(let [cls (:class tree)]
(cond (nil? cls) (+ (tree-error (:left tree))
(tree-error (:right tree)))
(= cls :unknown) 0
:else (count
(filter (partial = cls) (:oob tree))))))
(defn rf-error [forest]
(/ (reduce + 0
(pmap #(/ (tree-error %) (count (:oob %))) forest))
(count forest)))
\end{minted}
\end{figure}
For information on how to use this implementation of random forests,
see the example and the instructions in the appendix.
\subsubsection{Parameters}
The two parameters random forests depend on, the size of the forest
\texttt{ntree} and the number of randomly selected variables at each node
\texttt{mtry}, are problematic
in that users set them according to one recommendation or another,
or just use the default settings, which again can differ from implementation
to implementation.
Furthermore, not every dataset is best learned by random forests using the
same settings.
This lack of a standard way to determine the parameter values makes it hard
to compare the accuracy of different methods.
The comparative study by \citeA{banfield2007comparison} gives an
illustrative overview over the datasets used in previous publications,
among which are \citeA{breiman2001random} and \citeA{dietterich2000ensemble},
that each used ensemble sizes of 50-200, and applied the algorithms being
compared to 18-27 datasets.
In each case the authors concluded that their method was superior to the
other methods in the study.
However, the more recent study by \citeA{banfield2007comparison} found
“no stat. sig. improvement over bagging in 38 of 57 data sets”
when ensemble sizes of up to 1000 trees where used.
The study also concludes that bagging with an ensemble size of 1000 and
random forests with the randomisation parameter set to the
binary logarithm of the number of input variables were the best methods.
The study by \citeA{banfield2007comparison} also suggests a mechanism
to determine the best size of the ensemble automatically.
Methodologically, this would be a welcome improvement, because it eliminates
one way researchers can fiddle with the outcome of their calculations,
and because it would ensure better results due to the usually larger
ensemble sizes.
In defense of older studies, like those by \citeA{breiman2001random} and
\citeA{dietterich2000ensemble}, one has to mention that
computers back then were not as powerful.
\citeA{breiman2001random} mentions run times for random forests of 4 minutes
and 3 hours for Adaboost when building ensembles of size 100
while I can execute all examples in the paper by \citeA{strobl2009introduction}
in well under 10 seconds on one core running at 3.40 GHz, consuming about 180 Mb
of memory.
The second parameter of random forests is the randomisation parameter
\texttt{mtry} which controls how many variables are being selected randomly
at each decision tree node to search for an optimal split.
If \texttt{mtry} is set to the number of variables, random forest becomes
bagging as proposed by \citeA{breiman1996bagging}.
Common choices for \texttt{mtry} are 1, 2, and other small values in
older studies, e.g. \citeA{breiman2001random}, and the square root of n,
or the binary logarithm of n, with n being the number of input variables,
becoming increasingly popular in newer literature, e.g.
\citeA{strobl2009introduction}.
As mentioned above, using the binary logarithm of the number of
input variables is indeed the best choice for most datasets
\cite{banfield2007comparison}.
However, depending on the dataset and the ensemble size, \texttt{mtry}
needs to be chosen differently.
For example in studies of genetics, where the dataset often includes many
irrelevant input variables in addition to the dataset being a
“small n large p case”, it is necessary to use a larger value
for \texttt{mtry}, because otherwise some variables might never be used in the ensemble
at all \cite{strobl2009introduction}.
It might be interesting to consider choosing \texttt{mtry} automatically.
One way to do this, is to systematically try different values for \texttt{mtry}
and choose the best value using cross-validation \cite{psymeth}.
According to \citeA{bernard2008forest} a greedy search or choosing one of
the values discussed above is the usual approach in the literature.
The same paper shows, that this does not have to be the case, by demonstrating
a "push-button" method that automatically derives a suitable value for
\texttt{mtry} and "is at least as statistically significant as the original"
\cite{bernard2008forest}.
\subsubsection{Out-of-bag Data}
Because random forests are based on bagging and use bootstrapped samples,
each tree has a set of approximately one third of the original dataset
that has not been used to grow the tree.
This set can be used to test the prediction accuracy of each tree,
and the prediction accuracy of all trees can then be averaged to give an
error estimation for the entire random forest.
This out-of-bag error estimation is more precise than the naive error
estimate, which uses all of the dataset \cite{strobl2009introduction}.
This said, one should not forget that out-of-bag data is not the
same thing as a genuine test dataset.
Depending on the size of the training dataset and the size of the ensemble,
the downsides
of bootstrapping might shine through, and lead to an underestimation of
the prediction accuracy.
\subsubsection{Variable Importance}
As has been mentioned above, random forests have a built in variable
importance measure, which is calculated by permutating one input variable
in the out-of-bag data of every tree, and calculating a new error estimate.
The difference between the out-of-bag error estimates with and without
randomly permutated input variable is the variable importance.
The more important a variable is, the more drastically the prediction error
increases when the variable is being randomised.
The variable importance measure is sometimes scaled, i.e. z-standardised,
but because it strongly depends on the parameter of random forests,
it is not possible to compare these variable importances across studies,
hence there is little use in doing so, and the practice only encourages
problematic comparisons across studies \cite{strobl2009introduction}.
The idea behind this way of calculating variable importances is,
that one would like to compare a prediction model with and without a
particular input variable to measure the variable's impact.
Obviously, one cannot ignore all trees that incorporate a variable,
because each tree incorporates multiple input variables, and their impact
on the prediction would be modified too.
By randomly permutating the values of an input variable,
the variable's distribution characteristics do not change, but the connection to the
output variable is broken.
However, according to \citeA{strobl2007bias} the variable importance might
still depend on the type of the variable being measured, because the
variable importance measure is biased towards variables with many categories
and variables with many missing values.
Numeric variables usually have as many different values as there are rows
in the dataset, meaning that their importance measure is greatly biased due
to the large number of “categories”.
Fortunately, this can be fixed, but “Only when subsamples drawn without
replacement, instead of bootstrap samples, in combination with unbiased
split selection criteria, are used in constructing the forest, can the
resulting permutation importance be interpreted reliably”
\cite{strobl2007bias}, and correlated input variables still are
problematic.
The R package \texttt{party} includes two functions, \texttt{ctree} and
\texttt{cforest}, that use a different method for split selection and are not
affected by this bias.
Thus, \texttt{cforest} actually calculates another variable importance measure termed
“conditional variable importance” \cite{strobl2008conditional}.
Correlated variables can be problematic, because trees that include a
pair of correlated input variables are less affected by the random
permutation of one of them.
Conditional variable importance considers these correlations.
However, \citeA{gromping2009variable} argues, that this problem cannot be avoided
as long as \texttt{mtry} is smaller than the number of input variables,
and that considering all input variables, i.e. bagging,
“might already go a long way” towards remedying the problem,
although due to the “large p small n case”, “unbiased estimation of all
coefficients is impossible” in any case.
Another problem that affects the variable importance measure
is that some variables might not be well represented in a random forest.
This can be due to a small setting for \texttt{mtry} and or \texttt{ntree}
in the presence of many irrelevant input variables, as often is the case
in genetics datasets, or if variables show perfect higher order effects,
i.e. interaction effects, but no main effects.
The latter is called the \emph{XOR problem}.
It is important to note, that random forests with a different split selection
algorithm do not have to be affected by this, e.g. PERT \cite{cutler2001pert}.
Last but no least, both \citeA{strobl2009introduction} and
\citeA{gromping2009variable} see one of the advantages of random forests in
their variable importance measure.
\cite{gromping2009variable} compares linear variable importance measures
with the one built into random forests, and finds that the latter are
heavily dependent on the mtry parameter.
The larger \texttt{mtry} is, the better the importance estimates become.
Variable importance measures can be used to select input variables for a simpler
model, e.g. a generalised liner model, which is more interpretable than a
forest of decision trees \cite{strobl2009introduction}.
Variable importance measures are probably the only simple way to
“shed some light into the black box of random forests”
\cite{gromping2009variable}.
An alternative, but rather naive way to estimate variable importance is to count
the occurrence of a variable over all trees \cite{strobl2009introduction}.
Other imaginable ways to estimate variable importance would be to use
algorithms that analyse the structure of each decision tree.
Another interesting point to make is, that random perturbation of an input
variable could in theory be used as part of many other models, including but
not limited to generalised linear models, because although
this is not very useful in the case of generalised linear models,
many other complex models are similarly difficult to tease apart, e.g.
neural networks.
The way variable selection based on variable importance measures is
being done, is to find variables whose randomisation led to an improvement
in prediction accuracy.
These improvements are just random effects, and function as an indicator
for which variable importances are within the band of random fluctuations,
and which are true indicators for important variables.
\subsubsection{Regression}
Random forest can not only be used for classification, but also for regression.
To produce the numeric output values necessary for regression,
the vote on the most popular output class in the forest is replaced by the
average over all tree outputs.
One problem with regression using random forests is that more decision trees
end up covering the middle range of the output variable,
and few trees encode extreme output values.
This means that the prediction accuracy is good in the middle of the range
of values, but that the predictions for extremer values become more and more
inaccurate \cite{zhang2012bias}.
The predictions for large values tend to be too low, while the predictions for
small values tend to be too high.
\citeA{zhang2012bias} also suggests five ways to estimate and reduce this problem.
\subsubsection{Overfitting}
Overfitting \cite{wpOF} is the situation where an algorithm learns the
example dataset well enough to not only reproduce the underlying rules,
but to also reproduce the random errors in the dataset.
This is also called \emph{generalisation error}, since the algorithm
generalises errors in the data by deriving rules for them.
Random forests grow optimal decision trees, meaning they try to represent
each bootstrapped sample perfectly.
Except for the case where different rows in the sample have the same
values in their input variables, but different values in their output variable,
this leads to decision trees which represent the sample including all of its errors perfectly.
The advantage of ensemble learning is that by combining decision trees grown
for different samples and constructed using different random split selections,
these errors cancel out.
However, this also means that there is an upper bound on how accurate
random forests can become depending on the noise present in the dataset.
Of course, this only applies if the trees in an ensemble are reasonably
diverse, i.e. uncorrelated \cite{breiman2001random}.
An analysis by \citeA{liu2005maximising} showed that the generalisation error
is at its lowest when the tree ensemble is maximally diverse,
and that bootstrapping tends to limit tree diversity.
Different variants of random forests try to grow random forests on data
that is less noisy, thus limiting the risk of generalisation errors.
FRF stands for \emph{Fuzzy random forests}, and is a method described by
\citeA{bonissone2008fuzzy}.
FRF use what are called \emph{fuzzy sets} to represent the data in the dataset,
and to build their decision trees on top.
The advantages of this approach are that the resulting FRF are more immune
to noise, and \citeA{cadenas2012extending} extend this framework to
handle missing data.
Another attempt at constructing random forests from smoother data are
rotation forests by \cite{rodriguez2006rotation}, which use
\emph{PCA} (principal component analysis) to find derived input variables that
represent the input dimensions better.
Although linear combinations of input dimensions are not the same thing
as noise, since datasets are of a limited size and decision trees
might well fail to tease out the interactions between all pairs of input
variables, they will look like noisy input data if the decision trees
do not catch on.
Because better input dimensions lead to better splits, and to more
uncorrelated random variable selections, rotation forests are often more
accurate than random forests \cite{rodriguez2006rotation}.
\subsubsection{Optimality Criterion}
Random forests construct the underlying decision trees by selecting the
best split at each node from a number of randomly selected variables.
To compare the different possible splits, an optimality criterion gets
calculated for each split.
The most commonly used criterion is the Gini index.
The Gini index is “a measure of statistical dispersion developed by the
Italian statistician and sociologist Corrado Gini” \cite{wpGINI},
but it can also be used as a measure of entropy.
The latter is how it is used in random forests.