tests/testthat/test-simPregProp.R

test_that("simPregProp returns the expected structure", {
  data.prop <- simPregProp()

  expect_s3_class(data.prop, "preg.prop")
  expect_true(is.data.frame(data.prop))
  expect_named(data.prop, c("GA", "ExpGA", "Outcome", "Prop"))
  expect_true(nrow(data.prop) > 0)
})


test_that("simPregProp uses default hazard vectors", {
  data.prop <- simPregProp()

  expect_true(all(data.prop$GA >= 1 & data.prop$GA <= 301))
  expect_true(all(data.prop$ExpGA < data.prop$GA |
                    is.na(data.prop$ExpGA)))
})


test_that("simPregProp accepts custom hazard vectors", {
  haz <- rep(0.001, 301)

  data.prop <- simPregProp(
    haz.spont.livebirth = haz,
    haz.nonspont.livebirth = haz,
    haz.late.miscarriage.stillbirth = haz,
    haz.exposure = haz
  )

  expect_s3_class(data.prop, "preg.prop")
  expect_true(nrow(data.prop) > 0)
})


test_that("simPregProp rejects hazard vectors of unequal length", {
  expect_error(
    simPregProp(
      haz.spont.livebirth = rep(0.001, 301),
      haz.nonspont.livebirth = rep(0.001, 300)
    ),
    "All hazard vectors must have the same length"
  )
})


test_that("simPregProp rejects hazard vectors of length zero", {
  expect_error(
    simPregProp(
      haz.spont.livebirth = numeric(0),
      haz.nonspont.livebirth = numeric(0),
      haz.late.miscarriage.stillbirth = numeric(0),
      haz.exposure = numeric(0)
    ),
    "Hazard vectors must contain at least two values"
  )
})


test_that("simPregProp replaces NULL hazard ratios with 1", {
  data.null <- simPregProp(
    hr.spont.livebirth = NULL,
    hr.nonspont.livebirth = NULL,
    hr.late.miscarriage.stillbirth = NULL
  )

  data.one <- simPregProp(
    hr.spont.livebirth = rep(1, 301),
    hr.nonspont.livebirth = rep(1, 301),
    hr.late.miscarriage.stillbirth = rep(1, 301)
  )

  expect_identical(data.null, data.one)
})


test_that("simPregProp rejects hazard ratios of unequal length", {
  expect_error(
    simPregProp(
      hr.spont.livebirth = rep(1, 300)
    ),
    "hr.spont.livebirth must have the same length as the hazard vectors"
  )
})


test_that("simPregProp rejects invalid hazard values", {
  expect_error(
    simPregProp(
      haz.spont.livebirth = c(rep(0.001, 300), 1.1)
    ),
    "haz.spont.livebirth must be between 0 and 1"
  )

  expect_error(
    simPregProp(
      haz.spont.livebirth = c(rep(0.001, 300), NA_real_)
    ),
    "haz.spont.livebirth contains missing values"
  )
})


test_that("simPregProp rejects invalid hazard ratios", {
  expect_error(
    simPregProp(
      hr.spont.livebirth = c(rep(1, 300), 0)
    ),
    "hr.spont.livebirth must be > 0"
  )

  expect_error(
    simPregProp(
      hr.spont.livebirth = c(rep(1, 300), NA_real_)
    ),
    "hr.spont.livebirth contains missing values"
  )
})


test_that("simPregProp rejects hazard and hazard ratio products above 1", {
  expect_error(
    simPregProp(
      haz.spont.livebirth = c(rep(0.001, 300), 0.6),
      hr.spont.livebirth = c(rep(1, 300), 2)
    ),
    "haz.spont.livebirth \\* hr.spont.livebirth must be <= 1"
  )
})

Try the simPreg package in your browser

Any scripts or data that you put into this service are public.

simPreg documentation built on Sept. 27, 2026, 5:06 p.m.