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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.