Skip to content

Commit 51b446e

Browse files
author
Amy Wang
committed
glm non_negative coef argument added to R
1 parent 901bc40 commit 51b446e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

R/h2o-package/R/Algorithms.R

+13-12
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
262262
epsilon = 1e-4, standardize = TRUE, prior, variable_importances = FALSE, use_all_factor_levels = FALSE,
263263
tweedie.p = ifelse(family == "tweedie", 1.5, as.numeric(NA)), iter.max = 100,
264264
higher_accuracy = FALSE, lambda_search = FALSE, return_all_lambda = FALSE, max_predictors=-1,
265-
offset, has_intercept = TRUE) {
265+
offset, has_intercept = TRUE, non_negative = FALSE) {
266266

267267
if(!is.character(key)) stop("key must be of class character")
268268
if(nchar(key) > 0 && regexpr("^[a-zA-Z_][a-zA-Z0-9_.]*$", key)[1] == -1)
@@ -274,6 +274,7 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
274274
if(!is.numeric(alpha)) stop('alpha must be numeric')
275275
if( any(alpha < 0) ) stop('alpha must be >= 0')
276276
if(!is.logical(has_intercept)) stop('has_intercept must be logical')
277+
if(!is.logical(non_negative)) stop('non_negative must be logical')
277278
if(missing(offset)) { offset <- "" }
278279
else {
279280
if(!is.numeric(offset) && !is.character(offset)) stop("offset must be either an index or column name")
@@ -329,7 +330,7 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
329330
lambda_search = as.numeric(lambda_search), tweedie_variance_power = tweedie.p,
330331
max_predictors = max_predictors, variable_importances = as.numeric(variable_importances),
331332
use_all_factor_levels = as.numeric(use_all_factor_levels), link = link, offset = offset,
332-
has_intercept = as.numeric(has_intercept))
333+
has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
333334
else if(family == "binomial") {
334335
if(missing(prior)) prior = -1
335336
res = .h2o.__remoteSend(data@h2o, .h2o.__PAGE_GLM2, source = data@key, destination_key = key,
@@ -340,8 +341,8 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
340341
lambda_search = as.numeric(lambda_search), prior = prior,
341342
max_predictors = max_predictors, variable_importances = as.numeric(variable_importances),
342343
use_all_factor_levels = as.numeric(use_all_factor_levels), link = link, offset = offset,
343-
has_intercept = as.numeric(has_intercept))
344-
} else
344+
has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
345+
} else {
345346
res = .h2o.__remoteSend(data@h2o, .h2o.__PAGE_GLM2, source = data@key, destination_key = key,
346347
response = args$y, ignored_cols = paste(x_ignore, sep="", collapse=","), family = family,
347348
n_folds = nfolds, alpha = alpha, nlambdas = nlambda, lambda_min_ratio = lambda.min.ratio,
@@ -350,24 +351,24 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
350351
lambda_search = as.numeric(lambda_search),
351352
max_predictors = max_predictors, variable_importances = as.numeric(variable_importances),
352353
use_all_factor_levels = as.numeric(use_all_factor_levels), link = link, offset = offset,
353-
has_intercept = as.numeric(has_intercept))
354+
has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative)) }
354355

355356
params = list(x=args$x, y=args$y, family = .h2o.__getFamily(family, tweedie.var.p=tweedie.p), nfolds=nfolds,
356357
alpha=alpha, nlambda=nlambda, lambda.min.ratio=lambda.min.ratio, lambda=lambda,
357358
beta_epsilon=epsilon, standardize=standardize, max_predictors = max_predictors,
358359
variable_importances = variable_importances, use_all_factor_levels = use_all_factor_levels, h2o = data@h2o,
359-
link = link, offset = offset, has_intercept = as.numeric(has_intercept))
360+
link = link, offset = offset, has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
360361
.h2o.__waitOnJob(data@h2o, res$job_key)
361362
.h2o.get.glm(data@h2o, as.character(res$destination_key), return_all_lambda)
362363
} else
363364
.h2o.glm2grid.internal(x_ignore, args$y, data, key, family, link,nfolds, alpha, nlambda, lambda.min.ratio, lambda, epsilon,
364365
standardize, prior, tweedie.p, iter.max, higher_accuracy, lambda_search, return_all_lambda,
365366
variable_importances = variable_importances, use_all_factor_levels = use_all_factor_levels,
366-
offset = offset, has_intercept = as.numeric(has_intercept))
367+
offset = offset, has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
367368
}
368369

369370
.h2o.glm2grid.internal <- function(x_ignore, y, data, key, family, link, nfolds, alpha, nlambda, lambda.min.ratio, lambda, epsilon, standardize, prior, tweedie.p, iter.max, higher_accuracy, lambda_search, return_all_lambda,
370-
variable_importances, use_all_factor_levels, offset, has_intercept) {
371+
variable_importances, use_all_factor_levels, offset, has_intercept, non_negative) {
371372
if(family == "tweedie")
372373
res = .h2o.__remoteSend(data@h2o, .h2o.__PAGE_GLM2, source = data@key, destination_key = key, response = y,
373374
ignored_cols = paste(x_ignore, sep="", collapse=","), family = family, n_folds = nfolds,
@@ -376,7 +377,7 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
376377
higher_accuracy = as.numeric(higher_accuracy), lambda_search = as.numeric(lambda_search),
377378
tweedie_variance_power = tweedie.p, variable_importances = as.numeric(variable_importances),
378379
use_all_factor_levels = as.numeric(use_all_factor_levels), link = link, offset = offset,
379-
has_intercept = as.numeric(has_intercept))
380+
has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
380381
else if(family == "binomial") {
381382
if(missing(prior)) prior = -1
382383
res = .h2o.__remoteSend(data@h2o, .h2o.__PAGE_GLM2, source = data@key, destination_key = key, response = y,
@@ -385,7 +386,7 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
385386
beta_epsilon = epsilon, standardize = as.numeric(standardize), max_iter = iter.max,
386387
higher_accuracy = as.numeric(higher_accuracy), lambda_search = as.numeric(lambda_search), prior = prior,
387388
variable_importances = as.numeric(variable_importances), use_all_factor_levels = as.numeric(use_all_factor_levels),
388-
link = link, offset = offset, has_intercept = as.numeric(has_intercept))
389+
link = link, offset = offset, has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
389390
}
390391
else
391392
res = .h2o.__remoteSend(data@h2o, .h2o.__PAGE_GLM2, source = data@key, destination_key = key, response = y,
@@ -394,13 +395,13 @@ h2o.glm <- function(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5,
394395
beta_epsilon = epsilon, standardize = as.numeric(standardize), max_iter = iter.max,
395396
higher_accuracy = as.numeric(higher_accuracy), lambda_search = as.numeric(lambda_search),
396397
variable_importances = as.numeric(variable_importances), use_all_factor_levels = as.numeric(use_all_factor_levels),
397-
link = link, offset = offset, has_intercept = as.numeric(has_intercept))
398+
link = link, offset = offset, has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
398399

399400
params = list(x=setdiff(colnames(data)[-(x_ignore+1)], y), y=y, family=.h2o.__getFamily(family, tweedie.var.p=tweedie.p),
400401
link = link, nfolds=nfolds, alpha=alpha, nlambda=nlambda,
401402
lambda.min.ratio=lambda.min.ratio, lambda=lambda, beta_epsilon=epsilon, standardize=standardize,
402403
variable_importances = variable_importances, use_all_factor_levels = use_all_factor_levels, h2o = data@h2o,
403-
offset = offset, has_intercept = as.numeric(has_intercept))
404+
offset = offset, has_intercept = as.numeric(has_intercept), non_negative = as.numeric(non_negative))
404405

405406
.h2o.__waitOnJob(data@h2o, res$job_key)
406407
.h2o.get.glm.grid(data@h2o, as.character(res$destination_key), return_all_lambda, data)

R/h2o-package/man/h2o.glm.Rd

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ h2o.glm(x, y, data, key = "", family, link, nfolds = 0, alpha = 0.5, nlambda = -
1212
prior, variable_importances = FALSE, use_all_factor_levels = FALSE, tweedie.p =
1313
ifelse(family == 'tweedie', 1.5, as.numeric(NA)), iter.max = 100,
1414
higher_accuracy = FALSE, lambda_search = FALSE, return_all_lambda = FALSE,
15-
max_predictors = -1, offset, has_intercept = TRUE)
15+
max_predictors = -1, offset, has_intercept = TRUE, non_negative = FALSE)
1616
}
1717
\arguments{
1818
\item{x}{
@@ -88,6 +88,7 @@ tweedie: tweedie \cr
8888
}
8989
\item{offset}{(Optional) Column to be used as an offset, if you have one. }
9090
\item{has_intercept}{ A logical value indicating whether or not to include the intercept term. If there are factor columns in your model, then the intercept must be included. }
91+
\item{non_negative} { A logical value indicating whether or not coefficients will be restricted to only non-negative values.}
9192
}
9293
\value{
9394
An object of class \code{\linkS4class{H2OGLMModel}} with slots key, data, model and xval. The slot model is a list of the following components:

0 commit comments

Comments
 (0)