Load the Tidyverse package and the Party package.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(party)
## Loading required package: grid
## Loading required package: mvtnorm
## Loading required package: modeltools
## Loading required package: stats4
## Loading required package: strucchange
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: sandwich
## 
## Attaching package: 'strucchange'
## 
## The following object is masked from 'package:stringr':
## 
##     boundary
## 
## 
## Attaching package: 'party'
## 
## The following object is masked from 'package:dplyr':
## 
##     where

Load the Source-cases dataset

source <- read.csv('../data/Source_LM.csv', header = TRUE, sep = ";")

Select the columns relevant for the analysis of the effect of the type of the Landmark (LM) to the choice of source case. Excluded columns include the whole LM phrase, the referent of the LM, and the translation of the LM phrase, as well as the source of the example.

source <- select(source, Case, LM_word_class, LM_type)

Change the variables from character to factorial.

source <- mutate(source, Case = factor(Case), LM_word_class = factor(LM_word_class), LM_type = factor(LM_type))

Set the parameters for calculating a random forest. mtry is set to 2 as it is the closest integer to the square root of the number of predictor variables (~1.7). minsplit is set to 3 as the dataset is quite small. This should yield large trees.

gfcontrols <- cforest_unbiased(ntree=1000, mtry=2, minsplit=3)

Set an arbitrary seed and calculate the trees.

set.seed(1969)
forest_source <- cforest(Case ~ LM_word_class + LM_type, data=source, controls=gfcontrols)

Check the model fit.

table(predict(forest_source), source$Case)
##            
##             egressive elative
##   egressive       393     161
##   elative         107     339

Measure the variable of importance

varimp_source <- varimp(forest_source, conditional = FALSE)
round(varimp_source, 3)
## LM_word_class       LM_type 
##         0.010         0.218

Plot the variance of importance of the predictor variables.

dotchart(sort(varimp_source), main = "Conditional importance of variables")

Plot the best single conditional inference tree.