library(rstpm2) ## for coping with weird test behaviour from CRAN and R-devel .CRAN <- TRUE slow <- FALSE expect_eps <- function(expr, value, eps=1e-7) expect_lt(max(abs(expr-value)),eps) context("Missing data - stpm2") ## fit0 <- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer2[-1,]) ## dput("names<-"(coef(fit0),NULL)) beta1 <- c(-7.24331354281574, -0.359486483311025, 4.75355863915738, 11.5332451005529, 4.56596451196539) ## test_that("Missing event time - stpm2", { brcancer2 <- rstpm2::brcancer brcancer2$rectime[1] <- NA expect_warning(fit1 <<- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer2), "Some event times are NA") expect_eps(coef(fit1),beta1, 1e-5) expect_length(predict(fit1), 685) }) test_that("Invalid event time - stpm2", { brcancer2 <- rstpm2::brcancer brcancer2$rectime[1] <- -1 expect_warning(fit1 <<- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer2), "Some event times <= 0") expect_eps(coef(fit1),beta1, 1e-5) expect_length(predict(fit1), 685) }) test_that("Missing covariate - stpm2", { brcancer2 <- rstpm2::brcancer brcancer2$hormon[1] <- NA fit1 <- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer2) expect_eps(coef(fit1),beta1, 1e-5) expect_length(predict(fit1), 685) }) test_that("Missing weight - stpm2", { brcancer2 <- transform(rstpm2::brcancer, w=1) brcancer2$w[1] <- NA fit1 <- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer2,weights=w) expect_eps(coef(fit1),beta1, 1e-5) expect_length(predict(fit1), 685) }) test_that("Predictions with missing values - stpm2", { brcancer2 <- transform(rstpm2::brcancer, w=1) brcancer2$w[1] <- NA fit1 <- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer2,weights=w) test <- c(surv=0.7230207, fail=1-0.7230207, haz=0.000335262) for(name in names(test)) expect_eps(predict(fit1,newdata=data.frame(hormon=1,rectime=1000),type=name), test[name], 1e-6) expect_eps(predict(fit1,newdata=data.frame(hormon=1,rectime=1000),type="hr",var="hormon"), 0.6980347, 1e-6) expect_eps(predict(fit1,newdata=data.frame(hormon=1,rectime=1000),type="hdiff",var="hormon"), -0.0001012377, 1e-6) }) test_that("Missing bhazard - stpm2", { set.seed(12345) x <- rnorm(1e3,0,0.2) cause1 <- rexp(1e3,1e-3*exp(x)) other <- rexp(1e3,1e-4) e <- cause1