Skip to content

Commit 900c85f

Browse files
committed
update test case for survreg
1 parent dbc1077 commit 900c85f

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

R/pkg/DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Depends:
1111
R (>= 3.0),
1212
methods,
1313
Suggests:
14-
testthat
14+
testthat,
15+
survival
1516
Description: R frontend for Spark
1617
License: Apache License (== 2.0)
1718
Collate:

R/pkg/inst/tests/testthat/test_mllib.R

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,23 @@ test_that("kmeans", {
142142
expect_equal(sort(collect(distinct(select(cluster, "prediction")))$prediction), c(0, 1))
143143
})
144144

145-
test_that("survreg vs survival::survreg", {
146-
data <- list(list(4, 1, 0, 0), list(3, 1, 2, 0), list(1, 1, 1, 0),
147-
list(1, 0, 1, 0), list(2, 1, 1, 1), list(2, 1, 0, 1), list(3, 0, 0, 1))
148-
df <- createDataFrame(sqlContext, data, c("time", "status", "x", "sex"))
149-
model <- survreg(Surv(time, status) ~ x + sex, df)
145+
test_that("SparkR::survreg vs survival::survreg", {
146+
library(survival)
147+
data(ovarian)
148+
df <- suppressWarnings(createDataFrame(sqlContext, ovarian))
149+
150+
model <- SparkR::survreg(Surv(futime, fustat) ~ ecog_ps + rx, df)
150151
stats <- summary(model)
151-
coefs <- as.vector(stats$coefficients[, 1])
152-
rCoefs <- c(1.3149571, -0.1903409, -0.2532618, -1.1599802)
152+
coefs <- as.vector(stats$coefficients[, 1][1:3])
153+
scale <- exp(stats$coefficients[, 1][4])
154+
155+
rModel <- survival::survreg(Surv(futime, fustat) ~ ecog.ps + rx, ovarian)
156+
rCoefs <- as.vector(coef(rModel))
157+
rScale <- rModel$scale
158+
153159
expect_true(all(abs(rCoefs - coefs) < 1e-4))
160+
expect_true(abs(rScale - scale) < 1e-4)
154161
expect_true(all(
155162
rownames(stats$coefficients) ==
156-
c("(Intercept)", "x", "sex", "Log(scale)")))
163+
c("(Intercept)", "ecog_ps", "rx", "Log(scale)")))
157164
})

0 commit comments

Comments
 (0)