Skip to content

Commit eff2af1

Browse files
authored
Merge branch 'main' into rc_0_24_0
2 parents 0999769 + 73f86bc commit eff2af1

File tree

6 files changed

+139
-4
lines changed

6 files changed

+139
-4
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
## Bug fixes
1818

19+
* Fixed bug in `p_value()` for objects of class `averaging`.
20+
1921
* Fixed bug when extracting 'pretty labels' for model parameters, which could
2022
fail when predictors were character vectors.
2123

R/methods_averaging.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,27 @@ p_value.averaging <- function(model, component = "conditional", ...) {
6666
s <- summary(model)$coefmat.subset
6767
}
6868

69+
# to data frame
70+
s <- as.data.frame(s)
71+
72+
# do we have a p-value column based on t?
73+
pvcn <- which(colnames(s) == "Pr(>|t|)")
74+
# if not, do we have a p-value column based on z?
75+
if (length(pvcn) == 0) {
76+
pvcn <- which(colnames(s) == "Pr(>|z|)")
77+
}
78+
# if not, default to ncol
79+
if (length(pvcn) == 0) {
80+
if (ncol(s) > 4) {
81+
pvcn <- 5
82+
} else {
83+
pvcn <- 4
84+
}
85+
}
86+
6987
.data_frame(
7088
Parameter = .remove_backticks_from_string(params$Parameter),
71-
p = as.vector(s[, 5])
89+
p = as.vector(s[, pvcn])
7290
)
7391
}
7492

tests/testthat/_snaps/averaging.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# MuMIn link functions
2+
3+
Code
4+
print(mp)
5+
Output
6+
Parameter | Log-Odds | SE | 95% CI | z | p
7+
----------------------------------------------------------------
8+
(Intercept) | -1.01 | 0.26 | [-1.51, -0.50] | 3.91 | < .001
9+
var cont | -0.42 | 0.25 | [-0.90, 0.07] | 1.70 | 0.090
10+
var binom [1] | -0.71 | 0.62 | [-1.92, 0.50] | 1.15 | 0.250
11+
Message
12+
13+
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
14+
using a Wald z-distribution approximation.
15+
16+
The model has a log- or logit-link. Consider using `exponentiate =
17+
TRUE` to interpret coefficients as ratios.
18+
19+
# ggpredict, glmmTMB averaging
20+
21+
Code
22+
print(mp)
23+
Output
24+
Parameter | Coefficient | SE | 95% CI | z | p
25+
---------------------------------------------------------------------------------
26+
cond((Int)) | -0.11 | 0.22 | [ -0.55, 0.32] | 0.52 | 0.605
27+
cond(income) | -0.01 | 3.20e-03 | [ -0.02, -0.01] | 4.07 | < .001
28+
zi((Int)) | -23.11 | 17557.33 | [-34434.85, 34388.63] | 1.32e-03 | 0.999
29+
Message
30+
31+
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
32+
using a Wald z-distribution approximation.
33+
34+
# ggpredict, poly averaging
35+
36+
Code
37+
print(mp)
38+
Output
39+
Parameter | Coefficient | SE | 95% CI | z | p
40+
----------------------------------------------------------------------
41+
(Intercept) | 954.50 | 123.60 | [712.26, 1196.75] | 7.72 | < .001
42+
gear | -24.81 | 18.54 | [-61.14, 11.52] | 1.34 | 0.181
43+
mpg | -51.21 | 11.60 | [-73.96, -28.47] | 4.41 | < .001
44+
mpg^2 | 0.79 | 0.26 | [ 0.29, 1.30] | 3.07 | 0.002
45+
am [1] | -30.80 | 32.30 | [-94.11, 32.52] | 0.95 | 0.340
46+
Message
47+
48+
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
49+
using a Wald z-distribution approximation.
50+

tests/testthat/test-averaging.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
skip_on_cran()
2+
3+
skip_if_not_installed("MuMIn")
4+
skip_if_not_installed("withr")
5+
skip_if_not_installed("glmmTMB")
6+
skip_if_not_installed("betareg")
7+
8+
withr::with_options(
9+
list(na.action = "na.fail"),
10+
test_that("MuMIn link functions", {
11+
library(MuMIn) # nolint
12+
set.seed(1234)
13+
dat <- data.frame(
14+
outcome = rbinom(n = 100, size = 1, prob = 0.35),
15+
var_binom = as.factor(rbinom(n = 100, size = 1, prob = 0.2)),
16+
var_cont = rnorm(n = 100, mean = 10, sd = 7),
17+
group = sample(letters[1:4], size = 100, replace = TRUE),
18+
stringsAsFactors = FALSE
19+
)
20+
dat$var_cont <- as.vector(scale(dat$var_cont))
21+
m1 <- glm(
22+
outcome ~ var_binom + var_cont,
23+
data = dat,
24+
family = binomial(link = "logit")
25+
)
26+
out <- MuMIn::model.avg(MuMIn::dredge(m1), fit = TRUE)
27+
mp <- model_parameters(out)
28+
expect_snapshot(print(mp))
29+
})
30+
)
31+
32+
test_that("ggpredict, glmmTMB averaging", {
33+
library(MuMIn) # nolint
34+
data(FoodExpenditure, package = "betareg")
35+
m <- glmmTMB::glmmTMB(
36+
I(food / income) ~ income + (1 | persons),
37+
ziformula = ~1,
38+
data = FoodExpenditure,
39+
na.action = "na.fail",
40+
family = glmmTMB::beta_family()
41+
)
42+
set.seed(123)
43+
dr <- MuMIn::dredge(m)
44+
avg <- MuMIn::model.avg(object = dr, fit = TRUE)
45+
mp <- model_parameters(avg)
46+
expect_snapshot(print(mp))
47+
})
48+
49+
50+
withr::with_options(
51+
list(na.action = "na.fail"),
52+
test_that("ggpredict, poly averaging", {
53+
library(MuMIn)
54+
data(mtcars)
55+
mtcars$am <- factor(mtcars$am)
56+
57+
set.seed(123)
58+
m <- lm(disp ~ mpg + I(mpg^2) + am + gear, mtcars)
59+
dr <- MuMIn::dredge(m, subset = dc(mpg, I(mpg^2)))
60+
dr <- subset(dr, !(has(mpg) & !has(I(mpg^2))))
61+
mod.avg.i <- MuMIn::model.avg(dr, fit = TRUE)
62+
mp <- model_parameters(mod.avg.i)
63+
expect_snapshot(print(mp))
64+
})
65+
)
66+
67+
unloadNamespace("MuMIn")

tests/testthat/test-marginaleffects.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
skip_if_not_installed("marginaleffects", minimum_version = "0.18.0")
2-
skip_if_not_installed("insight", minimum_version = "0.19.9")
1+
skip_if_not_installed("marginaleffects", minimum_version = "1.0.0")
32
skip_if_not_installed("rstanarm")
43

54
test_that("marginaleffects()", {

tests/testthat/test-serp.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
skip_if_not_installed("serp")
2-
skip_if_not_installed("insight", minimum_version = "0.20.0")
32
skip_if_not_installed("withr")
43

54
# make sure we have the correct interaction mark for tests

0 commit comments

Comments
 (0)