|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +context("basic tests for CRAN") |
| 19 | + |
| 20 | +test_that("create DataFrame from list or data.frame", { |
| 21 | + sparkR.session(master = sparkRTestMaster, enableHiveSupport = FALSE) |
| 22 | + |
| 23 | + i <- 4 |
| 24 | + df <- createDataFrame(data.frame(dummy = 1:i)) |
| 25 | + expect_equal(count(df), i) |
| 26 | + |
| 27 | + l <- list(list(a = 1, b = 2), list(a = 3, b = 4)) |
| 28 | + df <- createDataFrame(l) |
| 29 | + expect_equal(columns(df), c("a", "b")) |
| 30 | + |
| 31 | + a <- 1:3 |
| 32 | + b <- c("a", "b", "c") |
| 33 | + ldf <- data.frame(a, b) |
| 34 | + df <- createDataFrame(ldf) |
| 35 | + expect_equal(columns(df), c("a", "b")) |
| 36 | + expect_equal(dtypes(df), list(c("a", "int"), c("b", "string"))) |
| 37 | + expect_equal(count(df), 3) |
| 38 | + ldf2 <- collect(df) |
| 39 | + expect_equal(ldf$a, ldf2$a) |
| 40 | + |
| 41 | + mtcarsdf <- createDataFrame(mtcars) |
| 42 | + expect_equivalent(collect(mtcarsdf), mtcars) |
| 43 | + |
| 44 | + bytes <- as.raw(c(1, 2, 3)) |
| 45 | + df <- createDataFrame(list(list(bytes))) |
| 46 | + expect_equal(collect(df)[[1]][[1]], bytes) |
| 47 | + |
| 48 | + sparkR.session.stop() |
| 49 | +}) |
| 50 | + |
| 51 | +test_that("spark.glm and predict", { |
| 52 | + sparkR.session(master = sparkRTestMaster, enableHiveSupport = FALSE) |
| 53 | + |
| 54 | + training <- suppressWarnings(createDataFrame(iris)) |
| 55 | + # gaussian family |
| 56 | + model <- spark.glm(training, Sepal_Width ~ Sepal_Length + Species) |
| 57 | + prediction <- predict(model, training) |
| 58 | + expect_equal(typeof(take(select(prediction, "prediction"), 1)$prediction), "double") |
| 59 | + vals <- collect(select(prediction, "prediction")) |
| 60 | + rVals <- predict(glm(Sepal.Width ~ Sepal.Length + Species, data = iris), iris) |
| 61 | + expect_true(all(abs(rVals - vals) < 1e-6), rVals - vals) |
| 62 | + |
| 63 | + # Gamma family |
| 64 | + x <- runif(100, -1, 1) |
| 65 | + y <- rgamma(100, rate = 10 / exp(0.5 + 1.2 * x), shape = 10) |
| 66 | + df <- as.DataFrame(as.data.frame(list(x = x, y = y))) |
| 67 | + model <- glm(y ~ x, family = Gamma, df) |
| 68 | + out <- capture.output(print(summary(model))) |
| 69 | + expect_true(any(grepl("Dispersion parameter for gamma family", out))) |
| 70 | + |
| 71 | + # tweedie family |
| 72 | + model <- spark.glm(training, Sepal_Width ~ Sepal_Length + Species, |
| 73 | + family = "tweedie", var.power = 1.2, link.power = 0.0) |
| 74 | + prediction <- predict(model, training) |
| 75 | + expect_equal(typeof(take(select(prediction, "prediction"), 1)$prediction), "double") |
| 76 | + vals <- collect(select(prediction, "prediction")) |
| 77 | + |
| 78 | + # manual calculation of the R predicted values to avoid dependence on statmod |
| 79 | + #' library(statmod) |
| 80 | + #' rModel <- glm(Sepal.Width ~ Sepal.Length + Species, data = iris, |
| 81 | + #' family = tweedie(var.power = 1.2, link.power = 0.0)) |
| 82 | + #' print(coef(rModel)) |
| 83 | + |
| 84 | + rCoef <- c(0.6455409, 0.1169143, -0.3224752, -0.3282174) |
| 85 | + rVals <- exp(as.numeric(model.matrix(Sepal.Width ~ Sepal.Length + Species, |
| 86 | + data = iris) %*% rCoef)) |
| 87 | + expect_true(all(abs(rVals - vals) < 1e-5), rVals - vals) |
| 88 | + |
| 89 | + sparkR.session.stop() |
| 90 | +}) |
0 commit comments