tests/testthat/test_checkBackgroundVariables.R

data(data.timss1)

varGroups <- list(ID = "IDSTUD", wgt = c("TOTWGT","JKZONE"), covars = c("female", "books", "lang", "migant"), depVar = "ASMMAT")

test_that("error occurs due to type", {
  lapply(varGroups, FUN = function (v) {
  if(v[[1]] == "female") {expect_error(existsBackgroundVariables (dat=data.timss1[[1]], variable=v))}
  })})

# correction
varGroups <- list(ID = "IDSTUD", wgt = c("TOTWGT","JKZONE"), covars = c("female", "books", "lang", "migrant"), depVar = "ASMMAT")
varList   <- lapply(varGroups, FUN = function (v) {
    existsBackgroundVariables (dat=data.timss1[[1]], variable=v)
})

# however, only one weighting variable is allowed
test_that("error occurs because obly one wgt variable is allowed", {
   expect_error(checkBackgroundVariables (varList, len1 = c("ID", "wgt", "depVar"), overlap = NULL))
})

# only one of the covariates should be used for group differences
# difference variable must be included in covars
varGroups <- list(ID = "IDSTUD", wgt = "TOTWGT", covars = c("female", "books", "lang"),
             differencesFor = "migrant", depVar = "ASMMAT")
varList   <- lapply(varGroups, FUN = function (v) {
    existsBackgroundVariables (dat=data.timss1[[1]], variable=v)
})

test_that("error occurs because some variable groups must have only one element", {
  expect_error(checkBackgroundVariables (varList, len1 = c("ID", "wgt", "depVar"),overlap = list(v1 = list(vars = c("covars","differencesFor"), len = 1))))
})

Try the eatTools package in your browser

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

eatTools documentation built on Nov. 28, 2026, 5:06 p.m.