Github user sethah commented on a diff in the pull request: https://github.com/apache/spark/pull/16699#discussion_r99263111 --- Diff: mllib/src/test/scala/org/apache/spark/ml/regression/GeneralizedLinearRegressionSuite.scala --- @@ -743,6 +743,84 @@ class GeneralizedLinearRegressionSuite } } + test("generalized linear regression with offset") { + /* + R code: + library(statmod) + df <- as.data.frame(matrix(c( + 1.0, 1.0, 2.0, 0.0, 5.0, + 2.0, 2.0, 0.5, 1.0, 2.0, + 1.0, 3.0, 1.0, 2.0, 1.0, + 2.0, 4.0, 0.0, 3.0, 3.0), 4, 5, byrow = TRUE)) + families <- list(gaussian, poisson, Gamma, tweedie(1.5)) + f1 <- V1 ~ -1 + V4 + V5 + f2 <- V1 ~ V4 + V5 + for (f in c(f1, f2)) { + for (fam in families) { + model <- glm(f, df, family = fam, weights = V2, offset = V3) + print(as.vector(coef(model))) + } + } + + [1] 0.535040431 0.005390836 + [1] 0.1968355 -0.2061711 + [1] 0.307996 -0.153579 + [1] 0.32166185 -0.09698986 + [1] -0.8800000 0.7342857 0.1714286 + [1] -1.9991044 0.7247511 0.1424392 + [1] -0.27378146 0.31599396 -0.06204946 + [1] -0.17118812 0.31200361 -0.02541656 + */ + val dataset = Seq( + OffsetInstance(1.0, 1.0, 2.0, Vectors.dense(0.0, 5.0)), + OffsetInstance(2.0, 2.0, 0.5, Vectors.dense(1.0, 2.0)), + OffsetInstance(1.0, 3.0, 1.0, Vectors.dense(2.0, 1.0)), + OffsetInstance(2.0, 4.0, 0.0, Vectors.dense(3.0, 3.0)) + ).toDF() + + val expected = Seq( + Vectors.dense(0.0, 0.535040431, 0.005390836), + Vectors.dense(0.0, 0.1968355, -0.2061711), + Vectors.dense(0.0, 0.307996, -0.153579), + Vectors.dense(0.0, 0.32166185, -0.09698986), + Vectors.dense(-0.88, 0.7342857, 0.1714286), + Vectors.dense(-1.9991044, 0.7247511, 0.1424392), + Vectors.dense(-0.27378146, 0.31599396, -0.06204946), + Vectors.dense(-0.17118812, 0.31200361, -0.02541656)) + + import GeneralizedLinearRegression._ + + var idx = 0 + for (fitIntercept <- Seq(false, true)) { + for (family <- Seq("gaussian", "poisson", "gamma", "tweedie")) { + var trainer = new GeneralizedLinearRegression().setFamily(family) + .setFitIntercept(fitIntercept).setOffsetCol("offset") + .setWeightCol("weight").setLinkPredictionCol("linkPrediction") + if (family == "tweedie") trainer = trainer.setVariancePower(1.5) + val model = trainer.fit(dataset) + val actual = Vectors.dense(model.intercept, model.coefficients(0), model.coefficients(1)) + assert(actual ~= expected(idx) absTol 1e-4, s"Model mismatch: GLM with family = $family," + --- End diff -- We need to be checking more than just the coefficients. For example, the computation of the null deviance does not match R, since the null model computation does not consider the offsets. Actually, I think we ought to just incorporate offsets into all of the other tests, which will make sure offsets are exhaustively tested. This has been done before e.g. https://github.com/apache/spark/pull/15488, and it _is_ a real pain, but it's probably the best way. I'd be open to other arguments though.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org