Debian Codegraph @

tests/testthat/test-car.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("modeltests")
4 library(modeltests)
5
6 skip_if_not_installed("survival")
7 suppressPackageStartupMessages(library(survival))
8
9 skip_if_not_installed("car")
10
11 test_that("tidy.durbinWatsonTest", {
12 check_arguments(tidy.durbinWatsonTest)
13
14 dw <- car::durbinWatsonTest(lm(mpg ~ wt, data = mtcars))
15 td <- tidy(dw)
16 gl <- glance(dw)
17
18 check_tidy_output(td)
19 check_glance_outputs(gl)
20
21 check_dims(td, 1, 5)
22 })
23
24 test_that("tidy.leveneTest", {
25 skip_if_not_installed("carData")
26
27 mod1 <- with(carData::Moore, leveneTest(conformity, fcategory))
28 mod2 <- with(
29 carData::Moore,
30 leveneTest(conformity, interaction(fcategory, partner.status))
31 )
32 mod3 <- leveneTest(conformity ~ fcategory * partner.status, data = Moore)
33 mod4 <- leveneTest(lm(conformity ~ fcategory * partner.status, data = Moore))
34 mod5 <- leveneTest(
35 conformity ~ fcategory * partner.status,
36 data = Moore,
37 center = mean
38 )
39 mod6 <- leveneTest(
40 conformity ~ fcategory * partner.status,
41 data = Moore,
42 center = mean,
43 trim = 0.1
44 )
45
46 # This is a tidy method, but the model object is very simple and the output
47 # is a 1-row tibble with `df` and `df.residual` columns. `tidy.htest` and
48 # `glance.htest` also return the same things.
49 check_glance_outputs(
50 tidy(mod1),
51 tidy(mod2),
52 tidy(mod3),
53 tidy(mod4),
54 tidy(mod5),
55 tidy(mod6)
56 )
57 })
58
59 test_that("tidy car::Anova glm", {
60 fit <- glm(am ~ mpg, mtcars, family = "binomial")
61 fit2 <- glm(am ~ mpg + wt, mtcars, family = "binomial")
62
63 car_anova <- car::Anova(fit, test.statistic = "LR")
64 car_anova2 <- car::Anova(fit2, test.statistic = "LR")
65
66 td <- tidy(car_anova)
67 td2 <- tidy(car_anova2)
68
69 check_tidy_output(td)
70 check_tidy_output(td2)
71
72 check_dims(td, expected_rows = 1, expected_cols = 4)
73 check_dims(td2, expected_rows = 2, expected_cols = 4)
74 })
75
76 test_that("tidy car::Anova coxph", {
77 fit <- coxph(Surv(time, status) ~ differ, data = colon)
78 fit2 <- coxph(Surv(time) ~ differ, data = colon)
79 fit3 <- coxph(Surv(time) ~ differ + extent, data = colon)
80
81 car_anova_coxph <- car::Anova(fit)
82 car_anova_coxph2 <- car::Anova(fit2)
83 car_anova_coxph3 <- car::Anova(fit3)
84
85 td <- tidy(car_anova_coxph)
86 td2 <- tidy(car_anova_coxph2)
87 td3 <- tidy(car_anova_coxph3)
88
89 check_tidy_output(td)
90 check_tidy_output(td2)
91 check_tidy_output(td3)
92
93 check_dims(td, expected_rows = 2, expected_cols = 5)
94 check_dims(td2, expected_rows = 2, expected_cols = 5)
95 check_dims(td3, expected_rows = 2, expected_cols = 4)
96 })
97
98 test_that("tidy car::linearHypothesis with long formulas (#1171)", {
99 reg_long <-
100 lm(
101 Fertility ~
102 Agriculture + Examination + Education + Catholic + Infant.Mortality,
103 data = swiss
104 )
105
106 test_long <- car::linearHypothesis(
107 reg_long,
108 hypothesis.matrix = c(0, 1, 0, 0, 0, -1)
109 )
110
111 td <- broom::tidy(test_long)
112 check_dims(td, expected_rows = 1, expected_cols = 10)
113 })