# Original link:http://tecdat.cn/?p=22482

# introduction

This paper is a short tutorial on fitting BRT (lifting regression tree) model in R. Our goal is to apply the BRT (lifting regression tree) model to ecological data and interpret the results.

The purpose of this tutorial is to help you learn how to develop a BRT model in R.

# Sample data

There are two sets of recorded data of short fin eel. One for model training (establishment) and one for model testing (evaluation). In the following example, we load training data. Presence (1) and absence (0) are recorded in column 2. Environmental variables are listed in columns 3 to 14.

`> head(train)`

# Fitting model

To fit the GBM model, you need to decide what settings to use. This article provides you with information on the use of rules of thumb. These data have recorded the presence of 202 short fin eels in 1000 locations. You can assume: 1. There is enough data to establish an interaction model with reasonable complexity. 2 2. The LR learning rate of about 0.01 may be a reasonable initial point. The following example shows how to determine the optimal number of trees (NT).

```
step(data= train, x = 3:13,
family = "bernoulli", comp = 5,
lr = 0.01, bag.fr = 0.5)
```

The lifting regression tree model is cross validated and optimized.

Using 1000 observations and 11 predictors, an initial model of 10 50 trees was created.

We used cross validation above. We define: data; Predictive variables; Dependent variable — column number representing species data; Tree complexity — we first try that the tree complexity is 5; Learning rate — we try to use 0.01.

Running a model as described above will output a progress report and make a graph. First, what you can see. This model is established with the default 10x cross validation method. The black solid curve is the average value of the change in the predicted deviation, and the point curve is 1 standard error (i.e. the result measured on the cross validation). The red line represents the minimum value of the average, and the green line represents the number of trees that generate the value. The final model returned in the model object is established on the complete data set, using the optimal number of trees.

`length(fitted)`

The returned results include fitted – the fitted value from the final tree, fitted.vars – the variance of the fitted value, residuals – the residual of the fitted value, and contribution – the relative importance of the variable. Statistics – relevant evaluation statistics. CV. Statistics these are the most appropriate evaluation statistics.

We calculate each statistic in each cross validation (calculated according to the average change of prediction deviation in all cross validation under the determined optimal number of trees), and then present the average value and standard error of these statistics based on cross validation here. Weights – the weight used to fit the model (by default, each observation is “1”, i.e. the weight is equal). Trees. Fitted – record of the number of trees fitted at each step in the phased fitting process; Training.loss.values – the phased change of the deviation on the training data, cv.values – the average of the CV estimates of the prediction deviation calculated at each step of the phased process.

You can use the summary function to see the importance of variables

`> summary(lr )`

# Select Settings

The above is a preliminary guess of the setting, using the empirical rules discussed in elith et al. (2008). Its model has only 650 trees, so our next step will be to reduce LR. For example, try LR = 0.005 for more than 1000 trees.

```
step(data=train, x = 3:13,
tree.co = 5,
+ lr = 0.005
```

To explore whether other settings perform better, you can divide the data into training sets and test sets, or use cross validation results to change TC, LR and bagging, and then compare the results.

# simplified model

Simplification builds many models, so it can be slow. Among them, we evaluated the value of the model with a simplified LR of 0.005, but only tested the elimination of up to 5 variables (“n.drop” parameter; the default is the automatic rule until the average change of the prediction deviation exceeds the original standard error calculated in gbm.step).

For our operation, it is estimated that the optimal number of variables to be eliminated is 1; You can use the number indicated by the red vertical line. Now, build a model to eliminate one predictive variable, and use [[1]] to indicate that we want to eliminate one variable.

`step( x= pred.list\[\[1\]\], )`

Now this has formed a new model, but considering that we do not particularly want a simpler model (because it is acceptable to include a small contribution of variables in a data set of this scale), we will not continue to use it.

# Draw the function and fitting value of the model

The fitting function of BRT model created by our function can be drawn by plot.

`> plot( lr005 )`

The additional parameters of this function allow a smooth representation of the graph. According to the distribution of observed values in environmental space, the fitting function can give the fitting value distribution related to each predictor.

` fits( lr005)`

The values above each graph represent the weighted average of the fitted values associated with each non factor predictor.

# Drawing interaction

This code evaluates the degree of pairwise interaction in the data.

` inter( lr005)`

Returns a list. The first two parts are the summary of the results. The first is the ranking list of the five most important interactions, and the second is the table of all interactions.

`f$intera`

You can draw interactions like this.

`persp( lr005, z.range=c(0,0.6)`

# Forecast new data

If you want to predict a group of locations (rather than the whole map), the general procedure is to establish a data framework, with rows representing locations and columns representing variables in your model. The data set we use to predict the site is in a file called test. “The column needs to be converted into a factor variable, and its level is consistent with that in the modeling data. Use predict to predict the sites in the BRT model, and the prediction results are in a vector named preds.

```
preds <- predict(lr005,test,
deviance(obs=test, pred=preds)
```

```
> d <- cbind(obs, preds)
> e <- evaluate(p=pres, a=abs)
```

A useful feature of prediction in GBM is that it can predict different numbers of trees.

```
tree<- seq(100, 5000, by=100)
predict( n.trees=tree, "response")
```

The above code will form a matrix. Each column is the prediction of the model for the number of trees specified by this element in tree.list. For example, the prediction in column 5 is for tree. List [5] = 500 trees. Now calculate the deviation of all these results, and then draw.

```
> for (i in 1:50) {
calc.devi(obs,
+ pred\[,i\])
+ }
> plot(tree.list,deviance
```

# Spatial prediction

Here we show how to predict the whole map.

`> plot(grids)`

We create a data. Frame with a constant value (the “factor” class) and pass it to the prediction function.

```
> p <- predict(grids, lr005,
> plot(p)
```

Most popular insights

1.Application case of multiple logistic regression in R language

2.Implementation of panel smooth transfer regression (PSTR) analysis case

3.Partial least squares regression (PLSR) and principal component regression (PCR) in MATLAB

4.Case study of Poisson Poisson regression model in R language

5.Hosmer lemeshow goodness of fit test in R language regression

6.Implementation of lasso regression, ridge ridge regression and elastic net model in R language

7.Implementation of logistic regression in R language

8.Python uses linear regression to predict stock price

9.How does R language calculate IDI and NRI in survival analysis and Cox regression