test_that("choice-experiment WTP is -beta/beta_cost", { b <- c(a = 0.8, b = -0.2, cost = -0.01) V <- diag(c(1e-8, 1e-8, 1e-12)) dimnames(V) <- list(names(b), names(b)) set.seed(1) w <- wtp_estimates(coef = b, vcov = V, cost = "cost") expect_equal(w$estimate, c(80, -20)) expect_equal(w$kr_mean, c(80, -20), tolerance = 1e-2) expect_true(all(w$lower <= w$estimate & w$upper >= w$estimate)) expect_output(print(w), "Krinsky-Robb") }) test_that("dichotomous-choice WTP from glm recovers the truth", { set.seed(3) bid <- sample(c(20, 50, 100, 200, 400), 3000, replace = TRUE) yes <- rbinom(3000, 1, plogis(2 - 0.01 * bid)) m <- glm(yes ~ bid, family = binomial) set.seed(4) w <- wtp_estimates(m, cost = "bid", type = "dichotomous", n_draws = 500) expect_equal(w$estimate, 200, tolerance = 0.1) expect_equal(w$estimate, unname(-coef(m)[1] / coef(m)[2])) }) test_that("wtp input checks", { expect_error(wtp_estimates(coef = c(a = 1), vcov = diag(1), cost = "z")) expect_error(wtp_estimates(cost = "x")) b <- c(bid = -0.01) V <- matrix(1e-6, 1, 1, dimnames = list("bid", "bid")) expect_error(wtp_estimates(coef = b, vcov = V, cost = "bid", type = "dichotomous"), "Intercept") })