stan-dev / stan-dev/rstanarm

`stan_glmer()` and `offset()` formula term

Open
#542 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
401
Forks
136
PR merge metrics
No merged PRs in 30d

Description

Summary:

A stan_glmer() fit with offset() in the formula seems to be handled incorrectly in posterior_linpred().

Description:

Unlike a stan_glm() fit, a stan_glmer() fit with offsets specified via offset() in the formula doesn't include the offsets in the linear predictors returned by posterior_linpred(). This might be related to issue #541.

Reproducible Steps:
library(rstanarm)
options(mc.cores = parallel::detectCores(logical = FALSE))
data("kidiq")
kidiq_gr <- within(kidiq, {
  agegr <- cut(mom_age,
               breaks = unique(quantile(mom_age)),
               include.lowest = TRUE)
  levels(agegr) <- paste0("lvl", seq_len(nlevels(agegr)))
})
set.seed(3492)
kidiq_gr$offs_col <- rnorm(nrow(kidiq))
glmm_fit <- stan_glmer(kid_score ~ mom_iq + (1 | agegr) + offset(offs_col),
                       data = kidiq_gr,
                       seed = 734572)
glmm_pl <- posterior_linpred(glmm_fit)

glmm_drws <- as.matrix(glmm_fit)
glmm_pl_man <- glmm_drws[, "(Intercept)"] +
  glmm_drws[, "mom_iq", drop = FALSE] %*%
  t(kidiq_gr[, "mom_iq", drop = FALSE]) +
  glmm_drws[, paste0("b[(Intercept) agegr:", kidiq_gr$agegr, "]"), drop = FALSE]
all.equal(unname(glmm_pl), unname(glmm_pl_man), tolerance = 1e-15)

with that last line returning TRUE, so the offsets are not included in the linear predictors. In contrast, for stan_glm(), the offsets are included in the linear predictors:

glm_fit <- stan_glm(kid_score ~ mom_iq + offset(offs_col),
                    data = kidiq_gr,
                    seed = 734572)
glm_pl <- posterior_linpred(glm_fit)

glm_drws <- as.matrix(glm_fit)
glm_pl_man <- glm_drws[, "(Intercept)"] +
  glm_drws[, "mom_iq", drop = FALSE] %*%
  t(kidiq_gr[, "mom_iq", drop = FALSE]) +
  matrix(kidiq_gr$offs_col,
         nrow = nrow(glm_drws),
         ncol = nrow(kidiq_gr),
         byrow = TRUE)
all.equal(unname(glm_pl), unname(glm_pl_man), tolerance = 1e-15)

(with that last line returning TRUE).

RStanARM Version:

2.21.2 (from https://mc-stan.org/r-packages/)

R Version:

4.1.0

Operating System:

Ubuntu 20.04.2

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the supplied kidiq example, comparing posterior_linpred() for stan_glmer() and stan_glm() with offset() terms. Trace how posterior_linpred() constructs linear predictors for each fit type; done means the stan_glmer() result includes the formula offset and no longer matches the manually computed version that omits it.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.