How to do it...

Perform the following steps to calculate and classify data with the gradient boosting method:

  1. First, install and load the package gbm:
        > install.packages("gbm")
        > library(gbm)  
  1. The gbm function only uses responses ranging from 0 to 1; therefore, you should transform yes/no responses to numeric responses (0/1):
        > trainset$churn = ifelse(trainset$churn == "yes", 1, 0)   
  1. Next, you can use the gbm function to train a training dataset:
        > set.seed(2)
        > churn.gbm = gbm(formula = churn ~ .,distribution = "bernoulli"        ,data = trainset,n.trees = 1000,interaction.depth = 7,shrinkage         = 0.01, cv.folds=3)
  1. Then, you can obtain the summary information from the fitted model:
        > summary(churn.gbm)      
Relative influence plot of a fitted ...

Get Machine Learning with R Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.