Declaring integers in transformed parameters block

1,458 views
Skip to first unread message

jonny_...@hotmail.com

unread,
Jun 12, 2013, 6:11:23 PM6/12/13
to stan-...@googlegroups.com
Hello All,

Thoroughly enjoying learning to use STAN.  Thanks so much for all of your efforts.

Not a question from me, but noting down a learning experience in the hopes that it might save someone else some time.

I needed to do some data manipulation in the transformed parameters block of my STAN code but couldn't figure out how to do it without delcaring an integer to use for controlling loops and array alocation.

The solution is that integers can be declared locally in the transformed parameters block. Variables are locally declared by inserting an opening curly brace before declaration and a closing curly brace at the end of the program block.

Thanks again,

Jon.

Bob Carpenter

unread,
Jun 13, 2013, 11:11:12 AM6/13/13
to stan-...@googlegroups.com


On 6/12/13 6:11 PM, jonny_...@hotmail.com wrote:
> Hello All,
>
> Thoroughly enjoying learning to use STAN. Thanks so much for all of your efforts.
>
> Not a question from me, but noting down a learning experience in the hopes that it might save someone else some time.

Thanks for sharing.

Any hints on how we could make this behavior clearer in the
manual would be appreciated. If you could share your example,
that might help.

> I needed to do some data manipulation in the transformed parameters block of my STAN code but couldn't figure out how to
> do it without delcaring an integer to use for controlling loops and array alocation.
>
> The solution is that integers can be declared locally in the transformed parameters block. Variables are locally
> declared by inserting an opening curly brace before declaration and a closing curly brace at the end of the program block.

I'm curious as to why you couldn't declare the integers in
the transformed data block? The only reason I can think of is
that they depended on parameters using comparisons/step functions
or floor/ceiling/nearest roundoff.

- Bob

jonny_...@hotmail.com

unread,
Jun 13, 2013, 6:23:48 PM6/13/13
to stan-...@googlegroups.com
Hi Bob,

I am reluctant to offer advice on the manual because a) you all have done an excellent job and I don't want to be in any way critical and b) I haven't read it cover-to-cover so maybe I missed something.  However, a soft suggestion is to put early in part III (Programming Techniques) a list of the different programming blocks, what they are for and what you cannot do in them.  I have picked up these things by looking at the many and excellent examples, but I think I would have learned them faster and more completely if they were explicitly spelled out.

I have just tried to declare the integer in the transformed data block and got this error message at the compile stage:

EXPECTATION FAILURE LOCATION: file=input; line=69, column=4

   cntr<-1;
   ^-- here


DIAGNOSTIC(S) FROM PARSER:
attempt to assign variable in wrong block. left-hand-side variable origin=transformed data

This is not a problem for me because I solved the problem as described above.

I will share my example in a further post...

jonny_...@hotmail.com

unread,
Jun 13, 2013, 7:15:25 PM6/13/13
to stan-...@googlegroups.com

Here is a description of my example.  I am fitting my models on simulated data at this stage.  Once i get a model that is working well enough I will make the attempt to apply it to real data.  The model I am sharing here does not converge (I think too much latency, not enough data) so I have more work ahead of me.  What I have loved about STAN so far is that it has enabled me to fail (so far) so quickly.  Someone else tried a similar model using the EM algorithm and there was weeks of development time and several overnight runs to find out it didn't converge. 

I want to predict the probability of a behaviour (e) displayed by entities.  These entities are controlled by one or more directors.  Each director has some level of desire (d) and opportunity (o) for the entity to carry out the behaviour.  The e, d and o all emit some data x_e, x_d and x_o which I have set up as a regression relationship.  Opportunity for a director (o) depends on desire of that director (d) and the behaviour (e) depends on the opportunity and desire of all directors.  The behaviour (e) is observed as a binary variable (e-hat)  I have attached a word document containing a DAG for this model.

My model assumes vague priors for all of the regression parameters.  I assume a beta(1,1) for d.  I use a beta prior for o where the parameters depend on d.  I use a beta prior for e where the parameters depend on d and o.

I use rstan to fit this model.  My session info is pasted in at the end of this post.

I attach an R-script that generates my data, defines my STAN code and runs the model.

Thanks for your interest.

Jon.

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252    LC_MONETARY=English_New Zealand.1252
[4] LC_NUMERIC=C                         LC_TIME=English_New Zealand.1252   

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] rstan_1.3.0     codetools_0.2-8 Rcpp_0.10.3     inline_0.3.11 

loaded via a namespace (and not attached):
[1] tools_3.0.1




dag.doc
STAN_example.R.txt

jonny_...@hotmail.com

unread,
Jun 13, 2013, 11:45:26 PM6/13/13
to stan-...@googlegroups.com
Just realised that this model is doomed to fail. It is not possible to isolate the effect of d and o (they are multiplied) on e.

Andrew Gelman

unread,
Jun 13, 2013, 11:46:06 PM6/13/13
to stan-...@googlegroups.com
Can be ok if you have proper priors.
--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages