tests/testthat/test-get_vcov.R

test_that("vcov_env works", {

  # ===================
  # Preparation
  # ===================

  # get data
  # ----------------
  set.seed(1)
  data_test <- data.frame(x = runif(10))
  data_test[['y']] <- 3 * data_test[['x']] + rnorm(10, sd = 1)

  # fit model
  # ----------------
  mod_gam <- gamlss::gamlss(y ~ x, data = data_test,
                            control = gamlss::gamlss.control(c.crit = 0.1, trace = FALSE))

  # ===================
  # Tests
  # ===================

  if(exists('data_test', .GlobalEnv)){
    data_test_global <- get('data_test', envir = .GlobalEnv)
    on.exit(assign('data_test', data_test_global, envir = .GlobalEnv))
  }

  # check that original vcov throws an error, indicating
  # that a fix was required
  expect_error(vcov(mod_gam))

  # check that get_vcov function works sucessfully
  vcov_global <- get_vcov(mod_gam, data = data_test,
                          data_name = 'data_test')
  expect_equal(class(vcov_global), 'matrix')

  # check that vcov function resets
  # data_test to what it was before correctly
  data_test_2 <- data_test
  data_test <- 'abc'
  vcov_global <- get_vcov(mod_gam, data = data_test_2,
                          data_name = 'data_test')
  expect_equal(class(vcov_global), 'matrix')
  expect_equal(data_test, 'abc')
})
MiguelRodo/gamlssutils documentation built on July 9, 2020, 12:48 a.m.