# h2o.glm

##### Fit a generalized linear model

Fits a generalized linear model, specified by a response variable, a set of predictors, and a description of the error distribution.

##### Usage

```
h2o.glm(x, y, training_frame, model_id = NULL, validation_frame = NULL,
nfolds = 0, seed = -1, keep_cross_validation_models = TRUE,
keep_cross_validation_predictions = FALSE,
keep_cross_validation_fold_assignment = FALSE,
fold_assignment = c("AUTO", "Random", "Modulo", "Stratified"),
fold_column = NULL, ignore_const_cols = TRUE,
score_each_iteration = FALSE, offset_column = NULL,
weights_column = NULL, family = c("gaussian", "binomial",
"quasibinomial", "ordinal", "multinomial", "poisson", "gamma",
"tweedie"), tweedie_variance_power = 0, tweedie_link_power = 1,
solver = c("AUTO", "IRLSM", "L_BFGS", "COORDINATE_DESCENT_NAIVE",
"COORDINATE_DESCENT", "GRADIENT_DESCENT_LH", "GRADIENT_DESCENT_SQERR"),
alpha = NULL, lambda = NULL, lambda_search = FALSE,
early_stopping = TRUE, nlambdas = -1, standardize = TRUE,
missing_values_handling = c("MeanImputation", "Skip"),
compute_p_values = FALSE, remove_collinear_columns = FALSE,
intercept = TRUE, non_negative = FALSE, max_iterations = -1,
objective_epsilon = -1, beta_epsilon = 1e-04,
gradient_epsilon = -1, link = c("family_default", "identity",
"logit", "log", "inverse", "tweedie", "ologit", "oprobit", "ologlog"),
prior = -1, lambda_min_ratio = -1, beta_constraints = NULL,
max_active_predictors = -1, interactions = NULL,
interaction_pairs = NULL, obj_reg = -1,
export_checkpoints_dir = NULL, balance_classes = FALSE,
class_sampling_factors = NULL, max_after_balance_size = 5,
max_hit_ratio_k = 0, max_runtime_secs = 0,
custom_metric_func = NULL)
```

##### Arguments

- x
(Optional) A vector containing the names or indices of the predictor variables to use in building the model. If x is missing, then all columns except y are used.

- y
The name or column index of the response variable in the data. The response must be either a numeric or a categorical/factor variable. If the response is numeric, then a regression model will be trained, otherwise it will train a classification model.

- training_frame
Id of the training data frame.

- model_id
Destination id for this model; auto-generated if not specified.

- validation_frame
Id of the validation data frame.

- nfolds
Number of folds for K-fold cross-validation (0 to disable or >= 2). Defaults to 0.

- seed
Seed for random numbers (affects certain parts of the algo that are stochastic and those might or might not be enabled by default) Defaults to -1 (time-based random number).

- keep_cross_validation_models
`Logical`

. Whether to keep the cross-validation models. Defaults to TRUE.- keep_cross_validation_predictions
`Logical`

. Whether to keep the predictions of the cross-validation models. Defaults to FALSE.- keep_cross_validation_fold_assignment
`Logical`

. Whether to keep the cross-validation fold assignment. Defaults to FALSE.- fold_assignment
Cross-validation fold assignment scheme, if fold_column is not specified. The 'Stratified' option will stratify the folds based on the response variable, for classification problems. Must be one of: "AUTO", "Random", "Modulo", "Stratified". Defaults to AUTO.

- fold_column
Column with cross-validation fold index assignment per observation.

- ignore_const_cols
`Logical`

. Ignore constant columns. Defaults to TRUE.- score_each_iteration
`Logical`

. Whether to score during each iteration of model training. Defaults to FALSE.- offset_column
Offset column. This will be added to the combination of columns before applying the link function.

- weights_column
Column with observation weights. Giving some observation a weight of zero is equivalent to excluding it from the dataset; giving an observation a relative weight of 2 is equivalent to repeating that row twice. Negative weights are not allowed. Note: Weights are per-row observation weights and do not increase the size of the data frame. This is typically the number of times a row is repeated, but non-integer values are supported as well. During training, rows with higher weights matter more, due to the larger loss function pre-factor.

- family
Family. Use binomial for classification with logistic regression, others are for regression problems. Must be one of: "gaussian", "binomial", "quasibinomial", "ordinal", "multinomial", "poisson", "gamma", "tweedie". Defaults to gaussian.

- tweedie_variance_power
Tweedie variance power Defaults to 0.

- tweedie_link_power
Tweedie link power Defaults to 1.

- solver
AUTO will set the solver based on given data and the other parameters. IRLSM is fast on on problems with small number of predictors and for lambda-search with L1 penalty, L_BFGS scales better for datasets with many columns. Must be one of: "AUTO", "IRLSM", "L_BFGS", "COORDINATE_DESCENT_NAIVE", "COORDINATE_DESCENT", "GRADIENT_DESCENT_LH", "GRADIENT_DESCENT_SQERR". Defaults to AUTO.

- alpha
Distribution of regularization between the L1 (Lasso) and L2 (Ridge) penalties. A value of 1 for alpha represents Lasso regression, a value of 0 produces Ridge regression, and anything in between specifies the amount of mixing between the two. Default value of alpha is 0 when SOLVER = 'L-BFGS'; 0.5 otherwise.

- lambda
Regularization strength

- lambda_search
`Logical`

. Use lambda search starting at lambda max, given lambda is then interpreted as lambda min Defaults to FALSE.- early_stopping
`Logical`

. Stop early when there is no more relative improvement on train or validation (if provided) Defaults to TRUE.- nlambdas
Number of lambdas to be used in a search. Default indicates: If alpha is zero, with lambda search set to True, the value of nlamdas is set to 30 (fewer lambdas are needed for ridge regression) otherwise it is set to 100. Defaults to -1.

- standardize
`Logical`

. Standardize numeric columns to have zero mean and unit variance Defaults to TRUE.- missing_values_handling
Handling of missing values. Either MeanImputation or Skip. Must be one of: "MeanImputation", "Skip". Defaults to MeanImputation.

- compute_p_values
`Logical`

. Request p-values computation, p-values work only with IRLSM solver and no regularization Defaults to FALSE.- remove_collinear_columns
`Logical`

. In case of linearly dependent columns, remove some of the dependent columns Defaults to FALSE.- intercept
`Logical`

. Include constant term in the model Defaults to TRUE.- non_negative
`Logical`

. Restrict coefficients (not intercept) to be non-negative Defaults to FALSE.- max_iterations
Maximum number of iterations Defaults to -1.

- objective_epsilon
Converge if objective value changes less than this. Default indicates: If lambda_search is set to True the value of objective_epsilon is set to .0001. If the lambda_search is set to False and lambda is equal to zero, the value of objective_epsilon is set to .000001, for any other value of lambda the default value of objective_epsilon is set to .0001. Defaults to -1.

- beta_epsilon
Converge if beta changes less (using L-infinity norm) than beta esilon, ONLY applies to IRLSM solver Defaults to 0.0001.

- gradient_epsilon
Converge if objective changes less (using L-infinity norm) than this, ONLY applies to L-BFGS solver. Default indicates: If lambda_search is set to False and lambda is equal to zero, the default value of gradient_epsilon is equal to .000001, otherwise the default value is .0001. If lambda_search is set to True, the conditional values above are 1E-8 and 1E-6 respectively. Defaults to -1.

- link
Must be one of: "family_default", "identity", "logit", "log", "inverse", "tweedie", "ologit", "oprobit", "ologlog". Defaults to family_default.

- prior
Prior probability for y==1. To be used only for logistic regression iff the data has been sampled and the mean of response does not reflect reality. Defaults to -1.

- lambda_min_ratio
Minimum lambda used in lambda search, specified as a ratio of lambda_max (the smallest lambda that drives all coefficients to zero). Default indicates: if the number of observations is greater than the number of variables, then lambda_min_ratio is set to 0.0001; if the number of observations is less than the number of variables, then lambda_min_ratio is set to 0.01. Defaults to -1.

- beta_constraints
Beta constraints

- max_active_predictors
Maximum number of active predictors during computation. Use as a stopping criterion to prevent expensive model building with many predictors. Default indicates: If the IRLSM solver is used, the value of max_active_predictors is set to 5000 otherwise it is set to 100000000. Defaults to -1.

- interactions
A list of predictor column indices to interact. All pairwise combinations will be computed for the list.

- interaction_pairs
A list of pairwise (first order) column interactions.

- obj_reg
Likelihood divider in objective value computation, default is 1/nobs Defaults to -1.

- export_checkpoints_dir
Automatically export generated models to this directory.

- balance_classes
`Logical`

. Balance training data class counts via over/under-sampling (for imbalanced data). Defaults to FALSE.- class_sampling_factors
Desired over/under-sampling ratios per class (in lexicographic order). If not specified, sampling factors will be automatically computed to obtain class balance during training. Requires balance_classes.

- max_after_balance_size
Maximum relative size of the training data after balancing class counts (can be less than 1.0). Requires balance_classes. Defaults to 5.0.

- max_hit_ratio_k
Maximum number (top K) of predictions to use for hit ratio computation (for multi-class only, 0 to disable) Defaults to 0.

- max_runtime_secs
Maximum allowed runtime in seconds for model training. Use 0 to disable. Defaults to 0.

- custom_metric_func
Reference to custom evaluation function, format: `language:keyName=funcName`

##### Value

A subclass of `'>H2OModel`

is returned. The specific subclass depends on the machine
learning task at hand (if it's binomial classification, then an `'>H2OBinomialModel`

is
returned, if it's regression then a `'>H2ORegressionModel`

is returned). The default print-
out of the models is shown, but further GLM-specifc information can be queried out of the object. To access
these various items, please refer to the seealso section below. Upon completion of the GLM, the resulting
object has coefficients, normalized coefficients, residual/null deviance, aic, and a host of model metrics
including MSE, AUC (for logistic regression), degrees of freedom, and confusion matrices. Please refer to the
more in-depth GLM documentation available here:
https://h2o-release.s3.amazonaws.com/h2o-dev/rel-shannon/2/docs-website/h2o-docs/index.html#Data+Science+Algorithms-GLM

##### See Also

`predict.H2OModel`

for prediction, `h2o.mse`

, `h2o.auc`

,
`h2o.confusionMatrix`

, `h2o.performance`

, `h2o.giniCoef`

,
`h2o.logloss`

, `h2o.varimp`

, `h2o.scoreHistory`

##### Examples

```
# NOT RUN {
h2o.init()
# Run GLM of CAPSULE ~ AGE + RACE + PSA + DCAPS
prostate_path = system.file("extdata", "prostate.csv", package = "h2o")
prostate = h2o.importFile(path = prostate_path)
h2o.glm(y = "CAPSULE", x = c("AGE","RACE","PSA","DCAPS"), training_frame = prostate,
family = "binomial", nfolds = 0, alpha = 0.5, lambda_search = FALSE)
# Run GLM of VOL ~ CAPSULE + AGE + RACE + PSA + GLEASON
predictors = setdiff(colnames(prostate), c("ID", "DPROS", "DCAPS", "VOL"))
h2o.glm(y = "VOL", x = predictors, training_frame = prostate, family = "gaussian",
nfolds = 0, alpha = 0.1, lambda_search = FALSE)
# GLM variable importance
# Also see:
# https://github.com/h2oai/h2o/blob/master/R/tests/testdir_demos/runit_demo_VI_all_algos.R
bank = h2o.importFile(
path = "https://s3.amazonaws.com/h2o-public-test-data/smalldata/demos/bank-additional-full.csv")
predictors = 1:20
target="y"
glm = h2o.glm(x=predictors, y=target, training_frame=bank, family="binomial", standardize=TRUE,
lambda_search=TRUE)
h2o.std_coef_plot(glm, num_of_features = 20)
# }
```

*Documentation reproduced from package h2o, version 3.22.1.1, License: Apache License (== 2.0)*

### Community examples

**violar@mail.tau.ac.il**at Feb 13, 2019 h2o v3.8.1.3

#standard GLM with H2O library(h2o) h2o.init(nthreads=-1) training_data_table<-h2o.importFile("C:/Folder/DT.csv", header = TRUE, sep = ",") my_glm = h2o.glm(y="column_a", x=c("column_b","column_c"), training_frame = training_data_table) #All the other arguments have defaults