Boosted+Regression+Trees

= =  ﻿﻿ **Applications: ** Data mining with large datasets and relatively large number of candidate predictor variables. Spatial predictions with raster file format.

**Assumptions: ** No statistical assumptions exist with this data mining method. The user must identify appropriate algorithm parameters regarding tree complexity and regularization (Ridgeway 2007; Hastie et al. 2009). If collinearity among predictor variables exists, then the order of variables entering the model may affect model outcome leading to bias in relative importance among predictor variables.

**Package: ** gbm (1.6-3.1) **Calls: ** predict.gbm; required package for Elith et al. (2008) code **Package: ** additional code of Elith et al. (2008) available here in a zip file **Calls: **<span style="font-family: Arial,Helvetica,sans-serif;"> gbm.step, gbm.simplify + gbm.interactions, gbm.plot, gbm.perspec <span style="font-family: arial,helvetica,sans-serif; font-size: 13px; line-height: 19px; margin: 0px; padding: 0px;">**<span style="font-family: Arial,Helvetica,sans-serif; font-size: 14px;">Calls: **<span style="font-family: Arial,Helvetica,sans-serif;"> prediction, performance, plot
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 20px;">Program: ** <span style="font-family: Arial,Helvetica,sans-serif; font-size: 16px;">R (2.11.1)
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 16px;">Package: ** <span style="font-family: Arial,Helvetica,sans-serif; font-size: 14px;">ROCR (1.0-4)

<span style="font-family: Arial,Helvetica,sans-serif;"># Data must be in tab delimited text file to follow this routine. <span style="font-family: Arial,Helvetica,sans-serif;"># All data must be in numeric format, i.e. no alpha characters <span style="font-family: Arial,Helvetica,sans-serif;"># Import data to R //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><- read.table(" <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">C:/<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">your_folder_path // / //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">filename //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">.txt", header = TRUE, sep = "\t") <span style="font-family: Arial,Helvetica,sans-serif;"># Assign data class if non-numeric <span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name$header //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><- as.factor( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name$header //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">) <span style="font-family: Arial,Helvetica,sans-serif;"># Check that data classes are correct <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">sapply( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">,class) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Review imported dataframe for accuracy <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">summary( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">) <span style="font-family: Arial,Helvetica,sans-serif;"># Set working directory to that containing extracted contents of Elith BRT zip folder <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">setwd("C:/<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">your_folder_path // ") <span style="font-family: Arial,Helvetica,sans-serif;"># Import functions <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">library(gbm) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">source("brt.functions.R") <span style="font-family: Arial,Helvetica,sans-serif;">## <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">Loads required packages: gbm 1.6-3.1, survival, splines, lattice <span style="font-family: Arial,Helvetica,sans-serif;">## <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"> GBM STEP - version 2.9 <span style="font-family: Arial,Helvetica,sans-serif;"># Determine column order for next step - the code requires using data column locations rather than header names <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">names(// data.name //)
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 16px;">Generic Implementation: **

<span style="color: #000000; font-family: 'Courier New',Courier,monospace;"># <span style="font-family: Arial,Helvetica,sans-serif;">Construct the BRT model <span style="font-family: Arial,Helvetica,sans-serif;"># This call automatically plots the holdout deviance with standard error used in selecting the optimal number of trees following Hastie et al. (2009) //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><-gbm.step(data= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, gbm.x=c( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">predictor_column_numbers //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">), gbm.y= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response_column_number //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, family=" //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">family //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">", tree.complexity= <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"># <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, learning.rate= <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#.# <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, bag.fraction= <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#.# <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, site.weights= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name$weights //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">) <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## <span style="font-family: 'Courier New',Courier,monospace; line-height: 19px;">for <span style="font-family: 'Courier New',Courier,monospace; line-height: normal; margin-bottom: 0cm;">// response // with dataframe // data.name // and using a family of // family // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## Using //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#### // observations and //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># ////<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># // predictors <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## creating 10 initial models of 50 trees <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## folds are stratified by prevalence <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## total mean deviance = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## tolerance is fixed at 9e-04 <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## ntrees resid. dev. <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## 50 //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## 100 //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## ... //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## fitting final gbm model with a fixed number of //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># ////<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># ////<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># ////<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># // trees for //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## mean total deviance = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## mean residual deviance = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## estimated cv deviance = <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // ; se = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## training data correlation = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## cv correlation = <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // ; se = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## training data ROC score = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## cv ROC score = <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // ; se = //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.### // <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">## elapsed time - <span style="font-family: arial,helvetica,sans-serif; line-height: 19px;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">###.## // minutes <span style="font-family: Arial,Helvetica,sans-serif;"># Determine relative influence of predictor variables <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // $contributions
 * 1) Performing cross-validation optimisation of a boosted regression tree model

<span style="font-family: Arial,Helvetica,sans-serif;"># Assess magnitude of 2nd order interactions <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">int.model // <-gbm.interactions(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ) <span style="color: #000000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="font-family: arial,helvetica,sans-serif;">## gbm.interactions - version 2.9 <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: 'Courier New',Courier,monospace; font-style: normal;"><span style="font-family: arial,helvetica,sans-serif;">## Cross tabulating interactions for gbm model with // <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">## predictors <span style="font-family: Arial,Helvetica,sans-serif;"># View list of top 10 interaction effects <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">int.model // $rank.list <span style="font-family: 'Courier New',Courier,monospace;">## var1.index var1.names var2.index var2.names int.size <span style="font-family: 'Courier New',Courier,monospace;">## 1 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 2 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 3 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 4 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 5 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 6 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 7 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 8 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 9 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: 'Courier New',Courier,monospace;">## 10 <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"># pred._ # pred._ ###.## <span style="font-family: Arial,Helvetica,sans-serif;"># View full matrix of interaction effects <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">int.model // $interactions <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // pred.1 pred.2 ... <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // pred.1 0 #.## #.## <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // pred.2 0 0 #.## <span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // ... 0 0 ...

<span style="font-family: Arial,Helvetica,sans-serif;"># Plot fitted functions of multiple predictor variables holding all other variables constant <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">gbm.plot(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //, n.plots=<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">## // , write.title=F) <span style="font-family: Arial,Helvetica,sans-serif;"># Plot fitted function of individual predictor variable <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">par(mfrow=c(1,1)) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">plot(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //, <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">predictor_number // , ...)

<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">gbm.perspec(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //, <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">first_predictor_number // , <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">second_predictor_number // , ...)
 * 1) Plot 2nd order interactions among predictor variables holding all other variables constant
 * 2) Note that this function only works for ratio scale input data

<span style="font-family: Arial,Helvetica,sans-serif;"># [optional] Simplify the list of predictors within the BRT model by determining which predictors can be removed without affecting the model deviance. <span style="font-family: Arial,Helvetica,sans-serif;"># This procedure should only be implemented if you have reason to use less than the full set of candidate variables. //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.simp //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><-gbm.simplify( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">,n.drops=<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"># // ) <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // gbm.simplify - version 2.9 <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // simplifying gbm.step model for Use with 14 predictors and 92364 observations <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // original deviance = <span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // (9e-04) <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // a fixed number of <span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"># // drops will be tested <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // creating initial models... <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // dropping predictor 1 ... dropping predictor //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"># // <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // now processing final dropping of variables with full data <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // 1 - //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">pred.1 // <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // 2 - //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">pred.2 // <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"><span style="font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // ... <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"> <span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // # <span style="font-family: 'Courier New',Courier,monospace;"> - <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">pred. // //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"># // <span style="font-family: Arial,Helvetica,sans-serif;"># Construct the simplified BRT model //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">simp.model.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><-gbm.step(data= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, gbm.x=<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.simp // $pred.list<span style="color:, gbm.y= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response_column_number //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, family=" //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">family //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">", tree.complexity= <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"># <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, learning.rate= <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#.# <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, bag.fraction= <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#.# <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, site.weights= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name$weights //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">) <span style="font-family: Arial,Helvetica,sans-serif;"># Review model output using the same steps as in the original model above

<span style="font-family: Arial,Helvetica,sans-serif;"># Generate spatial predictions of BRT model results <span style="font-family: Arial,Helvetica,sans-serif;"># Export raster layers as ASCII files from GIS software <span style="font-family: Arial,Helvetica,sans-serif;"># IMPORTANT: All predictor variables in the model must be represented by a raster file for the prediction function to work, <span style="font-family: Arial,Helvetica,sans-serif;"># this may require creating "dummy" layers for variables that are not rasterized already <span style="font-family: Arial,Helvetica,sans-serif;"># IMPORTANT: All imported raster ASCII files must be EXACTLY the same size for this function to work <span style="font-family: Arial,Helvetica,sans-serif;"># Call predictor names to ensure order and associate each with a raster ASCII file <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // $var.names <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">var.names<-c(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // $var.names) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Change the working directory to the location of stored raster ASCII files <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">setwd("C:/<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">your_folder_path // ") <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Import raster ASCII files into R <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">grid.names<-c("<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">pred_1 // .asc", "<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">pred_2 // .asc", "<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">pred_3 // .asc", ...) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">for(i in 1:length(grid.names)){assign(var.names[i], scan(grid.names[i], skip=6,na.string="-9999"), pos=1)} <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Store raster values in a dataframe using same <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">var.names[i] <span style="font-family: Arial,Helvetica,sans-serif;"> and define response values <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rasters // <-data.frame(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">predictor_names_list //, rep(1,length(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">predictor // ))) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">names(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rasters // )[15]<-"<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response // " <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rasters // $<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response // <-factor(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rasters // $<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response //, levels = c("0","1")) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Predict outcomes for imported raster layers <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Use the full number of trees determined from minimized holdout deviance <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rast.pred // <-predict.gbm(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //, <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rasters // , n.trees=<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#### // ) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Review prediction output for proper length <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">summary(<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"> rast //.pred// ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // Min. 1st Qu. Median Mean 3rd Qu. Max. <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">length(<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"> rast //.pred// ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // [1] //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">##### // <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Export prediction output to ASCII file for use as layer in GIS <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">write(<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;"> rast //.pred//, "C:/<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">your_folder_path // /<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">prediction // .asc", ncolumns= # <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">## <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">) <span style="font-family: Arial,Helvetica,sans-serif;"># Number of columns must be the same as the original input rasters for proper projection <span style="font-family: Arial,Helvetica,sans-serif;"># Open the <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">prediction // .asc file in text editor (e.g. Notepad) and paste the spatial information from one of the original raster ASCII export file headers <span style="font-family: Arial,Helvetica,sans-serif;"># Import and convert <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">prediction // .asc to raster in GIS

<span style="font-family: Arial,Helvetica,sans-serif;"># Evaluate model predictions with independent data <span style="font-family: Arial,Helvetica,sans-serif;"># Import evaluation data with all the same predictor variables as were used in creating the model <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">eval.data // <-read.table("C:/<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">your_folder_path // /<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">filename // .txt", header = TRUE, sep = "\t") <span style="color: #000000; font-family: Arial,Helvetica,sans-serif;"># Call predictor names to ensure all match and are present <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">names(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">eval.data // ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // $var.names <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Predict outcomes for imported evaluation data <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Use the full number of trees determined from minimized holdout deviance <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.pred // <-predict.gbm(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //, <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">eval.data // , n.trees=<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#### // ) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Review prediction output for value range and vector length <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">summary(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.pred // ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // Min. 1st Qu. Median Mean 3rd Qu. Max. <span style="color: #000000; font-family: arial,helvetica,sans-serif; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.#### // <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">length(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.pred // ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">length(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">eval.data // $<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response // ) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Evaluate the predicted outcomes compared to observations <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">library(ROCR) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.eval // <-prediction(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.pred //, <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">eval.data // $<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response // ) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Determine the area under the relative operating characteristic curve <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">performance(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.eval // ,"auc")@y.values <span style="color: #000000; font-family: arial,helvetica,sans-serif; margin: 0px; padding: 0px;">//<span style="color: #000000; font-family: arial,helvetica,sans-serif; font-style: normal;">## // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.####### // <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Plot the relative operating characteristic curve <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">plot(performance(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">brt.eval //, "tpr", "fpr"), main= "ROC curve") <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Plot the average classification accuracy across a range of threshold values <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">plot(performance(brt.eval, "acc"), avg="vertical", spread.estimate="boxplot", main= "Accuracy across the range of possible cutoffs", ...) <span style="color: #000000; font-family: Arial,Helvetica,sans-serif; margin: 0px; padding: 0px;"># Plot precision v. recall of model predictions <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">plot(performance(brt.eval, "prec", "rec"), main="Precision/Recall")

**Weblinks:**

Online textbook extremely brief introduction to boosting decision trees - []

 * References: **