How to fill in elements within a vector or matrix?

2,808 views
Skip to first unread message

Jaron

unread,
Mar 23, 2017, 1:48:54 PM3/23/17
to stan-...@googlegroups.com
Total STAN novice here, I need help assigning elements to a vector or matrix within my model statement.

(1) Suppose I define a vector in my model statement as:

real vec[3];

I want to assign elements within the vector, but the following does not work:

vec= (a,b,c);

where a,b, and c may be scalars or random variables.  I get a parser error saying it thinks there should be a ")" where the first comma is.

(2)  Now suppose I have a matrix:

matrix[3,3] mat;

In base R, I could easily fill in this matrix as follows: mat=cbind(c(a,b,c),c(d,e,f), c(g,h,i)) where the elements may or may not be scalars... In stan, is there any similar way to fill in the columns of a matrix?


Thanks





Bob Carpenter

unread,
Mar 23, 2017, 2:05:20 PM3/23/17
to stan-...@googlegroups.com
append_col(a, append_col(b, c))

will append three vectors (or matrices) by column.

The next version of Stan (2.15) will let you construct
vector and matrix literals. It'll look like:

real a;
row_vector[3] x = [ 1, 2, a ];
vector[3] xt = [ 1, 2, a ]'; // note transpose
matrix[3, 3] = [ [ 1, 2, 3],
[4, 5, 6],
[7, 8, 9] ];

Arrays use { 1, 2, 3 } instead of [1, 2, 3].

- Bob

> On Mar 23, 2017, at 1:48 PM, Jaron <arbe...@umn.edu> wrote:
>
> Total STAN novice here, I need help assigning elements to a vector or matrix within my model statement.
>
> (1) Suppose a define a vector in my model statement as:
>
> real vec[3];
>
> I want to assign elements within the vector, but the following does not work:
>
> vec= (a,b,c);
>
> where a,b, and c may be scalars or random variables. I get a parser error saying it thinks there should be a ")" where the first comma is.
>
> (2) Now suppose I have a matrix:
>
> matrix[3,3] mat;
>
> In base R, I could easily fill in this matrix as follows: mat=cbind(c(a,b,c),c(d,e,f), c(g,h,i)) where the elements may or may not be scalars... In stan, is there any similar way to fill in the columns of a matrix?
>
>
> Thanks
>
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jaron

unread,
Mar 23, 2017, 3:00:29 PM3/23/17
to stan-...@googlegroups.com
Thanks a lot!  I'm curious, is there anywhere online where you post the progress of future STAN updates? For example, the date of when you expect STAN 2.15 to come out?  I'd like to track future developments of STAN.

Thanks

Bob Carpenter

unread,
Mar 23, 2017, 3:11:22 PM3/23/17
to stan-...@googlegroups.com
The develop branches of our repos always have the latest.
The pull requests have what's in the queue to get added.
And we release when we feel we have enough to justify a release
in the way of bug fixes or features. No target dates because
it's an open-source project being worked on by people with lots
of outside obligations, but we always announce releases on the list.

- Bob

> On Mar 23, 2017, at 3:00 PM, Jaron <arbe...@umn.edu> wrote:
>
> Thanks a lot! I'm curious, is there anywhere online where you post the progress of future STAN updates? For example, the date of when you expect STAN 2.15 to come out? I'd like to track the development of STAN.

Jonah Gabry

unread,
Mar 23, 2017, 8:20:21 PM3/23/17
to Stan users mailing list
And since you seem to be an R user, we have separate GitHub repositories (at github.com/stan-dev) for all of our R packages where you can track their progress. In addition to the rstan package there's

- shinystan: interactive visualizations and tables for MCMC draws
- bayesplot: ggplot2-based plots for plotting parameter draws, diagnostics, and posterior predictive checks
- loo: efficient approximate leave-one-out cross validation
- rstanarm: R formula interface for common applied regression models
- rstantools: stuff for developers of R packages interfacing with Stan (packages like rstanarm, brms and others)

Jonah

Reply all
Reply to author
Forward
0 new messages