Skip to content

Commit

Permalink
fix an issue in validating re terms
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Sep 16, 2024
1 parent 9af7827 commit fa5f1ea
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion R/brmsframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ brmsframe.brmsterms <- function(x, data, frame = NULL, basis = NULL, ...) {
# this must be a multivariate model
stopifnot(is.list(frame))
x$frame <- frame
x$frame$re <- subset2(x$frame$re, resp = x$resp)
}
data <- subset_data(data, x)
x$frame$resp <- frame_resp(x, data = data)
Expand All @@ -51,6 +50,10 @@ brmsframe.brmsterms <- function(x, data, frame = NULL, basis = NULL, ...) {
basis = basis$nlpars[[nlp]], ...
)
}
# If this is a multivariate model, retain only the subset of random effects
# belonging to the current response variable. Subsetting is performed here
# rather than earlier to allow for correct validation of 're' terms in stan_sp.
x$frame$re <- subset2(x$frame$re, resp = x$resp)
class(x) <- c("brmsframe", class(x))
x
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/tests.stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ test_that("monotonic effects appear in the Stan code", {
)
})

test_that("Stan code for re prectiro terms is correct", {
test_that("Stan code for re predictor terms is correct", {
dat <- data.frame(
y = rnorm(100, mean = rep(1:10, each = 10)),
x = rnorm(100), gr = rep(1:10, each = 10)
Expand All @@ -1141,7 +1141,7 @@ test_that("Stan code for re prectiro terms is correct", {
bf(x ~ (1|gr) + re(gr, resp = "y")) +
set_rescor(FALSE)
scode <- make_stancode(bform, dat)
expect_match2(scode,
expect_match2(scode,
"mu_x[n] += (bsp_x[1]) * r_1_y_1[J_1_y[n]] + r_2_x_1[J_2_x[n]] * Z_2_x_1[n];"
)

Expand All @@ -1158,7 +1158,7 @@ test_that("Stan code for re prectiro terms is correct", {

bform <- bf(y ~ x + (1 + x|gr), sigma ~ re(gr, coef = "z"))
expect_error(
make_stancode(bform, dat),
make_stancode(bform, dat),
"Cannot find all varying coefficients"
)
})
Expand Down

0 comments on commit fa5f1ea

Please sign in to comment.