tests/testthat/test-dags.R

test_that("Can generate categorical variables with multiple levels.", {
  SIM <- datasim_make(x ~ categorical(n, c("one", "two", "three")))
  DF <- take_sample(SIM, n=99)
  expect_true(all(DF$x %in% c("one", "two", "three")))
})

test_that("Relative probabilities work with categorical generation.", {
  D <- datasim_make(x ~ categorical(n, c(one=1, two=2, three=3)))
  S <- take_sample(D, n=6000)
  # There should be about one-third as many "one"s and "three"s
  # The following are just rough checks but are pretty safe
  expect_true(sum(S$x=="one") < 1500)
  expect_true(sum(S$x=="three") > 2500)
})

test_that("Relative probabilities work with categorical generation.", {
  D <- datasim_make(x ~ categorical(n, c("one", "two", "three"), exact=TRUE))
  S <- take_sample(D, n=6000)
  # There should be about one-third as many "one"s and "three"s
  # The following are just rough checks but are pretty safe
  expect_true(sum(S$x=="one") == 2000)
  expect_true(sum(S$x=="three") == 2000)
})

test_that("rounding works", {
  D <- datasim_make(x ~ round(runif(n, 0,10)))
  S <- take_sample(D, n = 100)
  expect_equal(S$x, round(S$x))
})

test_that("Translation from categorical to numerical works.", {
          D <- datasim_make(
            g ~ categorical(n, c("A", "B", "C")),
            y ~ cat2value(g, A=1, B=2, C=10)
            )
          Samp <- take_sample(D, n=100)
          Summary <- Samp |> dplyr::summarize(m = mean(y), .by = g) |> dplyr::arrange(g)
          expect_equal(Summary$m, c(1,2,10))
         } )

Try the LSTbook package in your browser

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

LSTbook documentation built on April 3, 2025, 6:02 p.m.