## ---- echo=FALSE, include=FALSE----------------------------------------------- library(knitr) opts_chunk$set(fig.width = 6,fig.height = 6,fig.cap = '',collapse = TRUE) library(dismo) library(gbm) ## ----brt-0-------------------------------------------------------------------- library(dismo) data(Anguilla_train) head(Anguilla_train) ## ----brt-1-------------------------------------------------------------------- angaus.tc5.lr01 <- gbm.step(data=Anguilla_train, gbm.x = 3:13, gbm.y = 2, family = "bernoulli", tree.complexity = 5, learning.rate = 0.01, bag.fraction = 0.5) ## ----brt-2-------------------------------------------------------------------- names(angaus.tc5.lr01) summary(angaus.tc5.lr01) ## ----brt-3-------------------------------------------------------------------- angaus.tc5.lr005 <- gbm.step(data=Anguilla_train, gbm.x = 3:13, gbm.y = 2, family = "bernoulli", tree.complexity = 5, learning.rate = 0.005, bag.fraction = 0.5) ## ----brt-4-------------------------------------------------------------------- angaus.simp <- gbm.simplify(angaus.tc5.lr005, n.drops = 5) ## ----brt-5-------------------------------------------------------------------- angaus.tc5.lr005.simp <- gbm.step(Anguilla_train, gbm.x=angaus.simp$pred.list[[1]], gbm.y=2, tree.complexity=5, learning.rate=0.005) ## ----brt-6, fig.height = 9---------------------------------------------------- gbm.plot(angaus.tc5.lr005, n.plots=11, plot.layout=c(4, 3), write.title = FALSE) ## ----brt-7, fig.height = 9, fig.width = 9------------------------------------- gbm.plot.fits(angaus.tc5.lr005) ## ----brt-8-------------------------------------------------------------------- find.int <- gbm.interactions(angaus.tc5.lr005) find.int$interactions find.int$rank.list ## ----brt-9-------------------------------------------------------------------- gbm.perspec(angaus.tc5.lr005, 7, 1, y.range=c(15,20), z.range=c(0,0.6)) ## ----brt-10------------------------------------------------------------------- data(Anguilla_test) library(gbm) preds <- predict.gbm(angaus.tc5.lr005, Anguilla_test, n.trees=angaus.tc5.lr005$gbm.call$best.trees, type="response") calc.deviance(obs=Anguilla_test$Angaus_obs, pred=preds, calc.mean=TRUE) d <- cbind(Anguilla_test$Angaus_obs, preds) pres <- d[d[,1]==1, 2] abs <- d[d[,1]==0, 2] e <- evaluate(p=pres, a=abs) e ## ----brt-11------------------------------------------------------------------- angaus.5000 <- gbm.fixed(data=Anguilla_train, gbm.x=3:13, gbm.y=2, learning.rate=0.005, tree.complexity=5, n.trees=5000) tree.list <- seq(100, 5000, by=100) pred <- predict.gbm(angaus.5000, Anguilla_test, n.trees=tree.list, "response") ## ----brt-12------------------------------------------------------------------- angaus.pred.deviance <- rep(0,50) for (i in 1:50) { angaus.pred.deviance[i] <- calc.deviance(Anguilla_test$Angaus_obs, pred[,i], calc.mean=TRUE) } ## ----brt-13------------------------------------------------------------------- plot(tree.list, angaus.pred.deviance, ylim=c(0.7,1), xlim=c(-100,5000), type='l', xlab="number of trees", ylab="predictive deviance", cex.lab=1.5) ## ----brt-14, fig.width = 9---------------------------------------------------- data(Anguilla_grids) plot(Anguilla_grids) ## ----brt-15, fig.width = 7---------------------------------------------------- Method <- factor('electric', levels = levels(Anguilla_train$Method)) add <- data.frame(Method) p <- predict(Anguilla_grids, angaus.tc5.lr005, const=add, n.trees=angaus.tc5.lr005$gbm.call$best.trees, type="response") p <- mask(p, raster(Anguilla_grids, 1)) plot(p, main='Angaus - BRT prediction')