From b7d94b5eb1407c171f0272baf97980d7693caaa6 Mon Sep 17 00:00:00 2001 From: David Cortes Date: Sun, 23 Jan 2022 15:10:51 -0300 Subject: [PATCH 1/4] rename weight -> weights --- R-package/R/lightgbm.R | 7 ++++--- R-package/man/lightgbm.Rd | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R-package/R/lightgbm.R b/R-package/R/lightgbm.R index b40c8cc21c04..8fa17882a38e 100644 --- a/R-package/R/lightgbm.R +++ b/R-package/R/lightgbm.R @@ -91,7 +91,8 @@ NULL #' @description Simple interface for training a LightGBM model. #' @inheritParams lgb_shared_params #' @param label Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}} -#' @param weight vector of response values. If not NULL, will set to dataset +#' @param weights Sample / observation weights for rows in the input data. If `NULL`, will assume that all +#' observations / rows have the same importance / weight. #' @param save_name File name to use when writing the trained model to disk. Should end in ".model". #' If passing `NULL`, will not save the trained model to disk. #' @param ... Additional arguments passed to \code{\link{lgb.train}}. For example @@ -114,7 +115,7 @@ NULL #' @export lightgbm <- function(data, label = NULL, - weight = NULL, + weights = NULL, params = list(), nrounds = 100L, verbose = 1L, @@ -136,7 +137,7 @@ lightgbm <- function(data, # Check whether data is lgb.Dataset, if not then create lgb.Dataset manually if (!lgb.is.Dataset(x = dtrain)) { - dtrain <- lgb.Dataset(data = data, label = label, weight = weight) + dtrain <- lgb.Dataset(data = data, label = label, weight = weights) } train_args <- list( diff --git a/R-package/man/lightgbm.Rd b/R-package/man/lightgbm.Rd index 1e6be676f62e..6630fb47f605 100644 --- a/R-package/man/lightgbm.Rd +++ b/R-package/man/lightgbm.Rd @@ -7,7 +7,7 @@ lightgbm( data, label = NULL, - weight = NULL, + weights = NULL, params = list(), nrounds = 100L, verbose = 1L, @@ -27,7 +27,8 @@ may allow you to pass other types of data like \code{matrix} and then separately \item{label}{Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}}} -\item{weight}{vector of response values. If not NULL, will set to dataset} +\item{weights}{Sample / observation weights for rows in the input data. If `NULL`, will assume that all +observations / rows have the same importance / weight.} \item{params}{a list of parameters. See \href{https://lightgbm.readthedocs.io/en/latest/Parameters.html}{ the "Parameters" section of the documentation} for a list of parameters and valid values.} From 16d5d45fd42edeac0d12e527aa8523eacc8fd83a Mon Sep 17 00:00:00 2001 From: David Cortes Date: Wed, 23 Mar 2022 21:47:03 +0100 Subject: [PATCH 2/4] add test for 'weights' argument --- R-package/tests/testthat/test_basic.R | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/R-package/tests/testthat/test_basic.R b/R-package/tests/testthat/test_basic.R index 98744a942fa8..3c6707d4fd02 100644 --- a/R-package/tests/testthat/test_basic.R +++ b/R-package/tests/testthat/test_basic.R @@ -2927,3 +2927,29 @@ test_that("lightgbm() defaults to 'regression' objective if objective not otherw expect_true(any(model_txt_lines == "objective=regression")) expect_false(any(model_txt_lines == "objective=regression_l1")) }) + +test_that("lightgbm() accepts 'weight' and 'weights'", { + data(mtcars) + X <- as.matrix(mtcars[, -1L]) + y <- as.numeric(mtcars[, 1L]) + w <- rep(1.0, nrow(X)) + model <- lightgbm( + X + , y + , weights = w + , obj = "regression" + , nrounds = 5L + , verbose = -1L + ) + + # Avoid a bad CRAN check due to partial argument matches + lgb_args <- list( + X + , y + , weight = w + , obj = "regression" + , nrounds = 5L + , verbose = -1L + ) + model <- do.call(lightgbm, lgb_args) +}) From c2e206d3c3e48070935ae934142a2da2d2d8361a Mon Sep 17 00:00:00 2001 From: James Lamb Date: Fri, 25 Mar 2022 22:54:18 -0500 Subject: [PATCH 3/4] Update R-package/R/lightgbm.R --- R-package/R/lightgbm.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R-package/R/lightgbm.R b/R-package/R/lightgbm.R index 86be71ab1399..b76eb819c9db 100644 --- a/R-package/R/lightgbm.R +++ b/R-package/R/lightgbm.R @@ -91,7 +91,7 @@ NULL #' @description Simple interface for training a LightGBM model. #' @inheritParams lgb_shared_params #' @param label Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}} -#' @param weights Sample / observation weights for rows in the input data. If `NULL`, will assume that all +#' @param weights Sample / observation weights for rows in the input data. If \code{NULL}, will assume that all #' observations / rows have the same importance / weight. #' @param objective Optimization objective (e.g. `"regression"`, `"binary"`, etc.). #' For a list of accepted objectives, see From 314e6979b49b704e450761a5d7be264228f32eec Mon Sep 17 00:00:00 2001 From: David Cortes Date: Sat, 26 Mar 2022 09:33:32 +0100 Subject: [PATCH 4/4] update docs --- R-package/man/lightgbm.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R-package/man/lightgbm.Rd b/R-package/man/lightgbm.Rd index 64875b002589..4ea8cc3d663b 100644 --- a/R-package/man/lightgbm.Rd +++ b/R-package/man/lightgbm.Rd @@ -28,7 +28,7 @@ may allow you to pass other types of data like \code{matrix} and then separately \item{label}{Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}}} -\item{weights}{Sample / observation weights for rows in the input data. If `NULL`, will assume that all +\item{weights}{Sample / observation weights for rows in the input data. If \code{NULL}, will assume that all observations / rows have the same importance / weight.} \item{params}{a list of parameters. See \href{https://lightgbm.readthedocs.io/en/latest/Parameters.html}{