This tutorial focuses on tree-based models and their implementation in R. The entire R markdown document for this tutorial can be downloaded here. For the more advanced, a recommendable resource for tree-based modeling is Prasad, Iverson, and Liaw (2006) or Gries (2021). Very good papers dealing with many critical issues related to tree-based models are Strobl, Malley, and Tutz (2009) and Breiman (2001b).

This tutorial is based on R. If you have not installed R or are new to it, you will find an introduction to and more information how to use R here. For this tutorials, we need to install certain *packages* from an R *library* so that the scripts shown below are executed without errors. Before turning to the code below, please install the packages by running the code below this paragraph. If you have already installed the packages mentioned below, then you can skip ahead and ignore this section. To install the necessary packages, simply run the following code - it may take some time (between 1 and 5 minutes to install all of the libraries so you do not need to worry if it takes some time).

```
# clean current workspace
rm(list=ls(all=T))
# set options
options(stringsAsFactors = F) # no automatic data transformation
options("scipen" = 100, "digits" = 4) # suppress math annotation
# install libraries
install.packages(c("Boruta", "caret", "cowplot", "tidyverse", "flextable",
"ggparty", "Gmisc", "grid", "Hmisc",
"knitr", "party", "partykit", "randomForest",
"Rling", "DT"))
```

Once you have installed R, R-Studio, and have also initiated the session by executing the code shown above, you are good to go.

This section deals with tree-structure models which fall into the machine-learning rather than the inference statistics category as they are commonly used for classification and prediction tasks rather than explanation of relationships between variables.

The most basic type of tree-structure model is a decision tree or CART (classification and regression tree). A more optimized version of these simple decision trees are conditional inference trees (CITs) - although CART and CITs are commonly treated as one and the same thing although CITs differ from CARTs in that they provide more accurate variable importance measures. Like random forests, inference trees are non-parametric and thus do not rely on distributional requirements (or at least on fewer). The tree structure represents recursive partitioning of the data to minimize residual deviance. Several advantages have been associated with using tree-based models:

Tree-structure models are very useful because they can deal with different types of variables and provide a very good understanding of the structure in the data.

Tree-structure models have been deemed particularly interesting for linguists because they can handle moderate sample sizes and many high-order interactions better then regression models shows that there can be issues especially when dealing with small data samples, single trees (rather than forests), and data where the variance is predictable based on a single interaction (as shown by Gries (2021), chapter 7).

Tree-structure models are (supposedly) better at detecting non-linear or non-monotonic relationships between predictors and dependent variables.

Tree-structure models are easy to implement in R and do not require the model selection, validation, and diagnostics associated with regression models.

Tree-structure models can be used as variable-selection procedure which informs about which variables have any sort of significant relationship with the dependent variable and can thereby inform model fitting.

Despite these potential advantages, a word of warning is in order: Gries (2021) admits that tree-based models can be very useful but there are some issues that but some serious short-comings of tree-structure models remain under-explored. For instance,

Tree-structure models only inform about the importance of a variable but not if the variable is important as a main effect or as part of interactions (or both)! The importance only shows that there is some important connection between the predictor and the dependent variable.

Simple tree-structure models have been shown to fail in detecting the correct predictors if the variance is solely determined by a single interaction (Gries 2021, chap. 7.3). This failure is caused by the fact that the predictor used in the first split of a tree is selected as the one with the strongest main effect (Boulesteix et al. 2015, 344). This issue can, however, be avoided by hard-coding the interactions as predictors plus using ensemble methods such as random forests rather than individual trees (see Gries 2021, chap. 7.3).

Another shortcoming is that tree-structure models partition the data (rather than “fitting a line” through the data which can lead to more coarse-grained predictions compared to regression models when dealing with numeric dependent variables (again, see Gries 2021, chap. 7.3).

Boulesteix et al. (2015), 341 state that high correlations between predictors can hinder the detection of interactions when using small data sets. However, regression do not fare better here as they are even more strongly affected by (multi-)collinearity (see @Gries (2021), chapter 7.3).

Tree-structure models are bad a detecting interactions when the variables have strong main effects which is, unfortunately, common when dealing with linguistic data (Wright, Ziegler, and König 2016).

Before we implement a conditional inference tree in R, we will have a look at how decision trees work. We will do this in more detail here as random forests and Boruta analyses are extensions of inference trees and are therefore based on the same concepts.

The most basic type of tree-structure model is a decision tree. There are two types of decision trees: classification and regression trees (CARTs) and conditional inference trees (CITs). As CITs are much better at determining the *true* effect of a predictor, i.e. the effect of a predictor if all other effects are simultaneously considered, we will focus on CITs in the following. If you are interested in generating a CART, Gries (2021) shows how to implement and prune them (pruning is also not necessary for CITs).

Below is a conditional inference tree which shows how and what factors contribute to the use of discourse *like*. All conditional inference trees answer a simple question, namely *How do we classify elements based on the given predictors?*. The answer that conditional inference trees provide is the classification of the elements based on the levels of the predictors. If predictors are not significant or all elements can be classified correctly without them (i.e. if these predictors are unnecessary), then they are not included in the decision tree (or in the model fitting process). The conditional inference tree shows that the best predictor for discourse *like* use is age as it is the highest node. Among young speakers, those with high status use like more compared with speakers of lower social status. Among old speakers, women use discourse *like* more than men.

Before going through how this conditional decision tree is generated, let us first go over some basic concepts. The top of the decision tree is called “root” or “root node”, the categories at the end of branches are called “leaves” or “leaf nodes”. Nodes that are in-between the root and leaves are called “internal nodes” or just “nodes”. The root node has only arrows or lines pointing away from it, internal nodes have lines going to and from them, while leaf nodes only have lines pointing towards them.

Let us now go over the process by which the decision tree above is generated. In our example, we want to predict whether a person makes use of discourse *like* given their age, gender, and social status. The first six lines of this fictitious data set are displayed in the table below:

In a first step, we initialize our R session by setting options and activating the necessary packages.

```
# activate libraries
library(caret)
library(DT)
library(flextable)
library(partykit)
library(tidyverse)
library(grid)
library(Gmisc)
library(cowplot)
library(randomForest)
library(party)
library(Hmisc)
library(Boruta)
library(RCurl)
# set options
options(stringsAsFactors = F)
options(scipen = 999)
options(max.print=10000)
```

NOTE

In some cases, installing the `caret`

package can be a bit more complicated. In my case, it was necessary to execute the code chunk shown below. However, once the `caret`

package is installed, you do not need to go through these steps again and can simply activate it by calling `library(caret)`

.

```
# install caret library
source("https://bioconductor.org/biocLite.R"); biocLite(); library(Biobase)
install.packages("Biobase", repos=c("http://rstudio.org/_packages", "http://cran.rstudio.com",
"http://cran.rstudio.com/", dependencies=TRUE))
install.packages("dimRed", dependencies = TRUE)
install.packages('caret', dependencies = TRUE)
# activate caret library
```

We can now load and inspect the data that we will use in this tutorial.

```
# load data
citdata <- read.delim("https://raw.githubusercontent.com/MartinSchweinberger/coedlss2019materials/master/datatables/treedata.txt", header = T, sep = "\t")
# inspect data
ft <- flextable(head(citdata,10)) %>%
set_caption("First 10 rows of the citdata data set.")
ft
```

Age | Gender | Status | LikeUser |

15-40 | female | high | no |

15-40 | female | high | no |

15-40 | male | high | no |

41-80 | female | low | yes |

41-80 | male | high | no |

41-80 | male | low | no |

41-80 | female | low | yes |

15-40 | male | high | no |

41-80 | male | low | no |

41-80 | male | low | no |

An interactive display of the data is provided below.

The output tells us that the data is not factorized yet. As tree-based models require either numeric or factorized data, we factorize the “character” variables in our data.

```
# factorize variables (cit require factors instead of character vectors)
fcts <- c("Age", "Gender", "Status", "LikeUser")
citdata[fcts] <- lapply(citdata[fcts], factor)
# inspect data
head(citdata, 10)
```

```
## Age Gender Status LikeUser
## 1 15-40 female high no
## 2 15-40 female high no
## 3 15-40 male high no
## 4 41-80 female low yes
## 5 41-80 male high no
## 6 41-80 male low no
## 7 41-80 female low yes
## 8 15-40 male high no
## 9 41-80 male low no
## 10 41-80 male low no
```

The data now consists of factors which two levels each. After initializing the R session, it needs to be determined, what the root of the decision tree should be. This means that we have to determine which of the variables represents the root node. In order to do so, we tabulate for each variable level, how many speakers of that level have used discourse *like* (LikeUsers) and how many have not used discourse *like* (NonLikeUsers).

```
# tabulate data
table(citdata$LikeUser, citdata$Gender)
```

```
##
## female male
## no 43 75
## yes 91 42
```

`table(citdata$LikeUser, citdata$Age)`

```
##
## 15-40 41-80
## no 34 84
## yes 92 41
```

`table(citdata$LikeUser, citdata$Status)`

```
##
## high low
## no 33 85
## yes 73 60
```

None of the predictors is perfect (the predictors are therefore referred to as “impure”). To determine which variable is the root, we will calculate the degree of “impurity” for each variable - the variable which has the lowest impurity value will be the root.

The most common measure of impurity in the context of conditional inference trees is called *Gini*. The Gini value or gini index was introduced by Corrado Gini as a measure for income inequality. In our case we seek to maximize inequality of distributions of leave nodes which is why the gini index is useful for tree based models. For each level we apply the following equation to determine the gini impurity value:

\[\begin{equation} G_{x} = 1 - ( p_{1} )^{2} - ( p_{0} )^{2} \end{equation}\]

For the node for men, this would mean the following:

\[\begin{equation} G_{men} = 1-(\frac{42} {42+75})^{2} - (\frac{75} {42+75})^{2} = 0.4602235 \end{equation}\]

For women, we calculate G or Gini as follows:

\[\begin{equation} G_{women} = 1-(\frac{91} {91+43})^{2} - (\frac{43} {91+43})^{2} = 0.4358432 \end{equation}\]

To calculate the Gini value of Gender, we need to calculate the weighted average leaf node impurity (weighted because the number of speakers is different in each group). We calculate the weighted average leaf node impurity using the equation below.

\[\begin{equation} G_{Gender} = \frac{N_{men}} {N_{Total}} \times G_{men} + \frac{N_{women}} {N_{Total}} \times G_{women} G_{Gender} = \frac{159} {303} \times 0.4602235 + \frac{144} {303} \times 0.4358432 = 0.4611915 \end{equation}\]

Before we do these calculations in R, we will just briefly revisit the gender distribution and perform a \(\chi\)^{2}-test to see whether testing gender makes sense in the first place.

```
# GENDER
# re-inspect gender distribution
tblikegender <- table(citdata$LikeUser, citdata$Gender)
tblikegender
```

```
##
## female male
## no 43 75
## yes 91 42
```

`chisq.test(tblikegender) # sig difference: data can be split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tblikegender
## X-squared = 24.428, df = 1, p-value = 0.0000007714
```

The \(\chi\)^{2}-test shows that testing if we should split by gender does make sense as there are significant differences between men and women. We will now perform the gini-calculation for gender (see below).

```
# calculate Gini for men
gini_men <- 1-(42/(42+75))^2 - (75/(42+75))^2
# calculate Gini for women
gini_women <- 1-(91/(91+43))^2 - (43/(91+43))^2
# calculate weighted average of Gini for Gender
gini_gender <- 42/(42+75)* gini_men + 91/(91+43) * gini_women
gini_gender
```

`## [1] 0.4611915`

The gini for gender is .461. In a next step, we revisit the age distribution and also perform a \(\chi\)^{2}-test.

```
# re-inspect age distribution
tblikeage <- table(citdata$LikeUser, citdata$Age)
tblikeage
```

```
##
## 15-40 41-80
## no 34 84
## yes 92 41
```

`chisq.test(tblikeage) # sig difference: data can be split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tblikeage
## X-squared = 39.141, df = 1, p-value = 0.0000000003943
```

As the \(\chi\)^{2}-test returns a significant result, we continue to calculate the gini value for age.

```
# calculate Gini for age groups
gini_young <- 1-(92/(92+34))^2 - (34/(92+34))^2 # Gini: young
gini_old <- 1-(41/(41+84))^2 - (84/(41+84))^2 # Gini: old
# calculate weighted average of Gini for Age
gini_age <- 92/(92+34)* gini_young + 41/(41+84) * gini_old
gini_age
```

`## [1] 0.4323148`

The gini for age is .432 and we continue by revisiting the status distribution and also perform a \(\chi\)^{2}-test.

```
# re-inspect status distribution
tblikestatus <- table(citdata$LikeUser, citdata$Status)
tblikestatus
```

```
##
## high low
## no 33 85
## yes 73 60
```

`chisq.test(tblikestatus) # sig difference: data can be split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tblikestatus
## X-squared = 17.488, df = 1, p-value = 0.00002892
```

As the \(\chi\)^{2}-test returns a significant result, we continue to calculate the gini value for status.

```
gini_high <- 1-(73/(33+73))^2 - (33/(33+73))^2 # Gini: high
gini_low <- 1-(60/(60+85))^2 - (85/(60+85))^2 # Gini: low
# calculate weighted average of Gini for Status
gini_status <- 73/(33+73)* gini_high + 60/(60+85) * gini_low
gini_status
```

`## [1] 0.4960521`

The gini for status is .496 and we can now compare the gini values for age, gender, and status.

```
# compare age, gender, and status ginis
gini_age; gini_gender; gini_status
```

`## [1] 0.4323148`

`## [1] 0.4611915`

`## [1] 0.4960521`

Since age has the lowest gini (impurity) value, our first split is by age and age, thus, represents our root node. Our manually calculated conditional inference tree right now looks as below.

In a next step, we need to find out which of the remaining variables best separates the speakers who use discourse *like* from those that do not under the first node. In order to do so, we calculate the Gini values for *Gender* and *SocialStatus* for the *41-80* node.

In a first step, we tabulate the old speakers by gender, inspect the distribution, and use a \(\chi\)^{2}-test to evaluate whether splitting makes sense.

```
# 2ND NODE
# split data according to first split (only old data for now)
old <- citdata[citdata$Age == "41-80",]
# inspect distribution
tboldgender <- table(old$LikeUser, old$Gender)
tboldgender
```

```
##
## female male
## no 26 58
## yes 33 8
```

`chisq.test(tboldgender) # sig difference: data can be split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tboldgender
## X-squared = 25.176, df = 1, p-value = 0.0000005232
```

As the \(\chi\)^{2}-test returns a significant result, we continue to calculate the gini value for gender among old speakers.

```
# calculate Gini for Gender
# calculate Gini for men
gini_oldmen <- 1-(tboldgender[2,2]/sum(tboldgender[,2]))^2 - (tboldgender[1,2]/sum(tboldgender[,2]))^2
# calculate Gini for women
gini_oldwomen <- 1-(tboldgender[2,1]/sum(tboldgender[,1]))^2 - (tboldgender[1,1]/sum(tboldgender[,1]))^2
# # calculate weighted average of Gini for Gender
gini_oldgender <- sum(tboldgender[,2])/sum(tboldgender)* gini_oldmen + sum(tboldgender[,1])/sum(tboldgender) * gini_oldwomen
gini_oldgender
```

`## [1] 0.3451628`

The gini values for gender among old speakers is .345. We continue by inspecting the status distribution among old speakers.

```
# calculate Gini for Status
# inspect distribution
tboldstatus <- table(old$LikeUser, old$Status)
tboldstatus
```

```
##
## high low
## no 22 62
## yes 16 25
```

`chisq.test(tboldstatus) # sig difference: data can be split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tboldstatus
## X-squared = 1.5811, df = 1, p-value = 0.2086
```

Since the \(\chi\)^{2}-test returns a non-significant result, we do not need to calculate the gini value for status and split the old speakers by gender without further calculation. In a next step, we determine if we need to further divide the 3^{rd} node (old male speakers) by status. To do so, we inspect the status distribution for old male speakers and perform a \(\chi\)^{2}-test.

```
# 3RD NODE
# split data according to first split (only old data for now)
oldmale <- citdata %>%
dplyr::filter(Age == "41-80") %>%
dplyr::filter(Gender == "male")
# inspect distribution
tboldmalestatus <- table(oldmale$LikeUser, oldmale$Status)
tboldmalestatus
```

```
##
## high low
## no 17 41
## yes 5 3
```

`chisq.test(tboldmalestatus) # no sig difference: no more splits!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tboldmalestatus
## X-squared = 2.1514, df = 1, p-value = 0.1424
```

Since the \(\chi\)^{2}-test reports no significant differences, we have reached an end (“leave”) of this branch of our tree. We will now move on and check if we need to split the 4^{th} node (old female speakers) by status.

```
# 4TH NODE
# split data according to first split (only old data for now)
oldfemale <- citdata %>%
dplyr::filter(Age == "41-80") %>%
dplyr::filter(Gender == "female")
# inspect distribution
tboldfemalestatus <- table(oldfemale$LikeUser, oldfemale$Status)
tboldfemalestatus
```

```
##
## high low
## no 5 21
## yes 11 22
```

`chisq.test(tboldfemalestatus) # no sig difference: no more splits!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tboldfemalestatus
## X-squared = 0.83679, df = 1, p-value = 0.3603
```

Here, too, we have reached the end of this branch (“leave”) because the \(\chi\)^{2}-test reports no significant differences. We can thus move on to the young speakers (the 5^{th} node) and test if and how to split this branch.

```
# 5TH NODE
# split data according to first split (only young data)
young <- citdata %>%
dplyr::filter(Age == "15-40")
# inspect distribution
tbyounggender <- table(young$LikeUser, young$Gender)
tbyounggender
```

```
##
## female male
## no 17 17
## yes 58 34
```

`chisq.test(tbyounggender) # no sig difference: do not split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tbyounggender
## X-squared = 1.2535, df = 1, p-value = 0.2629
```

As the \(\chi\)^{2}-test does not report significant differences, we inspect the status distribution and check if low and high-status speakers differ significantly.

```
# calculate Gini for Status
# inspect distribution
tbyoungstatus <- table(young$LikeUser, young$Status)
tbyoungstatus
```

```
##
## high low
## no 11 23
## yes 57 35
```

`chisq.test(tbyoungstatus) # sig difference: split!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tbyoungstatus
## X-squared = 7.6066, df = 1, p-value = 0.005816
```

Since the \(\chi\)^{2}-test reports a significant result and gender was reported as being insignificant, we split by status without having to calculate the gini for status here. In a next step, we inspect the distribution of young low status speakers and test if we should split by gender.

```
# 6TH NODE
# split data according to first and second split (young and low status data)
younglow <- citdata %>%
dplyr::filter(Age == "15-40") %>%
dplyr::filter(Status == "low")
# inspect gender distribution
tbyounglowgender <- table(younglow$LikeUser, younglow$Gender)
tbyounglowgender
```

```
##
## female male
## no 13 10
## yes 21 14
```

`chisq.test(tbyounglowgender) # no sig difference: no more splits!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tbyounglowgender
## X-squared = 0, df = 1, p-value = 1
```

Here, too, we have reached the end of this branch (“leave”) because the \(\chi\)^{2}-test reports no significant differences. We can thus move on to the young high-status speakers (the 7^{th} node) and test if we need to split this branch by gender. Again, we inspect the distribution and perform a \(\chi\)^{2}-test.

```
# 7TH node
# split data according to first and second split (young and high-status data)
younghigh <- citdata %>%
dplyr::filter(Age == "15-40") %>%
dplyr::filter(Status == "high")
# inspect gender distribution
tbyounghighgender <- table(younghigh$LikeUser, younghigh$Gender)
tbyounghighgender
```

```
##
## female male
## no 4 7
## yes 37 20
```

`chisq.test(tbyounghighgender) # no sig difference: no more splits!`

```
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tbyounghighgender
## X-squared = 2.0598, df = 1, p-value = 0.1512
```

The lack of a significant difference reported by the \(\chi\)^{2}-test tells us that we have reached our final tree as all branches end in leaves now.

This analysis can be implemented much more smoothly in R, of course, which we will do below. Just a quick word on setting seeds. When we assign or set a seed, this means that any random number that is generated will be stored which makes analyses reproducible. Otherwise we would, of course, get different results if we relied on newly randomly generated numbers.

```
# set.seed (to store random numbers and thus make results reproducible)
set.seed(2019120202)
# create initial conditional inference tree model
citd.ctree <- partykit::ctree(LikeUser ~ Age + Gender + Status,
data = citdata)
plot(citd.ctree, gp = gpar(fontsize = 8)) # plot final ctree
```

In addition to plotting the conditional inference tree, we can also check its accuracy. To do so, we predict the use of like based on the conditional inference tree and compare them to the observed uses of like. Then we use the `confusionMatrix`

function from the `caret`

package to get an overview of the accuracy statistics.

```
citprediction <- predict(citd.ctree)
confusionMatrix(citprediction, citdata$LikeUser)
```

```
## Confusion Matrix and Statistics
##
## Reference
## Prediction no yes
## no 58 8
## yes 60 125
##
## Accuracy : 0.7291
## 95% CI : (0.6696, 0.7831)
## No Information Rate : 0.5299
## P-Value [Acc > NIR] : 0.00000000007873
##
## Kappa : 0.4424
##
## Mcnemar's Test P-Value : 0.00000000062237
##
## Sensitivity : 0.4915
## Specificity : 0.9398
## Pos Pred Value : 0.8788
## Neg Pred Value : 0.6757
## Prevalence : 0.4701
## Detection Rate : 0.2311
## Detection Prevalence : 0.2629
## Balanced Accuracy : 0.7157
##
## 'Positive' Class : no
##
```

The conditional inference tree has an accuracy of 72.91 percent which is significantly better than the base-line accuracy of 52.99 percent. To understand what the other statistics refer to and how they are calculated, run the command `?confusionMatrix`

.

While it is rather straight forward to calculate the Gini values for categorical variables, it may not seem quite as apparent how to calculate splits for numeric or ordinal variables. To illustrate how the algorithm works on such variables, consider the example data set shown below.

```
## Age LikeUser
## 1 15 yes
## 2 37 no
## 3 63 no
## 4 42 yes
## 5 22 yes
## 6 27 yes
```

In a first step, we order the numeric variable so that we arrive at the following table.

```
## Age LikeUser
## 1 15 yes
## 2 22 yes
## 3 27 yes
## 4 37 no
## 5 42 yes
## 6 63 no
```

Next, we calculate the means for each level of “Age”.

```
## Age LikeUser
## 1 15.0 yes
## 2 18.5
## 3 22.0 yes
## 4 24.5
## 5 27.0 yes
## 6 32.0
## 7 37.0 no
## 8 39.5
## 9 42.0 yes
## 10 52.5
## 11 63.0 no
```

Now, we calculate the Gini values for each average level of age. How this is done is shown below for the first split.

\[\begin{equation} G_{x} = 1 - ( p_{1} )^{2} - ( p_{0} )^{2} \end{equation}\]

For an age smaller than 18.5 this would mean:

\[\begin{equation} G_{youngerthan18.5} = 1-(\frac{1} {1+0})^{2} - (\frac{0} {1+0})^{2} = 0.0 \end{equation}\]

For an age greater than 18.5, we calculate G or Gini as follows:

\[\begin{equation} G_{olerthan18.5} = 1-(\frac{2} {2+3})^{2} - (\frac{3} {2+3})^{2} = 0.48 \end{equation}\]

Now, we calculate the Gini for that split as we have done above.

\[\begin{equation} G_{split18.5} = \frac{N_{youngerthan18.5}} {N_{Total}} \times G_{youngerthan18.5} + \frac{N_{olderthan18.5}} {N_{Total}} \times G_{olderthan18.5} G_{split18.5} = \frac{1} {6} \times 0.0 + \frac{5} {6} \times 0.48 = 0.4 \end{equation}\]

We then have to calculate the gini values for all possible age splits which yields the following results:

```
# 18.5
1-(1/(1+0))^2 - (0/(1+0))^2
1-(2/(2+3))^2 - (3/(2+3))^2
1/6 * 0.0 + 5/6 * 0.48
# 24.4
1-(2/(2+0))^2 - (0/(2+0))^2
1-(3/(3+1))^2 - (2/(3+1))^2
2/6 * 0.0 + 4/6 * 0.1875
# 32
1-(3/(3+0))^2 - (0/(3+0))^2
1-(1/(1+2))^2 - (2/(1+2))^2
3/6 * 0.0 + 3/6 * 0.4444444
# 39.5
1-(3/(3+1))^2 - (1/(3+1))^2
1-(1/(1+1))^2 - (1/(1+1))^2
4/6 * 0.375 + 2/6 * 0.5
# 52.5
1-(4/(4+1))^2 - (1/(4+1))^2
1-(0/(0+1))^2 - (1/(0+1))^2
5/6 * 0.32 + 1/6 * 0.0
```

```
## AgeSplit Gini
## 1 18.5 0.400
## 2 24.5 0.500
## 3 32.0 0.444
## 4 39.5 0.410
## 5 52.5 0.267
```

The split at 52.5 years of age has the lowest Gini value. Accordingly, we would split the data between speakers who are younger than 52.5 and speakers who are older than 52.5 years of age. The lowest Gini value for any age split would also be the Gini value that would be compared to other variables.

The same procedure that we have used to determine potential splits for a numeric variable would apply to an ordinal variable with only two differences:

- The Gini values are calculated for the actual levels and not the means between variable levels.
- The Gini value is nor calculated for the lowest and highest level as the calculation of the Gini values is impossible for extreme values. Extreme levels can, therefore, not serve as a potential split location.

When dealing with categorical variables with more than two levels, the situation is slightly more complex as we would also have to calculate the Gini values for combinations of variable levels. While the calculations are, in principle, analogous to the ones performed for binary of nominal categorical variables, we would also have to check if combinations would lead to improved splits. For instance, imagine we have a variable with categories A, B, and C. In such cases we would not only have to calculate the Gini scores for A, B, and C but also for A plus B, A plus C, and B plus C. Note that we ignore the combination A plus B plus C as this combination would include all other potential combinations.

The easiest and most common way to visualize CITs is to simply use the `plot`

function from `base R`

. However, using this function does not allow to adapt and customize the visualization except for some very basic parameters. The `ggparty`

function allows to use the `ggplot`

syntax to customize CITs which allows more adjustments and is more aesthetically pleasing.

To generate this customized CIT, we activate the `ggparty`

package and extract the significant p-values from the CIT object. We then plot the CIT and define the nodes, edges, and text elements as shown below.

```
# activate package
library(ggparty)
# extract p-values
pvals <- unlist(nodeapply(citd.ctree, ids = nodeids(citd.ctree), function(n) info_node(n)$p.value))
pvals <- pvals[pvals <.05]
# plotting
ggparty(citd.ctree) +
geom_edge() +
geom_edge_label() +
geom_node_label(line_list = list(aes(label = splitvar),
aes(label = paste0("N=", nodesize, ", p",
ifelse(pvals < .001, "<.001", paste0("=", round(pvals, 3)))),
size = 10)),
line_gpar = list(list(size = 13),
list(size = 10)),
ids = "inner") +
geom_node_label(aes(label = paste0("Node ", id, ", N = ", nodesize)),
ids = "terminal", nudge_y = -0.0, nudge_x = 0.01) +
geom_node_plot(gglist = list(
geom_bar(aes(x = "", fill = LikeUser),
position = position_fill(), color = "black"),
theme_minimal(),
scale_fill_manual(values = c("gray50", "gray80"), guide = FALSE),
scale_y_continuous(breaks = c(0, 1)),
xlab(""),
ylab("Probability"),
geom_text(aes(x = "", group = LikeUser,
label = stat(count)),
stat = "count", position = position_fill(), vjust = 1.1)),
shared_axis_labels = TRUE)
```

We can also use `position_dodge`

(instead of `position_fill`

) to display frequencies rather than probabilities as shown below.

```
# plotting
ggparty(citd.ctree) +
geom_edge() +
geom_edge_label() +
geom_node_label(line_list = list(aes(label = splitvar),
aes(label = paste0("N=", nodesize, ", p",
ifelse(pvals < .001, "<.001", paste0("=", round(pvals, 3)))),
size = 10)),
line_gpar = list(list(size = 13),
list(size = 10)),
ids = "inner") +
geom_node_label(aes(label = paste0("Node ", id, ", N = ", nodesize)),
ids = "terminal", nudge_y = 0.01, nudge_x = 0.01) +
geom_node_plot(gglist = list(
geom_bar(aes(x = "", fill = LikeUser),
position = position_dodge(), color = "black"),
theme_minimal(),
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()),
scale_fill_manual(values = c("gray50", "gray80"), guide = FALSE),
scale_y_continuous(breaks = seq(0, 100, 20),
limits = c(0, 100)),
xlab(""),
ylab("Frequency"),
geom_text(aes(x = "", group = LikeUser,
label = stat(count)),
stat = "count",
position = position_dodge(0.9), vjust = -0.7)),
shared_axis_labels = TRUE)
```

Like other tree-based methods, CITs are very intuitive, multivariate, non-parametric, they do not require large data sets, and they are easy to implement. Despite these obvious advantages, they have at least one major short coming compared to other, more sophisticated tree-structure models (in addition to the general issues that tree-structure models exhibit as discussed in the section Tree-Structure Model Basics): they are prone to overfitting which means that they fit the observed data very well but preform much worse when being applied to new data.

An extension which remedies this problem is to use a so-called ensemble method which grows many varied trees. The most common ensemble method is called a *Random Forest Analysis* and will have a look at how Random Forests work and how to implement them in R in the next section.

Random Forests (RFs) are an extension of Conditional Inference Trees (Breiman 2001a). Like Conditional Inference Trees, Random Forests represent a multivariate, non-parametric partitioning method that is particularly useful when dealing with relatively small sample sizes and many predictors (including interactions) and they are insensitive to multicollinearity (if two predictors strongly correlate with the dependent variable AND are highly correlated or collinear, RFs will report both variables as highly important - the ordering in which they were included into the model is irrelevant). The latter point is a real advantage over regression models in particular. Also, RFs outperform CITs in that they are substantially less prone to overfitting and they perform much better when applied to new data. However, random forests have several issues:

- RFs only show variable importance but not if the variable is positively or negatively correlated with the dependent variable;
- RFs do not report if a variable is important as a main effect or as part of an interactions
- RFs do not indicate in which significant interactions a variable is involved.

Therefore, Random Forest analyses are ideal for classification, imputing missing values, and - though to a lesser degree - as a variable selection procedure but they do not lend themselves for drawing inferences about the relationships of predictors and dependent variables.

**Bootstrapped Data**

Random Forests do not work on one-and-the-same data set (as CITs do) but in Random Forest analyses, many samples (with replacement) are drawn from the original data set. This generation of new data set based on an existing data set is called “bootstrapping”. Bootstrapping allows us to produce many trees based on variations of the original data set rather than dealing with only a single, fixed data set that would produce only a single tree. Therefore, because the data is different each time, the individual CITs are also different.

Imagine, we are dealing with a very small data set to which we want to apply a Random Forest Analysis. The original data set is displayed below.

We now draw a sample from this data set and receive the following data set.

```
## ID Age Gender Status LikeUser
## 1 6 41-80 male low no
## 2 3 15-40 male high no
## 3 4 41-80 female low yes
## 4 1 15-40 female high no
## 5 2 15-40 female high no
## 6 2 15-40 female high no
```

As you can see, the bootstrapped data contains the second row twice while the fifth row is missing.

Because the data is reshuffled for every new tree, a part of the data (on average about 30%) remains unused for a given tree. The data that is not used is called Out-Of-Bag data or OOB. The OOB is important because the quality of the overall performance of the random forest can be assessed by applying the resulting tree-model to the data that it was not fit to. The quality of that tree is then measured in the OOB error, which is the error rate of the respective tree if applied to the OOB data.

Random Forests also differ from simple CITs in that at each step, not all possible variables are considered for a node, but only a subset. For example, we have a data set with five predicting independent variables and one dependent variable. When generating a CIT, all possible variables (variables that do not represent a node further up in the tree) are considered as splitting candidates. In Random Forests, only a fixed number (typically the square-root of the number of independent variables) are considered as candidates for a node. So, at each potential split, a fixed number of randomly selected variables is considered potential node candidates.

This section shows how a Random Forest Analysis can be implemented in R. Ina first step, we load and inspect the data.

```
# load random forest data
rfdata <- read.delim("https://raw.githubusercontent.com/MartinSchweinberger/coedlss2019materials/master/datatables/rfdata.txt", header = T, sep = "\t")
# inspect data
datatable(rfdata, rownames = FALSE, options = list(pageLength = 5, scrollX=T), filter = "none")
```

The data consists of four categorical variables (*Gender*, *Age*, *ConversationType*, and *SUFLike*). Our dependent variable is SUFLike which stands for speech-unit final like (a pragmatic marker that is common in Irish English and is used as in *A wee girl of her age, like*). While Age and Gender are pretty straight forward what they are called, *ConversationType* encodes whether a conversation has taken place between interlocutors of the same or of different genders.

Before going any further, we need to factorize the variables as tree-based models require factors instead of character variables (but they can, of course, handle numeric and ordinal variables). In addition, we will check if the data contains missing values (NAs).

```
# factorize variables (rf require factors instead of character vectors)
fcts <- c("Gender", "Age", "ConversationType", "Priming", "SUFLike")
rfdata[fcts] <- lapply(rfdata[fcts], factor)
# inspect data
head(rfdata, 10)
```

```
## Gender Age ConversationType Priming SUFLike
## 1 Men Old MixedGender NoPrime no
## 2 Men Old MixedGender NoPrime no
## 3 Men Old MixedGender NoPrime yes
## 4 Men Old MixedGender NoPrime no
## 5 Men Old MixedGender NoPrime yes
## 6 Men Old MixedGender NoPrime no
## 7 Men Old MixedGender NoPrime no
## 8 Men Old MixedGender NoPrime no
## 9 Men Old MixedGender NoPrime no
## 10 Men Old MixedGender NoPrime no
```

We now check if the data contains missing values and remove those (if necessary).

```
# check for NAs
natest <- rfdata %>%
na.omit()
nrow(natest) # no NAs present in data (same number of rows with NAs omitted)
```

`## [1] 10170`

In our case, the data does not contain missing values. Random Forests offer a very nice way to deal with missing data though. If NAs are present, they can either be deleted OR their values for any missing values can be imputed using proximities. In this way, such data points do not have to be removed which can be problematic especially when dealing with relatively small data sets. For imputing values, you could run the code below but as our data does not have NAs, we will skip this step and just show it here so you can have a look at how it is done.

```
# replacing NAs with estimates
data.imputed <- rfImpute(SUFLike ~ ., data = rfdata, iter=6)
```

The argument `iter`

refers to the number of iterations to run. According to (Breiman 2001a), 4 to 6 iterations is usually good enough. With this data set (if it had NAs) and when we were to execute the code, the resulting OOB-error rates lie somewhere around 17 and 18 percent. When we were to set `iter`

to 20, we get values a little better and a little worse, so doing more iterations doesn’t improve the situation.

Also, if you want to customize the `rfImpute`

function, you can change the number of trees it uses (the default is 300) and the number of variables that it will consider at each step.

We will now generate a first random forest object and inspect its model fit. As random forests rely on re-sampling, we set a seed so that we arrive at the same estimations.

```
# set.seed
set.seed(2019120204)
# create initial model
rfmodel1 <- cforest(SUFLike ~ ., data = rfdata,
controls = cforest_unbiased(ntree = 50, mtry = 3))
# evaluate random forest (model diagnostics)
rfmodel1_pred <- unlist(treeresponse(rfmodel1))[c(FALSE,TRUE)]
somers2(rfmodel1_pred, as.numeric(rfdata$SUFLike) - 1)
```

```
## C Dxy n Missing
## 0.8299474 0.6598948 10170.0000000 0.0000000
```

The model parameters are excellent: remember that if the C-value is 0.5, the predictions are random, while the predictions are perfect if the C-value is 1. C-values above 0.8 indicate real predictive capacity (Baayen 2008, 204). Somers’ D_{xy} is a value that represents a rank correlation between predicted probabilities and observed responses. Somers’ D_{xy} values range between 0, which indicates complete randomness, and 1, which indicates perfect prediction (Baayen 2008, 204).

In a next step, we extract the variable importance (conditional=T adjusts for correlations between predictors).

```
# extract variable importance based on mean decrease in accuracy
rfmodel1_varimp <- varimp(rfmodel1, conditional = T)
# show variable importance
rfmodel1_varimp
```

```
## Gender Age ConversationType Priming
## 0.0060288616 0.0603527525 0.0091394976 0.0002244789
```

We can also calculate more robust variable importance using the `varimpAUC`

function from the `party`

package which calculates importance statistics that are corrected towards class imbalance, i.e. differences in the number of instances per category. The variable importance is easily visualized using the `dotplot`

function from base R.

```
# extract more robust variable importance
rfmodel1_robustvarimp <- party::varimp(rfmodel1)
# plot result
dotchart(sort(rfmodel1_robustvarimp), pch = 20, main = "Conditional importance of variables")
```

The plot shows that Age is the most important predictor and that Priming is not really important as a predictor for speech-unit final like. Gender and ConversationType are equally important but both much less so than Age.

We will now use an alternative way to calculate RFs which allows us to use different diagnostics and pruning techniques by using the `randomForest`

rather than the `cforest`

function.

A few words on the parameters of the `randomForest`

function: if the thing we’re trying to predict is a numeric variable, the `randomForest`

function will set `mtry`

(the number of variables considered at each step)to the total number of variables divided by 3 (rounded down), or to 1 if the division results in a value less than 1. If the thing we’re trying to predict is a “factor” (i.e. either “yes/no” or “ranked”), then `randomForest()`

will set `mtry`

to the square root of the number of variables (rounded down to the next integer value).Again, we start by setting a seed to store random numbers and thus make results reproducible.

```
# set.seed
set.seed(2019120205)
rfmodel2 <- randomForest(SUFLike ~ ., data=rfdata, proximity=TRUE)
# inspect model
rfmodel2
```

```
##
## Call:
## randomForest(formula = SUFLike ~ ., data = rfdata, proximity = TRUE)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 2
##
## OOB estimate of error rate: 17.5%
## Confusion matrix:
## no yes class.error
## no 821 1398 0.63001352
## yes 382 7569 0.04804427
```

The output tells us that the OOB error rate is 17.5 percent and produces a confusion matrix. We can now check the development of the OOB error rate to see if it would suffice to use fewer trees (which would be quicker and save computing time).

```
# lower number of trees makes the computation much quicker
oob.error.data <- data.frame(
Trees=rep(1:nrow(rfmodel2$err.rate), times=3),
Type=rep(c("OOB", "no", "yes"), each=nrow(rfmodel2$err.rate)),
Error=c(rfmodel2$err.rate[,"OOB"],
rfmodel2$err.rate[,"no"],
rfmodel2$err.rate[,"yes"]))
# plot error rates
ggplot(data=oob.error.data, aes(x=Trees, y=Error)) +
geom_line(aes(color=Type))
```

The output shows that the error rate is extremely stable and that we only need very few trees as the prediction accuracy does not improve if we grow more than 30 trees.

In addition to the number of trees, we can check if the model improves if we change the number of variables that are considered at each step (`mtry`

).

```
# check what mtry is optimal
tuneRF(rfdata[, !colnames(rfdata)== "SUFLike"],
rfdata[, colnames(rfdata)== "SUFLike"],
stepFactor = 2, # 2 has the lowest value to be optimal
plot = T, ntreeTry = 50, trace = T, improve = .05)
```

```
## mtry = 2 OOB error = 17.6%
## Searching left ...
## mtry = 1 OOB error = 18.15%
## -0.03128492 0.05
## Searching right ...
## mtry = 4 OOB error = 17.5%
## 0.005586592 0.05
```

```
## mtry OOBError
## 1.OOB 1 0.1815143
## 2.OOB 2 0.1760079
## 4.OOB 4 0.1750246
```

The error rate is very similar across different values of `mtry`

but the lowest values is reported if all 4 variables are considered at once. We can now create a pruned or optimized RF based on the previous recommendations (ntree = 30, mtry = 4). Again, we begin by setting a seed.

```
# set.seed (to store random numbers and thus make results reproducible)
set.seed(2019120206)
# create a new model with fewer trees and that takes 2 variables at a time
rfmodel3 <- randomForest(SUFLike ~ ., data=rfdata, ntree=30, mtry = 4, proximity=TRUE)
# inspect model
rfmodel3
```

```
##
## Call:
## randomForest(formula = SUFLike ~ ., data = rfdata, ntree = 30, mtry = 4, proximity = TRUE)
## Type of random forest: classification
## Number of trees: 30
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 17.5%
## Confusion matrix:
## no yes class.error
## no 821 1398 0.63001352
## yes 382 7569 0.04804427
```

Despite optimization, the results have not changed but it may be very useful for other data. To evaluate the tree, we create a confusion matrix.

```
# save what the model predicted in a new variable
rfdata$Prediction <- predict(rfmodel3, rfdata)
# create confusion matrix to check accuracy
confusionMatrix(rfdata$Prediction, rfdata$SUFLike)
```

```
## Confusion Matrix and Statistics
##
## Reference
## Prediction no yes
## no 821 382
## yes 1398 7569
##
## Accuracy : 0.825
## 95% CI : (0.8174, 0.8323)
## No Information Rate : 0.7818
## P-Value [Acc > NIR] : < 0.00000000000000022
##
## Kappa : 0.3856
##
## Mcnemar's Test P-Value : < 0.00000000000000022
##
## Sensitivity : 0.36999
## Specificity : 0.95196
## Pos Pred Value : 0.68246
## Neg Pred Value : 0.84410
## Prevalence : 0.21819
## Detection Rate : 0.08073
## Detection Prevalence : 0.11829
## Balanced Accuracy : 0.66097
##
## 'Positive' Class : no
##
```

The RF performs significantly better than a no-information base-line model but the base-line model already predicts 78.18 percent of cases correctly (compared to the RF with a prediction accuracy of 82.5 percent).

Unfortunately, we cannot easily compute robust variable importance for RF models nor C or Somers’ D_{xy} which is why it is advisable to create analogous models using both the `cforest`

and the `randomForest`

functions. In a last step, we can now visualize the results of the optimized RF.

```
# plot variable importance
varImpPlot(rfmodel3, main = "", pch = 20)
```

A second type of visualization that can provide insights in the `partialPlot`

function which shows the effects of individual predictors - but remember that this still does not provide information about the way that the predictor interacts with other predictors.

```
# extract importance of individual variables
partialPlot(rfmodel3, rfdata, Age)
```

Another common way to evaluate the performance of RFs and prune them is to split the data into a test and a training set. the model is then fit to the training set and, after that, applied to the test set. This allows us to evaluate how well the RF performs on data that it was not trained on. This approach is particularly common in machine learning contexts.

Boruta (Kursa, Rudnicki, and others 2010) is a variable selection procedure and it represents an extension of random forest analyses (Breiman 2001a). The name *Boruta* is derived from a demon in Slavic mythology who dwelled in pine forests. Boruta is an alternative to regression modeling that is better equipped to handle small data sets because it uses a distributional approach during which hundreds of (random) forests are grown from permuted data sets. Boruta outperforms random forest analyses because:

Boruta does not provide merely a single value for each predictor but a distribution of values leading to higher reliability.

Boruta provides definitive cut-off points for variables that have no meaningful relationship with the dependent variable. This is a crucial difference between RF and Boruta that make Boruta particularly interesting from a variable selection point of view.

The Boruta procedure consists out of five steps.

In a first step, the Boruta algorithm copies the data set and adds randomness to the data by (re-)shuffling data points and thereby creating randomized variables. These randomized variables are referred to as shadow features.

Secondly, a random forest classifier is trained on the extended data set.

In a third step, a feature importance measure (Mean Decrease Accuracy represented by z-scores) is calculated to determine the relative importance of all predictors (both original or real variables and the randomized shadow features).

In the next step, it is checked at each iteration of the process whether a real predictor has a higher importance compared with the best shadow feature. The algorithm keeps track of the performance of the original variables by storing whether they outperformed the best shadow feature or not in a vector.

In the fifth step, predictors that did not outperform the best shadow feature are removed and the process continues without them. After a set number of iterations, or if all the variables have been either confirmed as outperforming the best shadow feature, the algorithm stops.

Despite its obvious advantages of Boruta over random forest analyses and regression modeling, it can neither handle multicollinearity not hierarchical data structures where data points are nested or grouped by a given predictor (as is the case in the present analysis as data points are grouped by adjective type). As Boruta is a variable selection procedure, it is also limited in the sense that it provides information on which predictors to include and how good these predictors are (compared to the shadow variables) while it is neither able to take hierarchical data structure into account, nor does it provide information about how one level of a factor compares to other factors. In other words, Boruta shows that a predictor is relevant and how strong it is but it does not provide information on how the likelihood of an outcome being used differs between variable levels, for instance between men and women.

We begin by loading and inspecting the data.

```
# load data
borutadata <- read.delim("https://raw.githubusercontent.com/MartinSchweinberger/coedlss2019materials/master/datatables/borutadata.txt", header = T, sep = "\t")
# inspect data
datatable(borutadata, rownames = FALSE, options = list(pageLength = 5, scrollX=T), filter = "none")
```

As the data contains non-factorized character variables, we convert those into factors.

```
# factorize variables (boruta - like rf - require factors instead of character vectors)
fcts <- c("Age", "Adjective", "Function", "Priming", "Gender", "Occupation",
"ConversationType", "AudienceSize", "really", "Gradabilty", "SemanticCategory")
borutadata[fcts] <- lapply(borutadata[fcts], factor)
# inspect data
head(borutadata, 10)
```

```
## Age Adjective Function Priming Gender Occupation
## 1 26-40 good Attributive NoPrime Male AcademicManagerialProfessionals
## 2 26-40 good Attributive NoPrime Male AcademicManagerialProfessionals
## 3 26-40 good Predicative NoPrime Male AcademicManagerialProfessionals
## 4 17-25 nice Attributive NoPrime Male AcademicManagerialProfessionals
## 5 41-80 other Predicative NoPrime Male AcademicManagerialProfessionals
## 6 41-80 good Attributive NoPrime Female AcademicManagerialProfessionals
## 7 17-25 other Attributive NoPrime Male AcademicManagerialProfessionals
## 8 17-25 other Attributive Prime Male AcademicManagerialProfessionals
## 9 17-25 other Predicative Prime Male AcademicManagerialProfessionals
## 10 17-25 nice Attributive NoPrime Female AcademicManagerialProfessionals
## ConversationType AudienceSize really Frequency Gradabilty
## 1 SameSex 3+ 0 27.84810 NotGradable
## 2 SameSex 3+ 0 27.84810 NotGradable
## 3 SameSex 3+ 0 27.84810 NotGradable
## 4 SameSex 2 0 7.29282 NotGradable
## 5 SameSex 2 0 0.61728 NotGradable
## 6 MixedSex 3+ 0 20.98765 NotGradable
## 7 MixedSex 3+ 1 4.64088 GradabilityUndetermined
## 8 MixedSex 3+ 1 0.44199 NotGradable
## 9 MixedSex 3+ 1 0.44199 NotGradable
## 10 SameSex 2 1 7.29282 NotGradable
## SemanticCategory
## 1 Value
## 2 Value
## 3 Value
## 4 HumanPropensity
## 5 Value
## 6 Value
## 7 Dimension
## 8 PhysicalProperty
## 9 PhysicalProperty
## 10 HumanPropensity
```

We can now create our initial Boruta model and set a seed for reproducibility.

```
# set.seed
set.seed(2019120207)
# initial run
boruta1 <- Boruta(really~.,data=borutadata)
print(boruta1)
```

```
## Boruta performed 99 iterations in 5.11366 secs.
## 2 attributes confirmed important: Adjective, Frequency;
## 4 attributes confirmed unimportant: Age, Occupation, Priming,
## SemanticCategory;
## 5 tentative attributes left: AudienceSize, ConversationType, Function,
## Gender, Gradabilty;
```

```
# extract decision
getConfirmedFormula(boruta1)
```

```
## really ~ Adjective + Frequency
## <environment: 0x000000003fc2a858>
```

In a next step, we inspect the history to check if any of the variables shows drastic fluctuations in their importance assessment.

`plotImpHistory(boruta1)`

The fluctuations are do not show clear upward or downward trends (which what we want). If predictors do perform worse than the shadow variables, then these variables should be excluded and the Boruta analysis should be re-run on the data set that does no longer contain the superfluous variables. Tentative variables can remain but they are unlikely to have any substantial effect. We thus continue by removing variables that were confirmed as being unimportant, then setting a new seed, re-running the Boruta on the reduced data set, and again inspecting the decisions.

```
# remove irrelevant variables
rejected <- names(boruta1$finalDecision)[which(boruta1$finalDecision == "Rejected")]
# update data for boruta
borutadata <- borutadata %>%
dplyr::select(-rejected)
# set.seed (to store random numbers and thus make results reproducible)
set.seed(2019120208)
# 2nd run
boruta2 <- Boruta(really~.,data=borutadata)
print(boruta2)
```

```
## Boruta performed 99 iterations in 4.75849 secs.
## 4 attributes confirmed important: Adjective, AudienceSize, Frequency,
## Gradabilty;
## No attributes deemed unimportant.
## 3 tentative attributes left: ConversationType, Function, Gender;
```

```
# extract decision
getConfirmedFormula(boruta2)
```

```
## really ~ Adjective + AudienceSize + Frequency + Gradabilty
## <environment: 0x00000000405f7078>
```

Only adjective frequency and adjective type are confirmed as being important while all other variables are considered tentative. However, no more variables need to be removed as all remaining variables are not considered unimportant. In a last step, we visualize the results of the Boruta analysis.

```
library(ggplot2)
library(tidyr)
library(stringr)
borutadf <- as.data.frame(boruta2$ImpHistory) %>%
gather(Variable, Importance, Adjective:shadowMin) %>%
mutate(Type = ifelse(str_detect(Variable, "shadow"), "Control", "Predictor")) %>%
mutate(Type = factor(Type),
Variable = factor(Variable))
ggplot(borutadf, aes(x = reorder(Variable, Importance, mean), y = Importance, fill = Type)) +
geom_boxplot() +
geom_vline(xintercept=3.5, linetype="dashed", color = "black") +
scale_fill_manual(values = c("gray80", "gray40")) +
theme_bw() +
theme(legend.position = "top",
axis.text.x = element_text(angle=90)) +
labs(x = "Variable")
```

Of the remaining variables, adjective frequency and adjective type have the strongest effect and are confirmed as being important while syntactic function fails to perform better than the best shadow variable. All other variables have only a marginal effect on the use of really as an adjective amplifier.

Schweinberger, Martin. 2021. *Tree-Based Models in R*. Brisbane: The University of Queensland. url: https://slcladal.github.io/tree.html (Version 2021.01.22).

```
@manual{schweinberger2021tree,
author = {Schweinberger, Martin},
title = {Tree-Based Models in R},
note = {https://slcladal.github.io/tree.html},
year = {2020},
organization = "The University of Queensland, Australia. School of Languages and Cultures},
address = {Brisbane},
edition = {2021.01.22}
}
```

`sessionInfo()`

```
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19041)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
## [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
## [5] LC_TIME=German_Germany.1252
##
## attached base packages:
## [1] stats4 grid stats graphics grDevices utils datasets
## [8] methods base
##
## other attached packages:
## [1] ggparty_1.0.0 RCurl_1.98-1.2 Boruta_7.0.0
## [4] Hmisc_4.4-1 Formula_1.2-4 survival_3.2-7
## [7] party_1.3-5 strucchange_1.5-2 sandwich_3.0-0
## [10] zoo_1.8-8 modeltools_0.2-23 randomForest_4.6-14
## [13] cowplot_1.1.0 Gmisc_1.11.0 htmlTable_2.1.0
## [16] Rcpp_1.0.5 flextable_0.5.11 caret_6.0-86
## [19] lattice_0.20-41 DT_0.16 forcats_0.5.0
## [22] stringr_1.4.0 dplyr_1.0.2 purrr_0.3.4
## [25] readr_1.4.0 tidyr_1.1.2 tibble_3.0.4
## [28] ggplot2_3.3.2 tidyverse_1.3.0 partykit_1.2-10
## [31] mvtnorm_1.1-1 libcoin_1.0-6
##
## loaded via a namespace (and not attached):
## [1] TH.data_1.0-10 colorspace_1.4-1 ellipsis_0.3.1
## [4] class_7.3-17 base64enc_0.1-3 fs_1.5.0
## [7] rstudioapi_0.11 farver_2.0.3 prodlim_2019.11.13
## [10] fansi_0.4.1 lubridate_1.7.9 ranger_0.12.1
## [13] coin_1.3-1 xml2_1.3.2 codetools_0.2-16
## [16] splines_4.0.3 knitr_1.30 jsonlite_1.7.1
## [19] pROC_1.16.2 broom_0.7.2 cluster_2.1.0
## [22] dbplyr_2.0.0 png_0.1-7 compiler_4.0.3
## [25] httr_1.4.2 backports_1.1.10 assertthat_0.2.1
## [28] Matrix_1.2-18 cli_2.1.0 htmltools_0.5.0
## [31] tools_4.0.3 gtable_0.3.0 glue_1.4.2
## [34] reshape2_1.4.4 cellranger_1.1.0 vctrs_0.3.4
## [37] nlme_3.1-149 iterators_1.0.13 crosstalk_1.1.0.1
## [40] timeDate_3043.102 inum_1.0-1 gower_0.2.2
## [43] xfun_0.19 rvest_0.3.6 lifecycle_0.2.0
## [46] XML_3.99-0.5 MASS_7.3-53 scales_1.1.1
## [49] ipred_0.9-9 hms_0.5.3 parallel_4.0.3
## [52] RColorBrewer_1.1-2 yaml_2.2.1 gridExtra_2.3
## [55] gdtools_0.2.2 rpart_4.1-15 latticeExtra_0.6-29
## [58] stringi_1.5.3 foreach_1.5.1 e1071_1.7-4
## [61] checkmate_2.0.0 zip_2.1.1 lava_1.6.8.1
## [64] bitops_1.0-6 matrixStats_0.57.0 rlang_0.4.8
## [67] pkgconfig_2.0.3 systemfonts_0.3.2 evaluate_0.14
## [70] labeling_0.4.2 recipes_0.1.14 htmlwidgets_1.5.2
## [73] tidyselect_1.1.0 plyr_1.8.6 magrittr_1.5
## [76] R6_2.5.0 generics_0.1.0 multcomp_1.4-14
## [79] DBI_1.1.0 foreign_0.8-80 pillar_1.4.6
## [82] haven_2.3.1 withr_2.3.0 forestplot_1.10
## [85] abind_1.4-5 nnet_7.3-14 modelr_0.1.8
## [88] crayon_1.3.4 uuid_0.1-4 rmarkdown_2.5
## [91] officer_0.3.15 jpeg_0.1-8.1 readxl_1.3.1
## [94] data.table_1.13.2 ModelMetrics_1.2.2.2 reprex_0.3.0
## [97] digest_0.6.27 munsell_0.5.0
```

Baayen, R Harald. 2008. *Analyzing Linguistic Data. A Practical Introduction to Statistics Using R*. Cambridge: Cambridge University press.

Boulesteix, Anne-Laure, Silke Janitza, Alexander Hapfelmeier, Kristel Van Steen, and Carolin Strobl. 2015. “Letter to the Editor: On the Term ‘Interaction’ and Related Phrases in the Literature on Randomforests.” *Briefings in Bioinformatics* 16 (2): 338–45. https://academic.oup.com/bib/article/16/2/338/246566.

Breiman, Leo. 2001a. “Random Forests.” *Machine Learning* 45 (1): 5–32.

———. 2001b. “Statistical Modeling: The Two Cultures.” *Statistical Science* 16: 199–231. https://projecteuclid.org/euclid.ss/1009213726.

Gries, Stefan Th. 2021. *Statistics for Linguistics Using R: A Practical Introduction*. Berlin & New York: Mouton de Gruyter.

Kursa, Miron B, Witold R Rudnicki, and others. 2010. “Feature Selection with the Boruta Package.” *J Stat Softw* 36 (11): 1–13.

Prasad, Anantha M, Louis R Iverson, and Andy Liaw. 2006. “Newer Classification and Regression Tree Techniques: Bagging and Random Forests for Ecological Prediction.” *Ecosystems* 9 (2): 181–99.

Strobl, Carolin, James Malley, and Gerhard Tutz. 2009. “An Introduction to Recursive Partitioning: Rationale,application and Characteristics of Classification and Regression Trees, Bagging and Random Forests.” *Psychological Methods* 14 (4): 323–48. https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2927982/.

Wright, Marvin N., Andreas Ziegler, and Inke R. König. 2016. “Do Little Interactions Get Lost in Dark Random Forests?” 17 (145). https://bmcbioinformatics.biomedcentral.com/articles/10.1186/s12859-016-0995-8.