How to do it...

The section will demonstrate steps to build the GLM model using H2O.

  1. Now, load the occupancy train and test datasets in R:
# Load the occupancy data 
occupancy_train <-read.csv("C:/occupation_detection/datatraining.txt",stringsAsFactors = T)
occupancy_test <- read.csv("C:/occupation_detection/datatest.txt",stringsAsFactors = T)
  1. The following independent (x) and dependent (y) variables will be used to model GLM:
# Define input (x) and output (y) variables"
x = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
y = "Occupancy"
  1. Based on the requirement for H2O, convert the dependent variables into factors as follows:
# Convert the outcome variable into factor
occupancy_train$Occupancy <- as.factor(occupancy_train$Occupancy)
occupancy_test$Occupancy <- as.factor(occupancy_test$Occupancy)
  1. Then, convert the datasets to H2OParsedData objects:
occupancy_train.hex <- as.h2o(x = occupancy_train, destination_frame = "occupancy_train.hex")
occupancy_test.hex <- as.h2o(x = occupancy_test, destination_frame = "occupancy_test.hex")
  1. Once the data is loaded and converted to H2OParsedData objects, run a GLM model using the h2o.glm function. In the current setup, we intend to train for parameters such as five-fold cross validation, elastic net regularization (α = 5), and optimal regularization strength (with lamda_search = TRUE):
# Train the model
occupancy_train.glm <- h2o.glm(x = x, # Vector of predictor variable names
y = y, # Name of response/dependent variable
training_frame = occupancy_train.hex, # Training data
seed = 1234567, # Seed for random numbers
family = "binomial", # Outcome variable
lambda_search = TRUE, # Optimum regularisation lambda
alpha = 0.5, # Elastic net regularisation
nfolds = 5 # N-fold cross validation
)
  1. In addition to the preceding command, you can also define other parameters to fine-tune the model performance. The following list does not cover all the functional parameters, but covers some based on importance. The complete list of parameters can be found in the documentation of the h2o package.
    • Specify the strategy of generating cross-validation samples such as random sampling, stratified sampling, modulo sampling, and auto (select) using fold_assignment. The sampling can also be performed on a particular attribute by specifying the column name (fold_column).
    • Option to handle skewed outcomes (imbalanced data) by specifying weights to each observation using weights_column or performing over/under sampling using balance_classes.
    • Option to handle missing values by mean imputation or observation skip using missing_values_handling.
    • Option to restrict the coefficients to be non-negative using non_negative and constrain their values using beta_constraints.
    • Option to provide prior probability for y==1(logistic regression) in the case of sampled data if its mean of response does not reflect the reality (prior).
    • Specify the variables to be considered for interactions (interactions).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset