Skip to content

Commit cfba26c

Browse files
authored
Fix find_formula.glmmPQL (#836)
* Fix find_formula.glmmPQL * lintr
1 parent f4e739f commit cfba26c

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: insight
33
Title: Easy Access to Model Information for Various Model Objects
4-
Version: 0.19.7.1
4+
Version: 0.19.7.2
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ S3method(find_formula,gee)
121121
S3method(find_formula,glht)
122122
S3method(find_formula,glimML)
123123
S3method(find_formula,glmm)
124+
S3method(find_formula,glmmPQL)
124125
S3method(find_formula,glmmTMB)
125126
S3method(find_formula,glmmadmb)
126127
S3method(find_formula,gls)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Fixed issue in `get_loglikelihood()` for glm-models with binary outcome, where
66
levels were defined in reversed order.
77

8+
* Fixed issue in `find_formula()` for models of class `glmmPQL` (package *MASS*).
9+
810
# insight 0.19.7
911

1012
## General

R/find_formula.R

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -517,21 +517,21 @@ find_formula.afex_aov <- function(x, verbose = TRUE, ...) {
517517
dv <- attr(x, "dv")
518518
id <- attr(x, "id")
519519

520-
within <- names(attr(x, "within"))
521-
within <- paste0(within, collapse = "*")
522-
within <- paste0("(", within, ")")
523-
e <- paste0("Error(", id, "/", within, ")")
520+
within_variables <- names(attr(x, "within"))
521+
within_variables <- paste0(within_variables, collapse = "*")
522+
within_variables <- paste0("(", within_variables, ")")
523+
e <- paste0("Error(", id, "/", within_variables, ")")
524524

525525
between <- names(attr(x, "between"))
526526
if (length(between) > 0L) {
527527
tempf <- find_formula(x$lm)[[1]]
528528
between <- as.character(tempf)[3]
529529
between <- paste0("(", between, ")")
530530

531-
within <- paste0(c(within, between), collapse = "*")
531+
within_variables <- paste0(c(within_variables, between), collapse = "*")
532532
}
533533

534-
out <- list(conditional = stats::formula(paste0(dv, "~", within, "+", e)))
534+
out <- list(conditional = stats::formula(paste0(dv, "~", within_variables, "+", e)))
535535
class(out) <- c("insight_formula", "list")
536536
out
537537
}
@@ -600,12 +600,10 @@ find_formula.gls <- function(x, verbose = TRUE, ...) {
600600
}
601601

602602
l <- tryCatch(
603-
{
604-
list(
605-
conditional = stats::formula(x),
606-
correlation = stats::as.formula(f_corr)
607-
)
608-
},
603+
list(
604+
conditional = stats::formula(x),
605+
correlation = stats::as.formula(f_corr)
606+
),
609607
error = function(x) {
610608
NULL
611609
}
@@ -1267,6 +1265,16 @@ find_formula.sem <- function(x, verbose = TRUE, ...) {
12671265
#' @export
12681266
find_formula.lme <- function(x, verbose = TRUE, ...) {
12691267
fm <- stats::formula(x$terms)
1268+
.find_formula_nlme(x, fm, verbose = verbose, ...)
1269+
}
1270+
1271+
#' @export
1272+
find_formula.glmmPQL <- function(x, verbose = TRUE, ...) {
1273+
fm <- stats::formula(x)
1274+
.find_formula_nlme(x, fm, verbose = verbose, ...)
1275+
}
1276+
1277+
.find_formula_nlme <- function(x, fm, verbose = TRUE, ...) {
12701278
fmr <- eval(x$call$random)
12711279
if (!is.null(fmr) && safe_deparse(fmr)[1] == "~1") {
12721280
check_if_installed("nlme")
@@ -1364,12 +1372,10 @@ find_formula.BBmm <- function(x, verbose = TRUE, ...) {
13641372
#' @export
13651373
find_formula.mmclogit <- function(x, verbose = TRUE, ...) {
13661374
f <- tryCatch(
1367-
{
1368-
list(
1369-
conditional = stats::formula(x),
1370-
random = stats::as.formula(parse(text = safe_deparse(x$call))[[1]]$random)
1371-
)
1372-
},
1375+
list(
1376+
conditional = stats::formula(x),
1377+
random = stats::as.formula(parse(text = safe_deparse(x$call))[[1]]$random)
1378+
),
13731379
error = function(x) {
13741380
NULL
13751381
}
@@ -1418,12 +1424,10 @@ find_formula.stanreg <- function(x, verbose = TRUE, ...) {
14181424
# special handling for stan_gamm4
14191425
if (inherits(x, "gamm4")) {
14201426
f.random <- tryCatch(
1421-
{
1422-
lapply(.findbars(stats::formula(x$glmod)), function(.x) {
1423-
f <- safe_deparse(.x)
1424-
stats::as.formula(paste0("~", f))
1425-
})
1426-
},
1427+
lapply(.findbars(stats::formula(x$glmod)), function(.x) {
1428+
f <- safe_deparse(.x)
1429+
stats::as.formula(paste0("~", f))
1430+
}),
14271431
error = function(e) {
14281432
NULL
14291433
}
@@ -1484,8 +1488,8 @@ find_formula.MCMCglmm <- function(x, verbose = TRUE, ...) {
14841488
find_formula.BFBayesFactor <- function(x, verbose = TRUE, ...) {
14851489
if (.classify_BFBayesFactor(x) == "linear") {
14861490
fcond <- utils::tail(x@numerator, 1)[[1]]@identifier$formula
1487-
dt <- utils::tail(x@numerator, 1)[[1]]@dataTypes
1488-
frand <- names(dt)[which(dt == "random")]
1491+
dat_types <- utils::tail(x@numerator, 1)[[1]]@dataTypes
1492+
frand <- names(dat_types)[which(dat_types == "random")]
14891493

14901494
if (is_empty_object(frand)) {
14911495
f.random <- NULL
@@ -1816,15 +1820,14 @@ find_formula.model_fit <- function(x, verbose = TRUE, ...) {
18161820
fc <- try(.formula_clean(f[[1]]), silent = TRUE)
18171821
if (inherits(fc, "try-error")) {
18181822
format_error(attributes(fc)$condition$message)
1819-
} else {
1820-
if (verbose) {
1821-
format_warning(paste0(
1822-
"Using `$` in model formulas can produce unexpected results. Specify your model using the `data` argument instead.", # nolint
1823-
"\n Try: ", fc$formula, ", data = ", fc$data
1824-
))
1825-
}
1826-
return(FALSE)
18271823
}
1824+
if (verbose) {
1825+
format_warning(paste0(
1826+
"Using `$` in model formulas can produce unexpected results. Specify your model using the `data` argument instead.", # nolint
1827+
"\n Try: ", fc$formula, ", data = ", fc$data
1828+
))
1829+
}
1830+
return(FALSE)
18281831
}
18291832
return(TRUE)
18301833
}

tests/testthat/test-glmmPQL.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
skip_if_not_installed("nlme")
2+
skip_if_not_installed("MASS")
3+
4+
test_that("find_formula, get_data glmmPQL", {
5+
example_dat <- data.frame(
6+
prop = c(0.2, 0.2, 0.5, 0.7, 0.1, 1, 1, 1, 0.1),
7+
size = c("small", "small", "small", "large", "large", "large", "large", "small", "small"),
8+
x = c(0.1, 0.1, 0.8, 0.7, 0.6, 0.5, 0.5, 0.1, 0.1),
9+
species = c("sp1", "sp1", "sp2", "sp2", "sp3", "sp3", "sp4", "sp4", "sp4"),
10+
stringsAsFactors = FALSE
11+
)
12+
13+
mn <- MASS::glmmPQL(prop ~ x + size,
14+
random = ~ 1 | species,
15+
family = "quasibinomial", data = example_dat
16+
)
17+
expect_identical(find_formula(mn)$conditional, as.formula("prop ~ x + size"))
18+
expect_named(get_data(mn), c("prop", "x", "size", "species"))
19+
})

0 commit comments

Comments
 (0)