stan-dev / stan-dev/rstan

rstan::extract() fails when a parameter is renamed with a space

Open
#509 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

The rstan::extract() function fails when used on a renamed parameter with a space in its name.

Description:

I often rename parameters inside of a stanfit object e.g. replace beta[1-4] with the names of the predictors that the coefficients correspond with. I do this so that when I create a regression table or plot from the stanfit, the result will already have the correct (and meaningful) parameter names. However, the rstan::extract() function fails when used with a parameter that has a custom name with a space in it. For example, if I have renamed a parameter foo bar, then extract(fit, pars = 'foo bar') produces the following error: Error in check_pars(allpars, pars) : no parameter foo bar.

Is this functionality that could be added, or will this never work because you can't name a parameter foo bar directly in a stan model due to the space?

Reproducible Steps:

I'm including an example of this behavior based on the help page for rstan::extract().

 ex_model_code <- '
   parameters {
     real alpha[2,3];
     real beta[2]; 
   } 
   model {
     for (i in 1:2) for (j in 1:3) 
       alpha[i, j] ~ normal(0, 1); 
   for (i in 1:2) 
   beta ~ normal(0, 2); 
   } 
 '

 fit <- stan(model_code = ex_model_code, chains = 4) 

 names(fit)[grep('beta', names(fit))] <- c('Beta1', 'Beta 2')

 extract(fit, pars = 'beta')
 extract(fit, pars = 'Beta1')
 extract(fit, pars = 'Beta 2')
RStan Version:

rstan_2.17.3

R Version:

R version 3.4.4 (2018-03-15)

Operating System:

OS X 10.13.3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible stan model and the three rstan::extract() calls in the issue, then inspect extract()'s parameter-name handling. Done means extracting by a renamed parameter such as "Beta 2" succeeds while the existing beta and Beta1 selections continue to work.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.