| 1 |
skip_on_cran() |
| 2 |
|
| 3 |
# Matrix ABI version may differ (#1204) |
| 4 |
skip_if_not_r_version("4.4.0") |
| 5 |
|
| 6 |
skip_if_not_installed("modeltests") |
| 7 |
library(modeltests) |
| 8 |
|
| 9 |
skip_if_not_installed("lsmeans") |
| 10 |
suppressPackageStartupMessages(library(lsmeans)) |
| 11 |
|
| 12 |
skip_if_not_installed("lme4") |
| 13 |
suppressPackageStartupMessages(library(lme4)) |
| 14 |
|
| 15 |
fit <- lm(sales1 ~ price1 + price2 + day + store, data = oranges) |
| 16 |
rg <- ref.grid(fit) |
| 17 |
|
| 18 |
marginal <- lsmeans(rg, ~day) |
| 19 |
|
| 20 |
marginal_summary <- summary(marginal, infer = TRUE) |
| 21 |
joint_tests_summary <- joint_tests(fit) |
| 22 |
|
| 23 |
# generate dataset with dashes |
| 24 |
marginal_dashes <- tibble( |
| 25 |
y = rnorm(100), |
| 26 |
x = rep(c("Single", "Double-Barrelled"), 50) |
| 27 |
) |
| 28 |
marginal_dashes <- lm(y ~ x, data = marginal_dashes) |
| 29 |
marginal_dashes <- lsmeans::lsmeans(marginal_dashes, ~x) |
| 30 |
marginal_dashes <- lsmeans::contrast(marginal_dashes, "pairwise") |
| 31 |
|
| 32 |
test_that("lsmeans tidier arguments", { |
| 33 |
check_arguments(tidy.lsmobj, strict = FALSE) |
| 34 |
check_arguments(tidy.ref.grid) |
| 35 |
check_arguments(tidy.emmGrid) |
| 36 |
}) |
| 37 |
|
| 38 |
test_that("tidy.lsmobj", { |
| 39 |
tdm <- tidy(marginal) |
| 40 |
tdmd <- tidy(marginal_dashes) |
| 41 |
tdc <- tidy(contrast(marginal, method = "pairwise")) |
| 42 |
|
| 43 |
check_tidy_output(tdm, strict = FALSE) |
| 44 |
check_tidy_output(tdmd, strict = FALSE) |
| 45 |
check_tidy_output(tdc, strict = FALSE) |
| 46 |
|
| 47 |
check_dims(tdm, 6, 6) |
| 48 |
check_dims(tdmd, 1, 8) |
| 49 |
check_dims(tdc, 15, 8) |
| 50 |
|
| 51 |
tdm <- tidy(marginal, conf.int = TRUE) |
| 52 |
tdmd <- tidy(marginal_dashes, conf.int = TRUE) |
| 53 |
tdc <- tidy(contrast(marginal, method = "pairwise"), conf.int = TRUE) |
| 54 |
|
| 55 |
check_dims(tdm, 6, 8) |
| 56 |
check_dims(tdmd, 1, 10) |
| 57 |
check_dims(tdc, 15, 10) |
| 58 |
}) |
| 59 |
|
| 60 |
test_that("ref.grid tidiers work", { |
| 61 |
td <- tidy(rg) |
| 62 |
check_tidy_output(td, strict = FALSE) |
| 63 |
check_dims(td, 36, 9) |
| 64 |
|
| 65 |
td <- tidy(rg, conf.int = TRUE) |
| 66 |
check_dims(td, 36, 11) |
| 67 |
}) |
| 68 |
|
| 69 |
test_that("summary_emm tidiers work", { |
| 70 |
tdm <- tidy(marginal, conf.int = TRUE) |
| 71 |
tdms <- tidy(marginal_summary) |
| 72 |
|
| 73 |
expect_identical(tdm, tdms) |
| 74 |
|
| 75 |
tdjt <- tidy(joint_tests_summary) |
| 76 |
check_tidy_output(tdjt) |
| 77 |
check_dims(tdjt, 4, 5) |
| 78 |
|
| 79 |
glmm <- glmer( |
| 80 |
cbind(incidence, size - incidence) ~ period + (1 | herd), |
| 81 |
data = cbpp, |
| 82 |
family = binomial |
| 83 |
) |
| 84 |
emm_glmm <- emmeans(glmm, ~period) |
| 85 |
tdm <- tidy(emm_glmm, conf.int = TRUE) |
| 86 |
|
| 87 |
check_tidy_output(tdm[, -1]) |
| 88 |
}) |
| 89 |
|
| 90 |
test_that("tidy.ref.grid consistency with tidy.TukeyHSD", { |
| 91 |
amod <- aov(breaks ~ wool + tension, data = warpbreaks) |
| 92 |
td_hsd <- tidy(TukeyHSD(amod, "tension")) |
| 93 |
|
| 94 |
td_pairs <- lsmeans(amod, ~tension) |> |
| 95 |
pairs(reverse = TRUE) |> |
| 96 |
tidy(conf.int = TRUE) |> |
| 97 |
dplyr::select(-statistic, -df, -std.error) |> |
| 98 |
mutate(contrast = gsub(" ", "", contrast)) |
| 99 |
|
| 100 |
expect_equal( |
| 101 |
as.data.frame(td_hsd), |
| 102 |
as.data.frame(td_pairs) |
| 103 |
) |
| 104 |
}) |
| 105 |
|
| 106 |
test_that("tidy.ref.grid consistency with tidy.glht", { |
| 107 |
pigs.aov <- aov(log(conc) ~ source, data = pigs) |
| 108 |
pigs.emm.s <- emmeans(pigs.aov, "source") |
| 109 |
|
| 110 |
pigs.emm_c <- contrast( |
| 111 |
pigs.emm.s, |
| 112 |
list(lambda1 = c(1, 2, 0), lambda2 = c(0, 3, -2)), |
| 113 |
offset = c(-7, 1), |
| 114 |
adjust = "none" |
| 115 |
) |
| 116 |
|
| 117 |
td_emm <- tidy(pigs.emm_c) |> |
| 118 |
dplyr::select(-df) |
| 119 |
|
| 120 |
pigs.aov <- aov(log(conc) ~ 0 + source, data = pigs) |
| 121 |
K <- rbind( |
| 122 |
c(1, 2, 0), |
| 123 |
c(0, 3, -2) |
| 124 |
) |
| 125 |
rownames(K) <- c("lambda1", "lambda2") |
| 126 |
colnames(K) <- names(coef(pigs.aov)) |
| 127 |
|
| 128 |
aov_glht <- multcomp::glht( |
| 129 |
pigs.aov, |
| 130 |
linfct = multcomp::mcp(source = K), |
| 131 |
rhs = c(7, -1) |
| 132 |
) |
| 133 |
tidy_glht <- tidy(aov_glht, test = multcomp::adjusted("none")) |> |
| 134 |
mutate( |
| 135 |
estimate = estimate - null.value, |
| 136 |
null.value = -null.value |
| 137 |
) |
| 138 |
|
| 139 |
expect_equal( |
| 140 |
as.data.frame(td_emm), |
| 141 |
as.data.frame(purrr::map_dfr(tidy_glht, unname)), |
| 142 |
tolerance = 0.000001 |
| 143 |
) |
| 144 |
}) |
| 145 |
|
| 146 |
test_that("tidy.emmGrid for combined contrasts", { |
| 147 |
noise.lm <- lm(noise ~ size * type * side, data = auto.noise) |
| 148 |
noise.emm <- emmeans(noise.lm, ~ size * side * type) |
| 149 |
noise_c.s <- contrast( |
| 150 |
noise.emm, |
| 151 |
method = "consec", |
| 152 |
simple = "each", |
| 153 |
combine = TRUE, |
| 154 |
adjust = "dunnettx" |
| 155 |
) |
| 156 |
td_noise <- tidy(noise_c.s) |
| 157 |
|
| 158 |
# strict = FALSE needed becasue of factor names and "null.value" column |
| 159 |
check_tidy_output(td_noise, strict = FALSE) |
| 160 |
check_dims(td_noise, 20, 10) |
| 161 |
}) |