Relabelling helper function for working with factors in rstan
まだ誰も着手していません。
評価
調査の方向性
リポジトリのファイルやテストは指定されていません。まず、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分
- マージ済み PR(30日)
- 1
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
stan-dev/rstantools のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
stan-dev/rstantools#131 · コメント 2 件 ·
-
Use testthat 3e再び着手できるかも @VisruthSK が 80 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
stan-dev/rstantools#159 · リアクション 1 件 · 担当者 1 名 ·
-
Cleanup Vignette再び着手できるかも @VisruthSK が 83 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
stan-dev/rstantools#157 · コメント 1 件 · 担当者 1 名 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 45/100
stan-dev/rstantools#134 · コメント 6 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
stan-dev/rstantools#128 · コメント 1 件 ·
stan-dev/rstantools の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
Typos in schema fileオープン
難易度 1/5 1〜3時間 初心者へのやさしさ 65/100