How does cross validation work in R's gbm package? Can someone provide a work flow about this? For instance, suppose I am doing binary classification,
For each iteration of the algorithm:


*

*Randomly sample k*N rows, where k is the bag.fraction, and N is nrow(dataset).

*Build a classifier using this training sample, suppose we use classification tree here.

*Predict the terminal node class label.


This is how boosting is done without a CV. If I want to do a 3-fold CV, where exactly do I apply it? Between step 1 and 2 or after step 3? 
 A: If you want to estimate the error of the model and its corresponding variability when predicting new observations, after step 3. After fitting all the trees. Here the model that is being validated is the whole ensemble of weak learners. But naturally you could tune the hyperparameters using CV too. For example the optimal number of boosted trees. In the package 'dismo' the function gbm.step does exactly this. Example of usage:
brtTuning<- gbm.step(data=yourData,
     gbm.x = 1:18,
     gbm.y = 19,
     family = "gaussian",
     tree.complexity = 5,
     learning.rate = 0.005,
     bag.fraction = 0.5)

If you want to tune and then validate I beleive you need to do nested cross-validation.
A: Cross validation works by randomly (or by some other means) selecting rows into $K$ equally sized folds that are approximately balanced, training a classifier on $K-$ folds, testing on the remaining fold and then calculating a predictive loss function. This is repeated so that each fold is used as the test set. If you are randomly sampling rows for the folds you can then resample as needed. There are a number of packages that can do this in R, and it is pretty easy to code it up yourself. Using some form of cross-validation with boosting is a bit more complicated (I'm not terribly familiar with boosting). This question seems to provide some insight into that though.
