unconstrain_pars difficult when model parameters have vectors
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 1.1k
- Forks
- 266
- Avg merge
- 2h 56m
- Merged PRs (30d)
- 1
Description
Summary:
The unconstrain_pars function in rstan is cumbersome to use when the parameters block of the stan model has vectors. When extracting the post-warmup parameters from a stan fit object as a matrix or data frame, and supplying them to unconstrain_pars, it works only when the parameters block does not have vectors.
Description:
When the stan model has vectors in the parameters block, unconstrain_pars does not accept a single row of a parameter array, but only works when given a list. A list of parameters can be extracted from a stan fit object using extract, but the list includes all post-warmup samples and it is cumbersome to then select a single iteration that unconstrain_pars expects. I could not come up with a simple way to extract a list of parameters for just a single HMC iteration.
Reproducible Steps:
The following R code reproduces the problem for me. There are two similar models, but one has vectors in the parameters block and the other does not. The one with vectors produces an error when the parameters are given to unconstrain_pars. I also included a workaround, but it requires manually extracting a single iteration. In addition, the output of the workaround is different, because extract produces a list only if you permute the parameters.
library(rstan)
rstan_options(auto_write = TRUE)
# generate data
n <- as.integer(20)
x <- rnorm(n)
y <- x + rnorm(n)
standata <- list(N = n, x = x,y = y)
good_model_code <- "
data {
int<lower=0> N;
vector[N] x;
vector[N] y;
}
parameters {
real a;
real b;
real<lower=0> sigma;
}
model {
y ~ normal(x * b + a, sigma);
}
"
bad_model_code <- "
data {
int<lower=0> N;
vector[N] x;
vector[N] y;
}
parameters {
vector[2] ab;
real<lower=0> sigma;
}
model {
y ~ normal(x * ab[2] + ab[1], sigma);
}
"
good_fit <- stan(model_code = good_model_code, data = standata)
good_post <- as.matrix(good_fit)
good_unconst <- unconstrain_pars(good_fit, pars = good_post[1,])
bad_fit <- stan(model_code = bad_model_code, data = standata)
bad_post <- as.matrix(bad_fit)
bad_unconst <- unconstrain_pars(bad_fit, pars = bad_post[1,])
#cumbersome workaround
bad_post_list <- extract(bad_fit)
bad_post_list_row <- vector(mode="list")
bad_post_list_row$ab <- bad_post_list$ab[1,]
bad_post_list_row$sigma <- bad_post_list$sigma[1]
bad_unconst2 <- unconstrain_pars(bad_fit, pars = bad_post_list_row)
Current Output:
Error in object@.MISC$stan_fit_instance$unconstrain_pars(pars) :
variable ab missing
Expected Output:
no error
RStan Version:
‘2.18.2’
R Version:
R version 3.4.3 (2017-11-30)
Operating System:
Ubuntu 18.04.2
Contributor guide
No contributing guide indexed for this repository
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.
Research direction
Start by running the reproducible R code and inspecting the unconstrain_pars entry point; the issue provides no source file or test path. Confirm that a single row from as.matrix() works for a model with vector parameters, and add coverage showing the expected no-error behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100