Skip to content

Commit

Permalink
fix issue #1501
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Jul 12, 2023
1 parent 865d4f8 commit bc84657
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 35 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: brms
Encoding: UTF-8
Type: Package
Title: Bayesian Regression Models using 'Stan'
Version: 2.19.6
Date: 2023-05-17
Version: 2.19.7
Date: 2023-07-12
Authors@R:
c(person("Paul-Christian", "Bürkner", email = "paul.buerkner@gmail.com",
role = c("aut", "cre")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and incompatible with the newly implemented global shrinkage prior framework.
* No longer support multiple deprecated prior options for categorical and
multivariate models after around 3 years of deprecation. (#1420)
* Deprecate argument `newdata` of `get_refmodel.brmsfit()`. (#1502)
* Disallow binomial models without `trials` argument after several years
of deprecation. (#1501)

### Bug Fixes

Expand Down
34 changes: 10 additions & 24 deletions R/data-response.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,32 +173,18 @@ data_response.brmsterms <- function(x, data, check_response = TRUE,
# data for addition arguments of the response
if (has_trials(x$family) || is.formula(x$adforms$trials)) {
if (!length(x$adforms$trials)) {
if (is_multinomial(x$family)) {
stop2("Specifying 'trials' is required in multinomial models.")
}
trials <- round(max(out$Y, na.rm = TRUE))
if (isTRUE(is.finite(trials))) {
message("Using the maximum response value as the number of trials.")
warning2(
"Using 'binomial' families without specifying 'trials' ",
"on the left-hand side of the model formula is deprecated."
)
} else if (!is.null(basis$trials)) {
trials <- max(basis$trials)
} else {
stop2("Could not compute the number of trials.")
}
} else if (is.formula(x$adforms$trials)) {
trials <- get_ad_values(x, "trials", "trials", data)
if (!is.numeric(trials)) {
stop2("Number of trials must be numeric.")
}
if (any(!is_wholenumber(trials) | trials < 0)) {
stop2("Number of trials must be non-negative integers.")
}
} else {
stop2("Specifying 'trials' is required for this model.")
}
if (!is.formula(x$adforms$trials)) {
stop2("Argument 'trials' is misspecified.")
}
trials <- get_ad_values(x, "trials", "trials", data)
if (!is.numeric(trials)) {
stop2("Number of trials must be numeric.")
}
if (any(!is_wholenumber(trials) | trials < 0)) {
stop2("Number of trials must be non-negative integers.")
}
if (length(trials) == 1L) {
trials <- rep(trials, nrow(data))
}
Expand Down
6 changes: 0 additions & 6 deletions R/make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ standata_basis.brmsterms <- function(x, data, ...) {
}
# old levels are required to select the right indices for new levels
out$levels <- get_levels(tidy_meef(x, data), tidy_ranef(x, data))
if (has_trials(x$family)) {
# trials should not be computed based on new data
datr <- data_response(x, data, check_response = FALSE, internal = TRUE)
# partially match via $ to be independent of the response suffix
out$trials <- datr$trials
}
if (is_binary(x$family) || is_categorical(x$family)) {
y <- model.response(model.frame(x$respform, data, na.action = na.pass))
out$resp_levels <- levels(as.factor(y))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/tests.make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ test_that("make_standata returns correct values for addition terms", {
as.array(c(rep(1:0, 4), 0)))
expect_equal(make_standata(y | cens(c4, y + 2) ~ 1, data = dat)$rcens,
as.array(c(rep(0, 5), dat$y[6:9] + 2)))
sdata <- suppressWarnings(make_standata(s ~ 1, dat, family = "binomial"))
expect_equal(sdata$trials, as.array(rep(9, 9)))
expect_equal(make_standata(s | trials(10) ~ 1, dat,
family = "binomial")$trials,
as.array(rep(10, 9)))
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/tests.posterior_epred.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ test_that("posterior_epred helper functions run without errors", {
expect_equal(dim(SW(posterior_epred(fit, summary = FALSE))), c(ndraws, nobs))

# pseudo binomial model
old_formula <- fit$formula$formula
fit$formula$formula <- update(fit$formula$formula, . | trials(100) ~ .)
fit$autocor <- brms:::cor_empty()
fit$family <- fit$formula$family <- binomial()
expect_equal(dim(SW(posterior_epred(fit, summary = FALSE))), c(ndraws, nobs))

# pseudo beta-binomial model
fit$family <- fit$formula$family <- beta_binomial()
expect_equal(dim(SW(posterior_epred(fit, summary = FALSE))), c(ndraws, nobs))
Expand All @@ -82,6 +84,7 @@ test_that("posterior_epred helper functions run without errors", {
expect_equal(dim(SW(posterior_epred(fit, summary = FALSE))), c(ndraws, nobs))

# pseudo hurdle poisson model
fit$formula$formula <- old_formula
fit$family <- fit$formula$family <- hurdle_poisson()
fit$formula <- bf(count ~ Trt*Age + mo(Exp) + offset(Age) + (1+Trt|visit),
family = family(fit))
Expand Down

0 comments on commit bc84657

Please sign in to comment.