Debian Codegraph @

tests/testthat/test-mfx.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("modeltests")
4 library(modeltests)
5
6 skip_if_not_installed("mfx")
7 suppressPackageStartupMessages(library(mfx))
8
9 # simulate data
10 set.seed(12345)
11 n <- 1000
12 x <- rnorm(n)
13 # beta outcome
14 y_beta <- rbeta(n, shape1 = plogis(1 + 0.5 * x), shape2 = (abs(0.2 * x)))
15 y_beta <- (y_beta * (n - 1) + 0.5) / n # Smithson and Verkuilen correction
16 # binary outcome (logit and probit)
17 y_bin <- ifelse(pnorm(1 + 0.5 * x + rnorm(n)) > 0.5, 1, 0)
18 # negative binomial outcome (negative binomial and poisson)
19 y_negbin <- rnegbin(n, mu = exp(1 + 0.5 * x), theta = 0.5)
20 df <- data.frame(y_beta, y_bin, y_negbin, x)
21
22 # fit the different models
23 fit_betamfx <- betamfx(y_beta ~ x | x, data = df)
24 fit_logitmfx <- logitmfx(y_bin ~ x, data = df)
25 fit_negbinmfx <- negbinmfx(y_negbin ~ x, data = df)
26 fit_poissonmfx <- poissonmfx(y_negbin ~ x, data = df)
27 fit_probitmfx <- logitmfx(y_bin ~ x, data = df)
28
29
30 # Now run the tests
31
32 test_that("mfx tidier arguments", {
33 # logit
34 check_arguments(tidy.mfx)
35 check_arguments(glance.mfx)
36 check_arguments(augment.mfx)
37 })
38
39 test_that("tidy.mfx", {
40 # beta
41 td_betamfx <- tidy(fit_betamfx, conf.int = TRUE)
42 check_tidy_output(td_betamfx)
43 check_dims(td_betamfx, 1, 8)
44 # logit
45 td_logitmfx <- tidy(fit_logitmfx, conf.int = TRUE)
46 check_tidy_output(td_logitmfx)
47 check_dims(td_logitmfx, 1, 8)
48 # negbin
49 td_negbinmfx <- tidy(fit_negbinmfx, conf.int = TRUE)
50 check_tidy_output(td_negbinmfx)
51 check_dims(td_negbinmfx, 1, 8)
52 # poisson
53 td_poissonmfx <- tidy(fit_poissonmfx, conf.int = TRUE)
54 check_tidy_output(td_poissonmfx)
55 check_dims(td_poissonmfx, 1, 8)
56 # probit
57 td_probitmfx <- tidy(fit_probitmfx, conf.int = TRUE)
58 check_tidy_output(td_probitmfx)
59 check_dims(td_probitmfx, 1, 8)
60 })
61
62 test_that("glance.mfx", {
63 # beta
64 gl_betamfx <- glance(fit_betamfx)
65 check_glance_outputs(gl_betamfx)
66 # logit
67 gl_logitmfx <- glance(fit_logitmfx)
68 check_glance_outputs(gl_logitmfx)
69 # negbin
70 suppressWarnings(
71 gl_negbinmfx <- glance(fit_negbinmfx)
72 )
73 check_glance_outputs(gl_negbinmfx)
74 # poisson
75 gl_poissonmfx <- glance(fit_poissonmfx)
76 check_glance_outputs(gl_poissonmfx)
77 # probit
78 gl_probitmfx <- glance(fit_probitmfx)
79 check_glance_outputs(gl_probitmfx)
80 })
81
82 test_that("augment.mfx", {
83 # beta
84 check_augment_function(
85 aug = augment.betamfx,
86 model = fit_betamfx,
87 data = df,
88 newdata = df
89 )
90 # logit
91 check_augment_function(
92 aug = augment.logitmfx,
93 model = fit_logitmfx,
94 data = df,
95 newdata = df
96 )
97 # negbin
98 check_augment_function(
99 aug = augment.negbinmfx,
100 model = fit_negbinmfx,
101 data = df,
102 newdata = df
103 )
104 # poisson
105 check_augment_function(
106 aug = augment.poissonmfx,
107 model = fit_poissonmfx,
108 data = df,
109 newdata = df
110 )
111 # probit
112 check_augment_function(
113 aug = augment.probitmfx,
114 model = fit_probitmfx,
115 data = df,
116 newdata = df
117 )
118 })