Relabelling helper function for working with factors in rstan
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
No repository files or tests are named. Start by reviewing how rstantools handles rstan fit objects and parameter names, then compare the proposed delabel and relabel examples with existing interfaces. Done would require an agreed general-purpose helper design and corresponding validation, but the issue does not define acceptance criteria.
Written by the indexing model from the issue text.
Description
Summary:
When using factor variables (which are have attached meaningful level names), would it be useful/possible to have a function that relabels variables in the Stan fit object with the relevant factor levels?
Description:
When using categorical variables in stan you have to drop the level labels. This is fine and sensible, but with variables that have a number of levels, the output can be difficult to read. Would it be possible to write a helper function that relabels the numeric index with the relevant label for easier use? @jgabry thought it might be, but wasn't sure if it would be too complex to create something that was useful in the more general.
Reproducible Steps:
For example. With the 8 schools model the schools are coded as numbers. However, in real world data, they would often be recorded with meaningful names (here we use states).
schools_df <- data.frame(schools = state.name[1:8],
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
schools_df$schools = as.factor(schools_df$schools)
I am proposing a pair of functions that force the factors to numeric for use in rstan, and then relabels the relevant variables in the stan fit object. e.g. for the simple 8 schools case.
delabel <- function(x){
return(as.numeric(x))
}
relabel <- function(x,stan_fit,variables){
current_names <- names(stan_fit)
match <- cbind(levels(x),levels(as.factor(as.numeric(x))))
for (i in 1:length(variables)){
loc_var <- grepl(paste0("^",variables[i]),current_names)
for(j in 1:nrow(match)){
current_names[loc_var] <- gsub(pattern = match[j,2], replacement = match[j,1],current_names[loc_var])
}
}
names(stan_fit) <- current_names
return(stan_fit)
}
The idea is that is would work something like this:
schools_dat <- list(J = 8,
school = delabel(schools_df$schools),
y = schools_df$y,
sigma =schools_df$sigma)
library(rstan)
fit <- stan(file = '8schools.stan', data = schools_dat)
fit2 <- relabel(schools_df$schools,fit,c('theta','eta'))
colnames(as.matrix(fit2))
library(bayesplot)
mcmc_areas_ridges(as.matrix(fit2, pars = "theta"))
- Dominant language
- R
- Stars
- 48
- Forks
- 24
- Avg merge
- 1h 17m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
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.
More from stan-dev/rstantools
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
stan-dev/rstantools#131 · 2 comments ·
-
Use testthat 3eMay be free again @VisruthSK claimed this 70 days ago, and no pull request is open. Open
stan-dev/rstantools#159 · 1 reaction · 1 assignee ·
-
Cleanup VignetteMay be free again @VisruthSK claimed this 73 days ago, and no pull request is open. Open
stan-dev/rstantools#157 · 1 comment · 1 assignee ·
-
Difficulty 1/5 Under an hour Newbie friendliness 45/100
stan-dev/rstantools#134 · 6 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
stan-dev/rstantools#128 · 1 comment ·
All issues in stan-dev/rstantools
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
weecology/forecasting-course#119 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
OHDSI/CohortSymmetry#369 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100