tests/testthat/testmarginW.R

library(emmeans)
library(marginC)
library(testthat)

df <- data.frame(Y = rnorm(100),
                 A = rep(c("a1","a2"), each = 50),
                 B = rep(c("b1","b2","b3","b4"), 25))
fit <- aov(Y ~ A*B, data = df)
rg <- emmeans(fit, ~A*B)
rg_bad <- emmeans(fit, ~A)

my_weights <- list(
  c1 = list(A = c(-1,1), B = c(-1,1,0,0)),
  c2 = list(A = c(-1,0), B = c(-1,1,0,0))
)


test_that("get_marginal_weights & mw - no grid", {
  # return a mw object
  expect_is(my_w <- get_marginal_weights(A = c(-1,1), B = c(-1,1,0,0)),'mw')

  # test that mw returns the same as get_marginal_weights
  expect_equal(
    get_marginal_weights(A = c(-1,1), B = c(-1,1,0,0)),
    mw(A = c(-1,1), B = c(-1,1,0,0))
  )

  # can be used in emmeans
  expect_is(contrast(rg, list(my_w)), "emmGrid")
})

test_that("get_marginal_weights & mw - with grid", {
  # return a mw object
  expect_is(mw(A = c(-1,1), B = c(-1,1,0,0), .grid = rg),'mw')

  # test that mw returns the same as get_marginal_weights
  expect_equal(
    get_marginal_weights(A = c(-1,1), B = c(-1,1,0,0), .grid = rg),
    mw(A = c(-1,1), B = c(-1,1,0,0), .grid = rg)
  )

  # bad grid
  expect_error(mw(A = c(-1,1), B = c(-1,1,0,0), .grid = rg_bad))

  # update grid
  expect_warning(update_grid(mw(A = c(-1,1)),rg))
  expect_warning(update_grid(list(mw(A = c(-1,1)),mw(A = c(-1,1))),rg))

  # can be used in emmeans
  expect_is(contrast(rg, list(mw(A = c(-1,1), B = c(-1,1,0,0), .grid = rg))), "emmGrid")
})
mattansb/marginC documentation built on May 28, 2019, 3:39 p.m.