######################
## PLOTTING CHECKS
######################

test_that("Coxph plot no type", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  plot_options <- list("martingale" = FALSE, "surv_curv" = FALSE, "studyid" = "a", "verbose" = 0)
  if (system.file(package = "ggplot2") != "") {
    expect_error(plot(e, df, plot_options, verbose = 0))
  }
})
test_that("Coxph plot strata col not in data", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  # plot_options <- list("type" = c("surv", "run"), "martingale" = FALSE, "surv_curv" = FALSE, "strat_haz" = TRUE, "strat_col" = "e", "studyid" = "a", "verbose" = 0)
  plot_options <- list(fname = "run", "strat_haz" = TRUE, "strat_col" = "e", "studyid" = "a", "verbose" = 0)
  if (system.file(package = "ggplot2") != "") {
    # expect_error(plot(e, df, plot_options))
    expect_error(plotSurvival(e, df, plot_options))
  }
})
test_that("Coxph plot strata col not given", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  # plot_options <- list("type" = c("surv", "run"), "martingale" = FALSE, "surv_curv" = FALSE, "strat_haz" = TRUE, "studyid" = "a", "verbose" = 0)
  plot_options <- list(fname = "run", "strat_haz" = TRUE, "studyid" = "a", "verbose" = 0)
  if (system.file(package = "ggplot2") != "") {
    # expect_error(plot(e, df, plot_options))
    expect_error(plotSurvival(e, df, plot_options))
  }
})
test_that("Coxph plot martingale dose col not in data", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = "run", "martingale" = TRUE, "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    expect_error(plotMartingale(e, df, plot_options))
  }
})
test_that("Coxph plot martingale dose col not given", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = "run", "martingale" = TRUE, "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    expect_error(plotMartingale(e, df, plot_options))
  }
})
test_that("Coxph plot Survival ID col not in data", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  # plot_options <- list("type" = c("surv", "run"), "martingale" = FALSE, "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "f", "verbose" = 0, "km" = TRUE)
  plot_options <- list(fname = "run", "studyid" = "f", "km" = TRUE, "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    # expect_error(plot(e, df, plot_options))
    expect_error(plotSurvival(e, df, plot_options))
  }
})
test_that("Coxph plot Survival ID col not given", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list(fname = "run", "martingale" = FALSE, "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "verbose" = 0, "km" = TRUE)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    # expect_error(plot(e, df, plot_options))
    expect_error(plotSurvival(e, df, plot_options))
  }
})

test_that("Coxph risk too few unique values", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(1, 0, 1, 0, 1, 0, 0)
  d <- c(3, 1, 3, 1, 2, 3, 1)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  d <- c(3, 3, 3, 3, 3, 3, 3)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  options(warn = -1)
  if (system.file(package = "ggplot2") != "") {
    expect_error(plotRisk(e, df, plot_options))
  }
  options(warn = 0)
})
test_that("Coxph risk plotting above discrete step number limit", {
  a <- rep(c(0, 1, 2, 3, 4, 5, 6), 20)
  b <- rep(c(1, 2, 3, 4, 5, 6, 7), 20)
  c <- rep(c(1, 0, 1, 0, 1, 0, 0), 20)
  set.seed(3742)
  d <- runif(length(a))
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = 1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    expect_no_error(plotRisk(e, df, "fname" = paste(tempfile(), "run", sep = ""), studyid = "a", verbose = 0))
    expect_error(plotRisk(e, df, "fname" = paste(tempfile(), "run", sep = ""), studyid = "a", bad = 0))
    expect_error(plotRisk(e, df, plot_options = c(0))$d)
    expect_no_error(ep <- plotRisk(e, df, plot_options)$d)
    expect_equal(ep$y[100], 0.9938494, tolerance = 1e-4)
  }
})

test_that("Coxph plot no error", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(1, 0, 1, 0, 1, 0, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list(fname = paste(tempfile(), "run", sep = ""), "surv_curv" = TRUE, "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(tend = b, event = c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    # expect_no_error(ep <- plot(e, df, plot_options, verbose = 0)$standard)
    expect_no_error(ep <- plotSurvival(e, df, plot_options, verbose = 0)$standard)
    expect_equal(ep$h, c(0.0000000, 0.2551694, 0.3987517), tolerance = 1e-4)
  }
})
test_that("Coxph plot stratafied no error", {
  a <- c(0, 0, 0, 0, 0, 0, 1)
  b <- c(2, 3, 4, 2, 3, 2, 4)
  c <- c(1, 0, 1, 0, 1, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  e <- c(1, 1, 0, 0, 1, 0, 1)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d, "e" = e)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = 1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list(fname = paste(tempfile(), "run", sep = ""), "surv_curv" = TRUE, "strat_haz" = TRUE, "strat_col" = "e", "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox_Strata(a, b, c, e) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    # expect_no_error(ep <- plot(e, df, plot_options)$stratified_survival)
    expect_no_error(ep <- plotSurvival(e, df, plot_options, verbose = 0)$stratified_survival)
    expect_equal(ep$surv[2], 0.5999075, tolerance = 1e-4)
  }
})

test_that("Coxph risk no error", {
  a <- c(0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7)
  c <- c(1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9, 1, 2, 1, 1, 2, 1, 2)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    expect_no_error(ep <- plotRisk(e, df, plot_options)$d)
    expect_equal(ep$y[7], 0.5488116, tolerance = 1e-4)
    expect_equal(names(ep), c("x", "y"))
    #
    expect_no_error(ep <- plotRisk(e, df, plot_options, boundary = 1.95)$d)
    expect_equal(ep$"y:lower"[7], 0.01537157, tolerance = 1e-4)
    expect_equal(ep$"y:upper"[7], 19.59424, tolerance = 1e-4)
  }
})

test_that("Coxph risk no error, with interactions", {
  a <- c(0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7)
  c <- c(1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9, 1, 2, 1, 1, 2, 1, 2)
  e <- c(1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d, "e" = e)
  keep_constant <- c(0)
  a_n <- c(-0.1, 0.1, 0.1, 0.2)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "studyid" = "a", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ plinear(d * d, 0) + loglinear(factor(e)), df, control = control, a_n = a_n, norm = "max")
  if (system.file(package = "ggplot2") != "") {
    df_test <- data.table(
      a = c(0, 0, 0, 0, 0, 0, 0, 0),
      b = c(1, 1, 1, 1, 1, 1, 1, 1),
      c = c(0, 0, 0, 0, 0, 0, 0, 0),
      d = c(1, 2, 3, 4, 5, 6, 7, 8),
      e = c(0, 0, 0, 0, 0, 0, 0, 0)
    )
    expect_no_error(ep <- RelativeRisk(e, df_test))
    expect_equal(ep$Risk, 1 + df_test$d * -0.1 + df_test$d * df_test$d * 0.1, tolerance = 1e-3)
  }
})

test_that("Coxph schoenfeld no error", {
  fname <- "MULTI_COV.csv"
  colTypes <- c("double", "double", "integer", "integer", "integer")
  df <- fread(fname, nThread = min(c(detectCores(), 2)), data.table = TRUE, header = TRUE, colClasses = colTypes, verbose = FALSE, fill = TRUE)
  keep_constant <- c(0, 0)
  a_n <- c(0.01, -15)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  setDTthreads(throttle = 8000) # manually setting the throttle higher, so that the filtering doesn't set off valgrind false positive
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "studyid" = "t0", "verbose" = 0)
  e <- CoxRun(Cox(t0, t1, lung) ~ loglinear(a, b, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    expect_error(plotSchoenfeld(e, df, plot_options, verbose = 0, wrongname = T))
    expect_no_error(ep <- plotSchoenfeld(e, df, plot_options, verbose = 0)$b)
    expect_equal(ep$y[16], 9.999993e-01, tolerance = 1e-4)
  }
  setDTthreads(throttle = 1024) # returning to default
})
test_that("Coxph martingale no error", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  e <- c(1, 1, 2, 2, 3, 3, 3)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d, "e" = e)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "martingale" = TRUE, "cov_cols" = "d", "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "e", "verbose" = 0)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    expect_error(plotMartingale(e, df, plot_options, verbose = 0, wrongname = T))
    expect_no_error(ep <- plotMartingale(e, df, plot_options, verb = 0)$d)
    expect_equal(ep$res_sum[2], -0.2262094, tolerance = 1e-4)
  }
})
test_that("Coxph martingale combinations", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(0, 1, 0, 0, 0, 1, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  e <- c(1, 1, 2, 2, 3, 3, 3)
  f <- c(1, 2, 3, 4, 5, 6, 7)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d, "e" = e, "f" = f)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "martingale" = TRUE, "cov_cols" = "d", "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "Not_In", "verbose" = 0)
  keep_constant <- c(1)
  options(warn = -1)
  if (system.file(package = "ggplot2") != "") {
    e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
    expect_error(plotMartingale(e, df, plot_options))
    keep_constant <- c(0, 0)
    a_n <- c(-0.1, 0.1)
    plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "martingale" = TRUE, "cov_cols" = "d", "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "f", "verbose" = 0)
    e <- CoxRun(Cox(a, b, c) ~ loglinear(d, CONST, 0), df, control = control, a_n = a_n)
    expect_no_error(plotMartingale(e, df, plot_options))
    plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "martingale" = TRUE, "cov_cols" = "Not_In", "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "f", "verbose" = 0)
    e <- CoxRun(Cox(a, b, c) ~ loglinear(d, CONST, 0), df, control = control, a_n = a_n)
    expect_error(plotMartingale(e, df, plot_options))
    plot_options <- list("fname" = paste(tempfile(), "run", sep = ""), "martingale" = TRUE, "cov_cols" = "d", "surv_curv" = FALSE, "strat_haz" = FALSE, "smooth_haz" = FALSE, "studyid" = "f", "verbose" = 0)
    e <- CoxRun(Cox(a, b, c) ~ loglinear(d, CONST, 0), df, control = control, a_n = a_n)
    df$c <- rep(0, nrow(df))
    expect_error(plotMartingale(e, df, plot_options))
  }
  options(warn = 0)
})
test_that("Coxph km no error", {
  a <- c(0, 1, 2, 3, 4, 5, 6)
  b <- c(1, 2, 3, 4, 5, 6, 7)
  c <- c(1, 0, 1, 0, 1, 0, 0)
  d <- c(3, 4, 5, 6, 7, 8, 9)
  df <- data.table("a" = a, "b" = b, "c" = c, "d" = d)
  keep_constant <- c(0)
  a_n <- c(-0.1)
  control <- list("ncores" = 1, "lr" = 0.75, "maxiter" = -1, "halfmax" = 5, "epsilon" = 1e-9, "deriv_epsilon" = 1e-9, "step_max" = 1.0, "change_all" = TRUE, "thres_step_max" = 100.0, "verbose" = 0, "ties" = "breslow")
  plot_options <- list(fname = paste(tempfile(), "run", sep = ""), "surv_curv" = TRUE, "studyid" = "a", "verbose" = 0, "km" = TRUE)
  e <- CoxRun(Cox(a, b, c) ~ loglinear(d, 0), df, control = control, a_n = a_n)
  if (system.file(package = "ggplot2") != "") {
    # expect_no_error(ep <- plot(e, df, plot_options)$`kaplin-meier`)
    expect_no_error(ep <- plotSurvival(e, df, plot_options)$`kaplin-meier`)
    expect_equal(ep$n_t[3], 0.7142857, tolerance = 1e-4)
  }
})
