`posterior_survfit` should return covariate data alongside the predictions
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 401
- Forks
- 136
- PR merge metrics
- No merged PRs in 30d
Description
Summary:
For stan_surv models the id column in the data frame returned by posterior_survfit() represents the row of the covariate data that the predictions "belong" to. But the id column can be confusing, especially when the name id was used for one of the variables (e.g. the grouping factor) in the model itself.
The more explicit thing might be to return all of the covariate data used in the predictions as part of the data returned by posterior_survfit; that way the user knows exactly the covariate values that the row of predictions belongs to. Rather than having to infer it based on some "id" (i.e. row identifier) column.
This is essentially what tripped up one user here.
Reproducible Steps:
Here we fit a model with a grouping factor called id with values 5:10, then we predict survival probabilities for the groups that had id values 5 and 6
library(rstanarm)
data <- data.frame(
trt = rep(c("A", "B"), each = 15),
id = rep(5:10, each = 5),
status = sample(0:1, 30, replace = TRUE),
days = sample(10, 30, replace = TRUE))
mod = stan_surv(
formula = Surv(days, status) ~ trt + (1 | id),
data = data,
basehaz = "weibull",
chains = 1,
iter = 20,
cores = 1)
posterior_survfit(
mod,
type = "surv",
newdata = data[1:10,],
times = 1,
extrapolate = FALSE)
which returns:
stan_surv predictions
num. individuals: 10
prediction type: event free probability
standardised?: no
conditional?: no
id cond_time time median ci_lb ci_ub
1 1 NA 1.0000 0.9876 0.9716 0.9950
2 2 NA 1.0000 0.9876 0.9716 0.9950
3 3 NA 1.0000 0.9876 0.9716 0.9950
4 4 NA 1.0000 0.9876 0.9716 0.9950
5 5 NA 1.0000 0.9876 0.9716 0.9950
6 6 NA 1.0000 0.9852 0.9684 0.9936
7 7 NA 1.0000 0.9852 0.9684 0.9936
8 8 NA 1.0000 0.9852 0.9684 0.9936
9 9 NA 1.0000 0.9852 0.9684 0.9936
10 10 NA 1.0000 0.9852 0.9684 0.9936
but we see that the id column in the predictions has values 1 through 10. This is because it is referring to rows 1 through 10 of the prediction covariate data, not the values for the id variable in the original model! It would be clearer / safer if we just returned all the covariates, including id, and then we wouldn't even need a row identifier.
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 at the posterior_survfit() entry point and trace how newdata becomes the returned prediction data frame. The completed change should expose the covariate values used for each prediction, including a model variable named id, so the reproducible example no longer depends on an ambiguous row-number id.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100