Skip to content

Commit 1f1cf3a

Browse files
authored
[R] Replace vignettes and examples (#11123)
1 parent 5a94198 commit 1f1cf3a

23 files changed

+452
-2453
lines changed

R-package/R/xgb.importance.R

+45-66
Original file line numberDiff line numberDiff line change
@@ -38,85 +38,64 @@
3838
#' (based on C++ code), it starts at 0 (as in C/C++ or Python) instead of 1 (usual in R).
3939
#'
4040
#' @examples
41-
#'
42-
#' # binomial classification using "gbtree":
43-
#' data(agaricus.train, package = "xgboost")
44-
#'
45-
#' bst <- xgb.train(
46-
#' data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
47-
#' nrounds = 2,
48-
#' params = xgb.params(
49-
#' max_depth = 2,
50-
#' nthread = 2,
51-
#' objective = "binary:logistic"
52-
#' )
41+
#' # binary classification using "gbtree":
42+
#' data("ToothGrowth")
43+
#' x <- ToothGrowth[, c("len", "dose")]
44+
#' y <- ToothGrowth$supp
45+
#' model_tree_binary <- xgboost(
46+
#' x, y,
47+
#' nrounds = 5L,
48+
#' nthreads = 1L,
49+
#' booster = "gbtree",
50+
#' max_depth = 2L
5351
#' )
54-
#'
55-
#' xgb.importance(model = bst)
56-
#'
57-
#' # binomial classification using "gblinear":
58-
#' bst <- xgb.train(
59-
#' data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
60-
#' nrounds = 20,
61-
#' params = xgb.params(
62-
#' booster = "gblinear",
63-
#' learning_rate = 0.3,
64-
#' nthread = 1,
65-
#' objective = "binary:logistic"
66-
#' )
52+
#' xgb.importance(model_tree_binary)
53+
#'
54+
#' # binary classification using "gblinear":
55+
#' model_tree_linear <- xgboost(
56+
#' x, y,
57+
#' nrounds = 5L,
58+
#' nthreads = 1L,
59+
#' booster = "gblinear",
60+
#' learning_rate = 0.3
6761
#' )
68-
#'
69-
#' xgb.importance(model = bst)
70-
#'
71-
#' # multiclass classification using "gbtree":
72-
#' nclass <- 3
73-
#' nrounds <- 10
74-
#' mbst <- xgb.train(
75-
#' data = xgb.DMatrix(
76-
#' as.matrix(iris[, -5]),
77-
#' label = as.numeric(iris$Species) - 1
78-
#' ),
79-
#' nrounds = nrounds,
80-
#' params = xgb.params(
81-
#' max_depth = 3,
82-
#' nthread = 2,
83-
#' objective = "multi:softprob",
84-
#' num_class = nclass
85-
#' )
62+
#' xgb.importance(model_tree_linear)
63+
#'
64+
#' # multi-class classification using "gbtree":
65+
#' data("iris")
66+
#' x <- iris[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
67+
#' y <- iris$Species
68+
#' model_tree_multi <- xgboost(
69+
#' x, y,
70+
#' nrounds = 5L,
71+
#' nthreads = 1L,
72+
#' booster = "gbtree",
73+
#' max_depth = 3
8674
#' )
87-
#'
8875
#' # all classes clumped together:
89-
#' xgb.importance(model = mbst)
90-
#'
76+
#' xgb.importance(model_tree_multi)
9177
#' # inspect importances separately for each class:
78+
#' num_classes <- 3L
79+
#' nrounds <- 5L
9280
#' xgb.importance(
93-
#' model = mbst, trees = seq(from = 1, by = nclass, length.out = nrounds)
81+
#' model_tree_multi, trees = seq(from = 1, by = num_classes, length.out = nrounds)
9482
#' )
9583
#' xgb.importance(
96-
#' model = mbst, trees = seq(from = 2, by = nclass, length.out = nrounds)
84+
#' model_tree_multi, trees = seq(from = 2, by = num_classes, length.out = nrounds)
9785
#' )
9886
#' xgb.importance(
99-
#' model = mbst, trees = seq(from = 3, by = nclass, length.out = nrounds)
87+
#' model_tree_multi, trees = seq(from = 3, by = num_classes, length.out = nrounds)
10088
#' )
10189
#'
102-
#' # multiclass classification using "gblinear":
103-
#' mbst <- xgb.train(
104-
#' data = xgb.DMatrix(
105-
#' scale(as.matrix(iris[, -5])),
106-
#' label = as.numeric(iris$Species) - 1
107-
#' ),
108-
#' nrounds = 15,
109-
#' params = xgb.params(
110-
#' booster = "gblinear",
111-
#' learning_rate = 0.2,
112-
#' nthread = 1,
113-
#' objective = "multi:softprob",
114-
#' num_class = nclass
115-
#' )
90+
#' # multi-class classification using "gblinear":
91+
#' model_linear_multi <- xgboost(
92+
#' x, y,
93+
#' nrounds = 5L,
94+
#' nthreads = 1L,
95+
#' booster = "gblinear",
96+
#' learning_rate = 0.2
11697
#' )
117-
#'
118-
#' xgb.importance(model = mbst)
119-
#'
98+
#' xgb.importance(model_linear_multi)
12099
#' @export
121100
xgb.importance <- function(model = NULL, feature_names = getinfo(model, "feature_name"), trees = NULL) {
122101

R-package/R/xgb.plot.deepness.R

+10-13
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,24 @@
4949
#' data.table::setDTthreads(nthread)
5050
#'
5151
#' ## Change max_depth to a higher number to get a more significant result
52-
#' bst <- xgb.train(
53-
#' data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
52+
#' model <- xgboost(
53+
#' agaricus.train$data, factor(agaricus.train$label),
5454
#' nrounds = 50,
55-
#' params = xgb.params(
56-
#' max_depth = 6,
57-
#' nthread = nthread,
58-
#' objective = "binary:logistic",
59-
#' subsample = 0.5,
60-
#' min_child_weight = 2
61-
#' )
55+
#' max_depth = 6,
56+
#' nthreads = nthread,
57+
#' subsample = 0.5,
58+
#' min_child_weight = 2
6259
#' )
6360
#'
64-
#' xgb.plot.deepness(bst)
65-
#' xgb.ggplot.deepness(bst)
61+
#' xgb.plot.deepness(model)
62+
#' xgb.ggplot.deepness(model)
6663
#'
6764
#' xgb.plot.deepness(
68-
#' bst, which = "max.depth", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
65+
#' model, which = "max.depth", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
6966
#' )
7067
#'
7168
#' xgb.plot.deepness(
72-
#' bst, which = "med.weight", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
69+
#' model, which = "med.weight", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2
7370
#' )
7471
#'
7572
#' @rdname xgb.plot.deepness

R-package/R/xgb.plot.importance.R

+5-8
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,14 @@
5050
#' nthread <- 2
5151
#' data.table::setDTthreads(nthread)
5252
#'
53-
#' bst <- xgb.train(
54-
#' data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
53+
#' model <- xgboost(
54+
#' agaricus.train$data, factor(agaricus.train$label),
5555
#' nrounds = 2,
56-
#' params = xgb.params(
57-
#' max_depth = 3,
58-
#' nthread = nthread,
59-
#' objective = "binary:logistic"
60-
#' )
56+
#' max_depth = 3,
57+
#' nthreads = nthread
6158
#' )
6259
#'
63-
#' importance_matrix <- xgb.importance(colnames(agaricus.train$data), model = bst)
60+
#' importance_matrix <- xgb.importance(model)
6461
#' xgb.plot.importance(
6562
#' importance_matrix, rel_to_first = TRUE, xlab = "Relative importance"
6663
#' )

R-package/R/xgb.plot.multi.trees.R

+9-12
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,23 @@
3636
#' nthread <- 2
3737
#' data.table::setDTthreads(nthread)
3838
#'
39-
#' bst <- xgb.train(
40-
#' data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label),
39+
#' model <- xgboost(
40+
#' agaricus.train$data, factor(agaricus.train$label),
4141
#' nrounds = 30,
42-
#' verbose = 0,
43-
#' params = xgb.params(
44-
#' max_depth = 15,
45-
#' learning_rate = 1,
46-
#' nthread = nthread,
47-
#' objective = "binary:logistic",
48-
#' min_child_weight = 50
49-
#' )
42+
#' verbosity = 0L,
43+
#' nthreads = nthread,
44+
#' max_depth = 15,
45+
#' learning_rate = 1,
46+
#' min_child_weight = 50
5047
#' )
5148
#'
52-
#' p <- xgb.plot.multi.trees(model = bst, features_keep = 3)
49+
#' p <- xgb.plot.multi.trees(model, features_keep = 3)
5350
#' print(p)
5451
#'
5552
#' # Below is an example of how to save this plot to a file.
5653
#' if (require("DiagrammeR") && require("DiagrammeRsvg") && require("rsvg")) {
5754
#' fname <- file.path(tempdir(), "tree.pdf")
58-
#' gr <- xgb.plot.multi.trees(bst, features_keep = 3, render = FALSE)
55+
#' gr <- xgb.plot.multi.trees(model, features_keep = 3, render = FALSE)
5956
#' export_graph(gr, fname, width = 1500, height = 600)
6057
#' }
6158
#' @export

R-package/R/xgb.plot.shap.R

+22-29
Original file line numberDiff line numberDiff line change
@@ -81,51 +81,44 @@
8181
#' data.table::setDTthreads(nthread)
8282
#' nrounds <- 20
8383
#'
84-
#' bst <- xgb.train(
85-
#' data = xgb.DMatrix(agaricus.train$data, agaricus.train$label),
84+
#' model_binary <- xgboost(
85+
#' agaricus.train$data, factor(agaricus.train$label),
8686
#' nrounds = nrounds,
87-
#' verbose = 0,
88-
#' params = xgb.params(
89-
#' learning_rate = 0.1,
90-
#' max_depth = 3,
91-
#' subsample = 0.5,
92-
#' objective = "binary:logistic",
93-
#' nthread = nthread
94-
#' )
87+
#' verbosity = 0L,
88+
#' learning_rate = 0.1,
89+
#' max_depth = 3L,
90+
#' subsample = 0.5,
91+
#' nthreads = nthread
9592
#' )
9693
#'
97-
#' xgb.plot.shap(agaricus.test$data, model = bst, features = "odor=none")
94+
#' xgb.plot.shap(agaricus.test$data, model = model_binary, features = "odor=none")
9895
#'
99-
#' contr <- predict(bst, agaricus.test$data, predcontrib = TRUE)
100-
#' xgb.plot.shap(agaricus.test$data, contr, model = bst, top_n = 12, n_col = 3)
96+
#' contr <- predict(model_binary, agaricus.test$data, type = "contrib")
97+
#' xgb.plot.shap(agaricus.test$data, contr, model = model_binary, top_n = 12, n_col = 3)
10198
#'
10299
#' # Summary plot
103-
#' xgb.ggplot.shap.summary(agaricus.test$data, contr, model = bst, top_n = 12)
100+
#' xgb.ggplot.shap.summary(agaricus.test$data, contr, model = model_binary, top_n = 12)
104101
#'
105102
#' # Multiclass example - plots for each class separately:
106-
#' nclass <- 3
107103
#' x <- as.matrix(iris[, -5])
108104
#' set.seed(123)
109105
#' is.na(x[sample(nrow(x) * 4, 30)]) <- TRUE # introduce some missing values
110106
#'
111-
#' mbst <- xgb.train(
112-
#' data = xgb.DMatrix(x, label = as.numeric(iris$Species) - 1),
107+
#' model_multiclass <- xgboost(
108+
#' x, iris$Species,
113109
#' nrounds = nrounds,
114-
#' verbose = 0,
115-
#' params = xgb.params(
116-
#' max_depth = 2,
117-
#' subsample = 0.5,
118-
#' nthread = nthread,
119-
#' objective = "multi:softprob",
120-
#' num_class = nclass
121-
#' )
110+
#' verbosity = 0,
111+
#' max_depth = 2,
112+
#' subsample = 0.5,
113+
#' nthreads = nthread
122114
#' )
115+
#' nclass <- 3
123116
#' trees0 <- seq(from = 1, by = nclass, length.out = nrounds)
124117
#' col <- rgb(0, 0, 1, 0.5)
125118
#'
126119
#' xgb.plot.shap(
127120
#' x,
128-
#' model = mbst,
121+
#' model = model_multiclass,
129122
#' trees = trees0,
130123
#' target_class = 0,
131124
#' top_n = 4,
@@ -137,7 +130,7 @@
137130
#'
138131
#' xgb.plot.shap(
139132
#' x,
140-
#' model = mbst,
133+
#' model = model_multiclass,
141134
#' trees = trees0 + 1,
142135
#' target_class = 1,
143136
#' top_n = 4,
@@ -149,7 +142,7 @@
149142
#'
150143
#' xgb.plot.shap(
151144
#' x,
152-
#' model = mbst,
145+
#' model = model_multiclass,
153146
#' trees = trees0 + 2,
154147
#' target_class = 2,
155148
#' top_n = 4,
@@ -160,7 +153,7 @@
160153
#' )
161154
#'
162155
#' # Summary plot
163-
#' xgb.ggplot.shap.summary(x, model = mbst, target_class = 0, top_n = 4)
156+
#' xgb.ggplot.shap.summary(x, model = model_multiclass, target_class = 0, top_n = 4)
164157
#'
165158
#' @rdname xgb.plot.shap
166159
#' @export

R-package/R/xgb.plot.tree.R

+10-12
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,23 @@
3737
#' line.
3838
#'
3939
#' @examples
40-
#' data(agaricus.train, package = "xgboost")
41-
#'
42-
#' bst <- xgb.train(
43-
#' data = xgb.DMatrix(agaricus.train$data, agaricus.train$label),
44-
#' nrounds = 2,
45-
#' params = xgb.params(
46-
#' max_depth = 3,
47-
#' nthread = 2,
48-
#' objective = "binary:logistic"
49-
#' )
40+
#' data("ToothGrowth")
41+
#' x <- ToothGrowth[, c("len", "dose")]
42+
#' y <- ToothGrowth$supp
43+
#' model <- xgboost(
44+
#' x, y,
45+
#' nthreads = 1L,
46+
#' nrounds = 3L,
47+
#' max_depth = 3L
5048
#' )
5149
#'
5250
#' # plot the first tree
53-
#' xgb.plot.tree(model = bst, tree_idx = 1)
51+
#' xgb.plot.tree(model, tree_idx = 1)
5452
#'
5553
#' # Below is an example of how to save this plot to a file.
5654
#' if (require("DiagrammeR") && require("htmlwidgets")) {
5755
#' fname <- file.path(tempdir(), "plot.html'")
58-
#' gr <- xgb.plot.tree(bst, tree_idx = 1)
56+
#' gr <- xgb.plot.tree(model, tree_idx = 1)
5957
#' htmlwidgets::saveWidget(gr, fname)
6058
#' }
6159
#' @export

0 commit comments

Comments
 (0)