Skip to content

Commit

Permalink
"offset" parameter glm runit test and dataset addition
Browse files Browse the repository at this point in the history
  • Loading branch information
ericeckstrand committed Mar 24, 2015
1 parent e6b4882 commit faa6b35
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
34 changes: 34 additions & 0 deletions R/tests/testdir_algos/glm/runit_GLM_offset.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
setwd(normalizePath(dirname(R.utils::commandArgs(asValues=TRUE)$"f")))
source('../../findNSourceUtils.R')

test.GLM.offset <- function(conn) {
Log.info("Importing lung.csv data...\n")
lung.hex = h2o.uploadFile(conn, locate("smalldata/glm_test/lung.csv"))
lung.hex$log_pop <- log(lung.hex$pop)
lung.sum = summary(lung.hex)
print(lung.sum)

lung.r = read.csv(locate("smalldata/glm_test/lung.csv"), header = TRUE)
lung.r = na.omit(lung.r)

Log.info(cat("H2O GLM (poisson)"))
lung.glm.h2o = h2o.glm(y = 4, x = 1:2, data = lung.hex, family = "poisson", link = "log", offset = "log_pop")
print(lung.glm.h2o)

Log.info(cat("{stats} glm (poisson)"))
lung.glm.r = glm(cases ~ city + age + offset(log(pop)), family = "poisson", data = lung.r)

checkEqualsNumeric(lung.glm.h2o@model$deviance, lung.glm.r$deviance, tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$aic, lung.glm.r$aic, tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$null.deviance, lung.glm.r$null.deviance, tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$coefficients["city.Horsens"], lung.glm.r$coefficients["cityHorsens"], tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$coefficients["city.Kolding"], lung.glm.r$coefficients["cityKolding"], tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$coefficients["city.Vejle"], lung.glm.r$coefficients["cityVejle"], tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$coefficients["age"], lung.glm.r$coefficients["age"], tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$coefficients["Intercept"], lung.glm.r$coefficients["(Intercept)"], tolerance = 0.01)
checkEqualsNumeric(lung.glm.h2o@model$coefficients["log_pop"], 1.0, tolerance = 1e-10)

testEnd()
}

doTest("GLM offset test", test.GLM.offset)
25 changes: 25 additions & 0 deletions smalldata/glm_test/lung.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"city","age","pop","cases"
"Fredericia",47,3059,11
"Horsens",47,2879,13
"Kolding",47,3142,4
"Vejle",47,2520,5
"Fredericia",57,800,11
"Horsens",57,1083,6
"Kolding",57,1050,8
"Vejle",57,878,7
"Fredericia",62,710,11
"Horsens",62,923,15
"Kolding",62,895,7
"Vejle",62,839,10
"Fredericia",67,581,10
"Horsens",67,834,10
"Kolding",67,702,11
"Vejle",67,631,14
"Fredericia",72,509,11
"Horsens",72,634,12
"Kolding",72,535,9
"Vejle",72,539,8
"Fredericia",75,605,10
"Horsens",75,782,2
"Kolding",75,659,12
"Vejle",75,619,7

0 comments on commit faa6b35

Please sign in to comment.