tests/testthat/test_sae-overall-function.R

library(SAE)

context("Checks if all the objects in the general sae function body interact in the right way")

surveydata <-  data.frame(a = c(1,2,4,5,6,7), b = c(2,3,4,1,4,1), c= c(2,4,3,1,5,7))
censusdata <-  data.frame(a = c(1,2,6,7,5,1,39,6), b = c(2,3,6,3,4,7,2,8), c= c(2,4,5,1,8,9,6,4))
model <- a ~ b + c
location_survey <- "c"



test_that("the function works", {
  expect_equal(length(ELLsae(model, surveydata , censusdata, location_survey, n_boot = 5, welfare.function = identity)),8)

})


test_that("different welfare functions can be introduced", {
  expect_equal(length(ELLsae(model, surveydata , censusdata, location_survey, n_boot = 5, welfare.function = function(x){2*x})),8)
  expect_equal(length(ELLsae(model, surveydata , censusdata, location_survey, n_boot = 5, welfare.function = function(x){log(x^2 + 2)})),8)
})



context("Testing whether the function handles inputs the way it is supposed to be")


test_that("the function handles inputs for surveydata correctly", {
  expect_equal(length(ELLsae(model, as.matrix(surveydata) , censusdata, location_survey, n_boot = 5, welfare.function = identity)),8)
  expect_error(length(ELLsae(model = model, censusdata = censusdata,
                             location_survey = location_survey, n_boot = 5)),
                "Data frame with the surveydata is missing")
})

test_that("the function handles inputs for censusdata correctly", {
  expect_equal(length(ELLsae(model, surveydata , as.matrix(censusdata), location_survey, n_boot = 5, welfare.function = identity)),8)
  expect_error(length(ELLsae(model = model, surveydata = surveydata,
                             location_survey = location_survey, n_boot = 5)),
               "Data frame with the censusdata is missing")
})

test_that("the function handles different inputs for locations correctly", {
  # input: string = variable name
  expect_equal(length(ELLsae(model, surveydata , censusdata, location_survey = "c",
                             n_boot = 5)),8)
  # input: numeric location vector
  expect_equal(length(ELLsae(model, surveydata , censusdata,
                             location_survey = c(1,2,3,4,5,6), n_boot = 5)),8)
  # input: character location vector
  expect_equal(length(ELLsae(model, surveydata , censusdata,
                             location_survey = c("a","b","c","d","e","f"), n_boot = 5)),8)
  # input: matrix that can be converted to vector
  expect_equal(length(ELLsae(model, surveydata , censusdata,
                             location_survey = matrix(c(1,2,3,4,5,6), ncol = 1), n_boot = 5)),8)
  # input: location vector missing
  expect_error(length(ELLsae(model, surveydata , censusdata, n_boot = 5)),
               "you have to provide either 1) a vector of locations of length corresponding to the number of observations in the survey data or 2) a string with the name of a variable in the surveydata that provides the locations of individual observations")
  # input: string = not a variable name
  expect_error(length(ELLsae(model, surveydata , censusdata, location_survey = "d",
                             n_boot = 5)),
               "String that was specified as variable name for the location is not the name of one of the variables in the survey data set.")
  # numeric vector of length not corresponding to number of observations
  expect_error(length(ELLsae(model, surveydata , censusdata, location_survey = c(1,2,3),
                             n_boot = 5)),
               "Number of locations provided does not correspond to the number of observations in the survey data set")
})
nikosbosse/SAE documentation built on May 12, 2019, 4:37 a.m.