Real to Int


#21

I was so hoping it would finally work…

functions {  

  real like0_lpmf(int[,] TDX, real beta, vector gamma, vector phi) {
   int to;
   int dO;
   int x;
   real P;

   to = TDX[1];
   dO = TDX[2];
   x = TDX[3];
   P = 10;
   i = 1;

 return log(P);
}


data {

int<lower=0> N;
int TDX[N, 3];

}

parameters {

  vector [4] <lower=-20,upper=20> gamma;
  real<lower=-20,upper=20> beta;

}

model {

  for(i in 1:N){     

    TDX[i] ~ like0(beta, gamma, 0.9);

 }

beta ~ normal(0,10);
gamma ~ normal(0,10);

}

Gave me the same message as before:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:

dimension mismatch in assignment; variable name = to, num dimensions given = 0; right-hand side dimensions = 1

ERROR at line 12

10: real P;
11:
12: to = TDX[1];
^
13: dO = TDX[2];

PARSER EXPECTED: expression assignable to left-hand side

I also tried int [N, 3] TDX; and int[N, 3] TDX; in the data section.

Thanks again for trying.


How to declare a variable in model block
#22

please try to understand the parser error message, which is exactly right:
you have a scalar variable to.
you have a 2-dim array TDX.
the expression TDX[1] will give you the first element of the 2-dim array. a 2-dim array is an array of 1-dim arrays, therefore the element returned is an array. you cannot assign an array to a scalar.

if you want to access the elements of this 2-dim array of ints, you’d probably want to write some nested loops that would pull out elements TDX[i, j].


#23

I’m so sorry if I frustrated you. I did try and understand but couldn’t, I’m used to coding in R and we don’t have either data types or variable declarations. I really appreciate your patience with me.

This worked!!
int to[1];
int dO[1];
int x[1];

Thanks so much for all of your help!! All of this to get an integer from a real number!!


#24

your problem here is not converting a real to an int, it’s about properly indexing array elements. do you understand what the type of the variable t0 is given declaration to[1] ?

(answer: a 1-dimensional int array of length 1)

in general, when you find yourself doing a lot of work to get a simple result, it’s often the case that you’re going about things the wrong way. and the only way to see this is to step back from the particular problem and read up on general solutions, in this case, data structures and algorithms and the Stan programming language.

FWIW, the solution Bob gave you many messages ago would have worked.

R has both: https://www.tutorialspoint.com/r/r_data_types.htm

the implication that you should be able to just use a real like an int is wrong, given the underlying machinery that both R and Stan are built on (c++). if R does something for you, do you know what it’s doing and why? and if you don’t know that, how can you defend the results it returns?


#25

Hi Mizi,

Thanks for your reflections on this process.

Yes, that is how I came up with it. I RTFM which was so helpful once I understood the error message. The stan manual that I have been referencing is from 2015 and there are limited examples of building your own models, I searched the web til I found some Survival Analysis models by DW Bester, but alas they were continuous not discrete.

I was thinking about that myself last night and this morning, so I hear you and agree with you. And yes, you are right reading about the structures was what helped in the end.

Are you speaking of the while loop? It don’t think it’ll work with the rest of the likelihood, but I’ll double check.

What I wrote isn’t true, is it? Sorry. I should’ve said that R is much more flexible than Stan, in my experience. Ross Ihaka argues that it’s too flexible. At the beginning of this presentation he changes + to - in the middle of a function: https://www.stat.auckland.ac.nz/en/about/news-and-events-5/events/events-2017/03/ihaka-ross-ihaka.html and says that it’s flexibility slows down the language. I don’t imagine this is possible in Stan which might be why it’s faster

I had a conversation with Ross about this once and he told me there is coding in R that works and no one knows how. I’m not willing to publicly disclose his state of mind while writing it.

That’s a question I might pose to Ross next time I talk to him, he’s so good at the big picture, one of the best that I’ve ever encountered.

Many people believe R, but if there are things that its founders don’t know how work perhaps we all shouldn’t? Humpf!

Have you seen the wonderful training materials RStudio has for Shiny? It’s amazing what you can learn just by perusing their site. Another thing I learned from Ross is to look at the examples for functions when reading documentation. When I have a better grasp of Stan, I might have time to help you guys write a lot of examples that might make it easier for people to code their own functions.

All the best,
gwynn


#26

It’s trivial to compute an integer data structure to real.

int a = 5;
real x = a;

You probably don’t ever want to do this:

int to[1];
int dO[1];
int x[1];

because you’re hard-coding one-element arrays. It’d be better to just make them single integers and skip the indexing.

Going from real to int is intentionally hard because it almost always violates the assumptions built into Stan’s sampler, which is that the posterior is continuously differentiable.


#27

We’re always happy to take doc pull requests.

R’s definitely more flexible — it’s a general programming language and Stan’s scope was intentionally very limited. R’s slow because it’s interpreted and because the original authors made some unfortunate choices in data structures and scoping principles (presumably what Ross Ihaka was talking about).

I’ve been less than impressed with the RStudio doc for R markdown, knitr, bookdown, and ggplot2—haven’t looked at Shiny. The main issue I have is that the doc rarely provides simple examples that don’t assume you know the rest of R, they rely heavily on ... notation meaning a lot of what I need isn’t even documented locally, and the typing is always a crap shoot. Even Rstan isn’t consistent in return types from single functions—extract() returns one fewer dimensions if you set the permuted flag than if you don’t!