posterior_survfit.stansurv fails with non-numeric grouping factors
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 401
- Forks
- 136
- PR merge metrics
- No merged PRs in 30d
Description
Summary:
posterior_survfit fails for stan_surv models when predicting for new groups (i.e. levels of the random effect) and the original grouping factor in the model was a character or factor data type, and not numeric.
I think the error is occurring here since the object$xlevs include the grouping factors when they were character variables. This isn't ideal because it then prevents new groups (in that case the code interprets them as new "x levels") from being used. I think any grouping factors need to be removed from objects$levs list, or something like that.
Reproducible Steps:
library(rstanarm)
data <- data.frame(
trt = rep(c("A", "B"), each = 90),
group_numeric = rep(1:6, each = 30),
group_string = rep(letters[1:6], each = 30),
group_factor = factor(rep(letters[1:6], each = 30)),
status = sample(0:1, 180, replace = TRUE),
days = sample(10, 180, replace = TRUE))
mod_numeric = stan_surv(
formula = Surv(days, status) ~ trt + (1 | group_numeric),
data = data,
basehaz = "weibull",
chains = 1,
iter = 100,
cores = 1)
mod_string = update(
mod_numeric,
formula = Surv(days, status) ~ trt + (1 | group_string))
mod_factor = update(
mod_numeric,
formula = Surv(days, status) ~ trt + (1 | group_factor))
nd <- data.frame(
trt = c("A", "B"),
group_numeric = 999, # new value in the prediction data
group_string = "zzz", # new value in the prediction data
group_factor = factor("zzz")) # new value in the prediction data
# works, with warnings about not being factors
ps_numeric = posterior_survfit(
mod_numeric,
newdata = nd,
type = "surv",
times = 0)
# doesn't work, doesn't allow new group levels
ps_string = posterior_survfit(
mod_string,
newdata = nd,
type = "surv",
times = 0)
# doesn't work, doesn't allow new group levels
ps_factor = posterior_survfit(
mod_factor,
newdata = nd,
type = "surv",
times = 0)
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with posterior_survfit and the grouping-level handling in R/pp_data.R around line 374. Run the reproducible example using character and factor grouping variables, comparing it with the numeric case. Trace why grouping levels are treated as x levels, and consider the issue's suggested separation; done means predictions for new non-numeric groups work without the current failure.
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
- 35/100