stan-dev / stan-dev/rstan

error when calculating unconstrained values for lower triangular matrix parameter

Open
#323 5 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 function ".upars" does not work for parameters defined as lower triangular matrices (e.g. cholesky_factor_corr).

Description:

Choosing the option unconstrain = T when plotting parameters in rstan does not work
when one of the parameters defined in the stan model is a lower triangular matrix.
The reason is that in the function .upars parameter names for the complete matrix are defined,
whereas the function unconstrain_pars returns only the values for the lower triangular matrix.

adding the following lines of code to the .upars function removes the bug:

cpp_code = strsplit(get_cppcode(get_stanmodel(object)), "\n")[[1]]
rms = c()
for (p in pblock) {
  par_mentions = grep(paste0("(",p,")"), 
                                     fixed = TRUE, value = TRUE, 
                                     x = cpp_code)
  if (length(grep("cholesky_corr",par_mentions)) > 0) {
    for (v in grep(p,param_names)) {
      if (diff(as.numeric(strsplit(x = param_names[v],
                                   split = c("\\[|\\]|,"))[[1]][2:3])) > -1) { rms = c(v,rms)}
      }
    }
  }
param_names = param_names[-rms]

This solution checks only for parameters of the type "cholesky_factor_corr" and could of course be extended to other types that are lower triangular or similar.

Reproducible Steps:
  1. Fit any model that includes a parameters defined as a lower triangular matrix.
  2. Try e.g. stan_trace(stanfit, unconstrain = T)
Current Output:

Error message, e.g.:
Error in names(plist) <- param_names :
'names' attribute [781] must be the same length as the vector [430]

Expected Output:

Correct vector of parameter names for unconstrained parameters.

RStan Version:

2.11.1

R Version:

3.3.0

Operating System:

Windows 7 Professional

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 by tracing the .upars function and its use of unconstrain_pars when stan_trace is called with unconstrain = T. Reproduce the mismatch with a model containing a lower triangular parameter, then verify that the generated parameter names match the unconstrained values and that plotting completes without the names-length error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.