stan-dev / stan-dev/projpred

Error for `as.matrix()` applied to GAMM projection

Open
#131 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

additive bug
Dominant language
R
Stars
114
Forks
31
PR merge metrics
No merged PRs in 30d

Description

Applying as.matrix() to a GAMM projection does not work:

# Source: Example section of `?rstanarm::stan_gamm4`.
library(projpred)
library(rstanarm)
options(mc.cores = parallel::detectCores(logical = FALSE))
dat <- mgcv::gamSim(1, n = 400, scale = 2) ## simulate 4 term additive truth
## Now add 20 level random effect`fac'...
dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * .5
br <- stan_gamm4(y ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac),
                 chains = 1,
                 iter = 500, # for example speed
                 seed = 1140350788)

myproj <- project(br,
                  solution_terms = c("x1", "s(x2)", "(1 | fac)"),
                  nclusters = 2)
myprjmat <- as.matrix(myproj)

On branch develop (for example), that last line raises the following error:

Error in names(population_effects) <- replace_intercept_name(names(population_effects)) :
  attempt to set an attribute on NULL

The issue is probably that as.matrix.lm() (which is used for submodels of class "gamm4") uses coef() which in turn is NULL for objects of class "gamm4":

coef(myproj$sub_fit[[1]])
## --> NULL

So for submodels of class "gamm4", the as.matrix.lm() method probably can't be used.

Contributor guide

Open the contributing guide

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 example from ?rstanarm::stan_gamm4 and start at as.matrix(myproj), tracing into as.matrix.lm and coef(myproj$sub_fit[[1]]). Compare the GAMM submodel path with the failing names(population_effects) assignment. Done means as.matrix(myproj) completes for the shown GAMM projection without the NULL-related error.

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
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.