tests/testthat/test-get_coef_tbl.R

test_that("get_coef_tbl works", {

  # =================
  # Model that doesn't use all parameters
  # =================

  set.seed(1)
  data_mod <- data.frame(x = rnorm(20, 5) + rgamma(20, 0.01, 0.0001))
  data_mod[['y']] <- 3 * data_mod[['x']] + rnorm(20, sd = 5)
  mod <- gamlss::gamlss(formula = y ~ -1 +  x,
                        sigma.formula = ~ 1,
                        nu.formula = ~ 1,
                        tau.formula = ~ 1,
                        family = "NO",
                        data = data_mod,
                        control = gamlss::gamlss.control(trace = FALSE))

  coef_tbl <- get_coef_tbl(mod)
  expect_identical(colnames(coef_tbl), c("Variable", "Mu coefficient",
                                         "Sigma coefficient"))
  expect_identical(coef_tbl$Variable, c("Intercept", "x"))
  expect_equivalent(trunc(coef_tbl[["Mu coefficient"]] * 10), c(NA, 28))
  expect_equivalent(trunc(coef_tbl[["Sigma coefficient"]] * 10), c(49, NA))

  # =================
  # Model that uses all parameters
  # =================

  set.seed(2)
  mod <- gamlss::gamlss(formula = y ~ -1 +  x,
                        sigma.formula = ~ 1,
                        nu.formula = ~ 1,
                        tau.formula = ~ 1,
                        family = "BCPE",
                        data = data_mod,
                        control = gamlss::gamlss.control(trace = FALSE))

  coef_tbl <- get_coef_tbl(mod)
  expect_identical(colnames(coef_tbl), c("Variable", "Mu coefficient",
                                         "Sigma coefficient",
                                         'Nu coefficient',
                                         'Tau coefficient'))
  expect_identical(coef_tbl$Variable, c("Intercept", "x"))
  expect_equivalent(trunc(coef_tbl[["Mu coefficient"]] * 10), c(NA, 28))
  expect_equivalent(trunc(coef_tbl[["Sigma coefficient"]] * 100), c(38, NA))
  expect_equivalent(trunc(coef_tbl[["Nu coefficient"]] * 100), c(79, NA))
  expect_equivalent(trunc(coef_tbl[["Tau coefficient"]] * 100), c(84, NA))

  # =================
  # Check that error is thrown when link function not recognised
  # =================

  mod$mu.link <- 'abc'
  expect_error(get_coef_tbl(mod))

})
MiguelRodo/gamlssutils documentation built on July 9, 2020, 12:48 a.m.