Last active
September 15, 2021 23:56
-
-
Save nathansutton/4aecc0fa499e779941dfbdaaa5a41405 to your computer and use it in GitHub Desktop.
translate an h2o model into sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{r} | |
translate <- function(fit){ | |
### return the sql translation of a h2o.glm model ### | |
# safety | |
stopifnot(class(fit) %in% c("H2OBinomialModel","H2ORegressionModel")) | |
stopifnot(.hasSlot(fit, "algorithm")) | |
stopifnot(.hasSlot(fit, "model")) | |
stopifnot(.hasSlot(fit, "parameters")) | |
stopifnot(fit@algorithm == "glm") | |
stopifnot(fit@parameters$family %in% c("binomial","gaussian")) | |
stopifnot("coefficients" %in% names(fit@model)) | |
# extract coefficients | |
df <- data.frame( | |
term = names(fit@model$coefficients), | |
beta = fit@model$coefficients, | |
sql = character(length(fit@model$coefficients)), | |
stringsAsFactors = FALSE, | |
row.names = NULL | |
) | |
# pick out the intercept | |
intercept <- grepl("intercept",tolower(df$term)) | |
# translate each row into sql term * beta | |
df$sql[intercept] <- paste0('(',df$beta[intercept],')') # no multiplication | |
df$sql[!intercept] <- paste0('(',df$term[!intercept],'',' * ',df$beta[!intercept],')') | |
# a classification model | |
if(class(fit) == "H2OBinomialModel"){ | |
# logistic function | |
sql <- paste0( | |
'1.0 - 1.0 / (1.0 + EXP(', | |
paste(df$sql,collapse = " + "), | |
'))' | |
) | |
# otherwise a regression model | |
} else { | |
sql <- paste(df$sql,collapse = " + ") | |
} | |
return(sql) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment