File: test-nestedLogit.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (100 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
skip_if_not_installed("nestedLogit")
skip_if_not_installed("broom")
skip_if_not_installed("car")
skip_if_not_installed("carData")

test_that("model_parameters.nestedLogit", {
  data(Womenlf, package = "carData")

  comparisons <- nestedLogit::logits(
    work = nestedLogit::dichotomy("not.work", working = c("parttime", "fulltime")),
    full = nestedLogit::dichotomy("parttime", "fulltime")
  )

  mnl1 <- nestedLogit::nestedLogit(
    partic ~ hincome + children,
    dichotomies = comparisons,
    data = Womenlf
  )

  out <- model_parameters(mnl1)
  expect_identical(
    out$Parameter,
    c(
      "(Intercept)", "hincome", "childrenpresent", "(Intercept)",
      "hincome", "childrenpresent"
    )
  )
  expect_equal(
    out$Coefficient,
    unname(c(coef(mnl1)[, 1], coef(mnl1)[, 2])),
    ignore_attr = TRUE,
    tolerance = 1e-3
  )
  expect_equal(
    out$SE,
    unname(do.call(rbind, lapply(summary(mnl1), coef))[, "Std. Error"]),
    ignore_attr = TRUE,
    tolerance = 1e-3
  )
  expect_equal(
    out$CI_low,
    c(0.60591, -0.08226, -2.16144, 2.11087, -0.18921, -3.80274),
    ignore_attr = TRUE,
    tolerance = 1e-3
  )

  out <- model_parameters(mnl1, ci_method = "wald")
  expect_equal(
    out$CI_low,
    c(0.58367, -0.08108, -2.14847, 1.97427, -0.184, -3.71194),
    ignore_attr = TRUE,
    tolerance = 1e-3
  )

  out <- model_parameters(mnl1, exponentiate = TRUE)
  expect_equal(
    out$Coefficient,
    exp(unname(c(coef(mnl1)[, 1], coef(mnl1)[, 2]))),
    ignore_attr = TRUE,
    tolerance = 1e-3
  )

  out <- model_parameters(mnl1, vcov = "HC3")
  expect_equal(
    out$SE,
    c(0.41738, 0.02256, 0.29565, 0.76467, 0.0373, 0.56165),
    ignore_attr = TRUE,
    tolerance = 1e-3
  )

  out <- model_parameters(mnl1, component = "work")
  expect_identical(nrow(out), 3L)
})


test_that("simulate_parameters.nestedLogit", {
  skip_if(getRversion() < "4.2.0")
  skip_on_os(c("linux", "mac"))

  data(Womenlf, package = "carData")

  comparisons <- nestedLogit::logits(
    work = nestedLogit::dichotomy("not.work", working = c("parttime", "fulltime")),
    full = nestedLogit::dichotomy("parttime", "fulltime")
  )

  mnl1 <- nestedLogit::nestedLogit(
    partic ~ hincome + children,
    dichotomies = comparisons,
    data = Womenlf
  )

  set.seed(123)
  out <- simulate_parameters(mnl1, iterations = 100)
  expect_equal(
    out$Coefficient,
    c(1.35612, -0.04667, -1.59096, 3.45594, -0.10316, -2.69807),
    tolerance = 1e-3
  )
})