Relabelling helper function for working with factors in rstan
还没有人认领这个 Issue。
评估
调研方向
未提及任何仓库文件或测试。首先检查 rstantools 如何处理 rstan 拟合对象和参数名称,然后将提议的 delabel 和 relabel 示例与现有接口进行比较。要视为完成,需要就通用 helper 的设计达成一致并进行相应的验证,但该 issue 未定义验收标准。
由索引模型根据 Issue 内容生成。
描述
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"))
- 主要语言
- R
- 星标
- 49
- 派生
- 24
- 平均合并
- 23 小时 46 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
stan-dev/rstantools 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 72/100
stan-dev/rstantools#131 · 2 条评论 ·
-
Use testthat 3e可能重新可做 @VisruthSK 于 86 天前认领,目前没有进行中的 PR。 未关闭
stan-dev/rstantools#159 · 1 个 reaction · 已指派 1 人 ·
-
Cleanup Vignette可能重新可做 @VisruthSK 于 89 天前认领,目前没有进行中的 PR。 未关闭
stan-dev/rstantools#157 · 1 条评论 · 已指派 1 人 ·
-
难度 1/5 1 小时以内 新手友好度 45/100
stan-dev/rstantools#134 · 6 条评论 ·
-
难度 5/5 一周以上 新手友好度 25/100
stan-dev/rstantools#128 · 1 条评论 ·
查看 stan-dev/rstantools 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
HelenaLC/spatialdataR#284 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
mlr-org/mlr3cmprsk#42 ·
维护者通常 1 天内回复
-
Hackathon: Tier 1
难度 2/5 1-3 小时 新手友好度 88/100
pharmaverse/logrx#309 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100