concatenating

1,643 views
Skip to first unread message

Andrew Gelman

unread,
Apr 17, 2015, 3:18:44 PM4/17/15
to stan-...@googlegroups.com
Is there any easy way to concatenate several objects in Stan into a single vector?
For example:

vector[K] mu_a;
vector<lower=0>[K] sigma_a;
real<lower=0> sigma_y;

I’d like to do the equivalent of:

vector[2*K+1] all;
all <- c(mu_a, log(sigma_a), log(sigma_y));

But I’m having difficulty doing this except manually:

segment(all, 1, K) <- mu_a;
segment(all, K+1, 2*K) <- log(sigma_a);
segment(all, 2*K+1, 1) <- log(sigma_y);

If that’s the only way to do it, no problem; I’m certainly not saying that it should be a high priority to add a concatenate statement to Stan. But if there already a clean way to concatenate, I’d use it here.

Also I’m confused about the manual because there’s no chapter on vector operations. They seem to be inside the chapter on matrix operations. Maybe it’s a simple matter of changing the name of chapter 34 to Vector and Matrix Operations?

P.S. Actually, the “segment” commands returned an error so I had to _really_ brute-force it:

all[1] <- mu_a[1];
all[2] <- mu_a[2];
all[3] <- log(sigma_a[1]);
all[4] <- log(sigma_a[2]);
all[5] <- log(sigma_y);


Ben Goodrich

unread,
Apr 17, 2015, 3:22:47 PM4/17/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu
On Friday, April 17, 2015 at 3:18:44 PM UTC-4, Andrew Gelman wrote:
Is there any easy way to concatenate several objects in Stan into a single vector?

append_row() works for exactly two vectors but not for scalars

https://github.com/stan-dev/stan/issues/1347

Ben

Andrew Gelman

unread,
Apr 17, 2015, 4:19:25 PM4/17/15
to Ben Goodrich, stan-...@googlegroups.com
Yes, append_row I know about, but if I was going to have to hack the scalar anyway, I figured it would be cleaner to hack the whole thing.

Jonah

unread,
Apr 17, 2015, 6:27:32 PM4/17/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu
On Friday, April 17, 2015 at 3:18:44 PM UTC-4, Andrew Gelman wrote:

P.S.  Actually, the “segment” commands returned an error so I had to _really_ brute-force it:


My guess is that this error you're referring to arises because the R-style replacement

 segment(all, 1, K) <- mu_a 

won't work in Stan. 

For example in R you can do things like

colnames(X) <- whatever

because of the distinction between extraction and replacement methods. 

Bob Carpenter

unread,
Apr 18, 2015, 12:02:55 AM4/18/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu

> On Apr 18, 2015, at 8:27 AM, Jonah <jga...@gmail.com> wrote:
>
> On Friday, April 17, 2015 at 3:18:44 PM UTC-4, Andrew Gelman wrote:
>
> P.S. Actually, the “segment” commands returned an error so I had to _really_ brute-force it:
>
>
> My guess is that this error you're referring to arises because that the R-style replacement
>
> segment(all, 1, K) <- mu_a
>
> won't work in Stan.

Yet --- it's near the top of my to-do list but keeps getting
pushed down.

- Bob

James Savage

unread,
Jan 12, 2016, 10:28:13 AM1/12/16
to Stan users mailing list, gel...@stat.columbia.edu
Bob, 

Was there any progress on this? I'd like to do something like: 

tau <- append_row(tau_raw, 0.0); // where tau_raw is 1 shorter than tau

but keep bumping into errors as append_row is not defined for (vector, real) and I can't do

tau <- append_row(tau_raw, to_vector(0.0));

as to_vector(real) is not a thing. Am I missing an easy solution? 

Jim

Michael Betancourt

unread,
Jan 12, 2016, 12:02:59 PM1/12/16
to stan-...@googlegroups.com
Does rep_vector(0.0, 1) not work?

--
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.

James Savage

unread,
Jan 12, 2016, 12:36:08 PM1/12/16
to Stan users mailing list
Thanks Michael - worked a treat. 

Tran

unread,
Apr 7, 2017, 5:22:38 AM4/7/17
to Stan users mailing list
Hi all,

Do we have something to deal with scalars because when I try

latent_xieta[i, 1 : 2] = append_col(xi_raw[k], eta_raw[k]);

for two scalars it does not work.

Thanks,
Tran.

Bob Carpenter

unread,
Apr 7, 2017, 1:25:30 PM4/7/17
to stan-...@googlegroups.com
It's impossible to help without more context on what the
variable types are that you're trying to manipulate.
All of the available function signatures are listed in the
index of the manual and in the error messages from Stan.

- Bob

Marco Inacio

unread,
Apr 7, 2017, 2:26:58 PM4/7/17
to stan-...@googlegroups.com
I think what Tran actually wants is somekind of anonymous vector/array
creation like:

[scalar_one, scalar_two, scalar_three]

Trung Dung Tran

unread,
Apr 7, 2017, 2:42:51 PM4/7/17
to stan-...@googlegroups.com
Hi,

I follow a discussion from 2015 in this link started with Andrew. My email in this series looks like it open a new discussion so it is difficult to guess my question.

Suppose we have vector L length 2. I want to assign

L=c(3, 5) as in R or L=append(3, 5) but I tried both but failed.

Tran.

On 7 April 2017 at 20:26, Marco Inacio <marcoig...@gmail.com> wrote:
I think what Tran actually wants is somekind of anonymous vector/array creation like:

[scalar_one, scalar_two, scalar_three]



On 07/04/2017 14:24, Bob Carpenter wrote:
It's impossible to help without more context on what the
variable types are that you're trying to manipulate.
All of the available function signatures are listed in the
index of the manual and in the error messages from Stan.

- Bob

On Apr 7, 2017, at 5:22 AM, Tran <trungd...@gmail.com> wrote:

Hi all,

Do we have something to deal with scalars because when I try

latent_xieta[i, 1 : 2] = append_col(xi_raw[k], eta_raw[k]);

for two scalars it does not work.

Thanks,
Tran.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/VSab3Two6UE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

Bob Carpenter

unread,
Apr 10, 2017, 11:31:33 AM4/10/17
to stan-...@googlegroups.com
The index to the manual lists all the functions, so there's
no need to guess. The language will also report all the legal
signatures for a function you try.

The next release (2.15, which is in the pipeline now), will let
you write:

vector[2] L = [3, 5]';

where [3, 5] is a row vector and ' is the postfix transpose operator.

With 2.14, the closest you can get is:

vector[2] L = to_vector({3, 5});

which uses the {3, 5} to construct an int[] and
then to_vector(int[]) to produce a vector.

If we eventually want a flattening operator like R's c(),
that'll be something completely different:

> c(c(1, 2, 3), 4)
[1] 1 2 3 4

> c(c(1, 2), c(3, 4))
[1] 1 2 3 4
>

In Stan, if you do { { 1, 2 }, { 3, 4 } }, you're going to
get a 2 x 2 array.

- Bob




> On Apr 7, 2017, at 2:42 PM, Trung Dung Tran <trungd...@gmail.com> wrote:
>
> Hi,
>
> I follow a discussion from 2015 in this link started with Andrew. My email in this series looks like it open a new discussion so it is difficult to guess my question.
>
> Suppose we have vector L length 2. I want to assign
>
> L=c(3, 5) as in R or L=append(3, 5) but I tried both but failed.
>
> Tran.
>
> On 7 April 2017 at 20:26, Marco Inacio <marcoig...@gmail.com> wrote:
> I think what Tran actually wants is somekind of anonymous vector/array creation like:
>
> [scalar_one, scalar_two, scalar_three]
>
>
>
> On 07/04/2017 14:24, Bob Carpenter wrote:
> It's impossible to help without more context on what the
> variable types are that you're trying to manipulate.
> All of the available function signatures are listed in the
> index of the manual and in the error messages from Stan.
>
> - Bob
>
> On Apr 7, 2017, at 5:22 AM, Tran <trungd...@gmail.com> wrote:
>
> Hi all,
>
> Do we have something to deal with scalars because when I try
>
> latent_xieta[i, 1 : 2] = append_col(xi_raw[k], eta_raw[k]);
>
> for two scalars it does not work.
>
> Thanks,
> Tran.
>
> --
> 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.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/VSab3Two6UE/unsubscribe.
> To unsubscribe from this group and all its topics, 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.
>
>
> --
> 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.
Reply all
Reply to author
Forward
0 new messages