This is a new implementation of the TraitMatching (see https://github.com/TheoreticalEcology/Pichler-et-al-2019 ) package based on mlr3.
More about using machine learning for TraitMatching: https://doi.org/10.1111/2041-210X.13329
To install the package:
devtools::install_github(repo = "https://github.com/MaximilianPi/TraitMatching", subdir = "TraitMatching")
library(TraitMatching)
- simulate data (replace the A, B, and Z matrices with your own):
library(TraitMatching)
## Warning: replacing previous import 'mlr3measures::tnr' by 'mlr3tuning::tnr' when
## loading 'TraitMatching'
sim = simulateInteraction(weights = list(main = 0.1, inter = c(0.3,0.3,0.3)))
A = sim$A
B = sim$B
Z = sim$binar()
community = createCommunity(A, B, Z)
## missForest iteration 1 in progress...done!
## missForest iteration 2 in progress...done!
## missForest iteration 1 in progress...done!
## missForest iteration 2 in progress...done!
- Fit random forest in a nested resampling strategy and with hyper-parameter tuning:
result = runTM(community = community, method = "RF", iters = 20L)
print(result)
## nr resample_result task_id learner_id resampling_id
## 1: 1 <ResampleResult[21]> classif_community classif.ranger.tuned custom
## iters classif.auc
## 1: 10 0.705656
- Compare different ML algorithms:
result = runTM(community = community, method = c("RF", "SVM"), iters = 3L)
print(result)
## nr resample_result task_id learner_id resampling_id
## 1: 1 <ResampleResult[21]> classif_community classif.ranger.tuned custom
## 2: 2 <ResampleResult[21]> classif_community classif.svm.tuned custom
## iters classif.auc
## 1: 10 0.7127029
## 2: 10 0.5529854
- Predict target for new data with different ensemble ML algorithms (trained with optimal hyper-parameters)
predict(result, newdata=community$data[1:5,])
## RF SVM
## 1 0.8869743 0.7538086
## 2 0.9247219 0.8050551
## 3 0.5959860 0.5702086
## 4 0.7903801 0.5801098
## 5 0.4514330 0.3647977