Real to Int

Hi there,

I have a situation where I’d like to have a loop for(i in 1:to), in this situation it appears that to must be an int, however, I am inputting to as a vector which assumes that it’s real and results in a mismatch. I tried a while loop: while(ti <= i <= (to-1)) for the function Delta, which also resulted in an error. Any suggestions would be greatly appreciated.

The coding is here: https://github.com/nzgwynn/Amelia MyModel.stan and MyModelRStan.R.

Thanks for your time,
gwynn

instead of a vector, use an array of int.

e.g. don’t use vector[N] foo instead use int foo[N] (and of course you have int N beforehand).

You have to stick to Stan syntax. The while loop would look like this:

real a;
...
{
  int i = 1;
  while (i < a) {
    ... go to town ...
    i = i + 1;
  }
}

The extra layer of brackes is to let you define the variable i as a local variable for the loop.

Thanks Bob and Mitzi.

I updated it to an array, but it’s still not compiling:
Indexed expression must have at least as many dimensions as number of indexes supplied: **
** indexed expression dimensionality = 0; indexes supplied = 1

at line 50 where I have
to = TDX[1];

but if I have it as a function of int TDX[3] it also crashes, int [3] TDX, gives the same error as above.

Thanks for all of your support, I feel so ssssslllloooowwwww.

gwynn

I took a quick read of your program, and I believe the problem has to do with the way that you’ve declared your function arguments. See the Stan Reference manual, section: “Type Declarations for Functions” and the following section on “Array Types for Function Declarations”

Type Declarations for Functions
Function argument and return types are not declared with their sizes. They also may not contain any constraints; see Figure 22.1 for a list.
Unlike type declarations for variables, function type declarations for matrix and vector types are not declared with their sizes.

We can’t debug from fragments. Include a minimal program illustrating what you’re trying to do and we can help you fix that and then you can apply it to your huge program.

Hi Bob and Mitzi,

I hope this example is minimal enough:

functions {
real like0_lpdf(real beta, vector gamma, vector phi, int TDX){
    int to;
    int dO;
    int x;
    real P;

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

    return P;
    }
    

data {

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

}

parameters {

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

}

model {
  
  for(i in 1:N){
    
    TDX[i] ~ like0_lpdf(beta, gamma, 0.9);
    
  }
  
  beta ~ normal(0,10);
  gamma ~ normal(0,10);

}

I read the suggested section of the Stan manual but got the same error when I changed thing. Any suggestions would be appreciated.

Thanks again,
gwynn

[Edit: code formatted as code]

your function argument declaration needs to be:
int TDX[]

the parser error you’re getting is saying that TDX is a scalar, and the number of dimensions for a scalar is 0, so the statment to = TDX[1] is wrong, because as far as the parser knows, TDX has 0 not 1 dimensions.

Hi Mitzi,

Thanks for your help. For some reason I got this error:

ERROR at line 6

4: ## Definitions
5: ##################
6: real like0_lpdf(real beta, vector gamma, vector phi, int TDX[]){
^
7: int to;

PARSER EXPECTED: <argument declaration or close paren ) to end argument declarations>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘MyBrokenModel’ due to the above error.

when I ran this:

functions {

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

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

  return P;
 }

data {

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

}

parameters {

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

}

model {

  for(i in 1:N){

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

 }

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

}

I must be very close…

gwynn

For some reason the error didn’t go through so I’ll try again:

PARSER EXPECTED: argument declaration or close paren ) to end argument declarations

Hey Gwynn

There’s a few problems that you’re gonna hit. I’ll just edit your model and leave comments inside it.

functions {
  // The "data" (there might be a special name for this but I dunno) argument for the _lpmf/_lpdf functions comes first.
  // I think TDX is the data in your case?
  // Also, TDX is an integer, so it's probably a pmf, not a pdf that you want
  real like0_lpmf(int TDX, real beta, vector gamma, vector phi) {
    ...
    return log(probability); // The lpmf/lpdf return logs of the probabilities they compute
}

data {
  int N;
  int TDX[3]; // Should TDX be 3 dimensional? I ask because later you have a loop over
                    //    TDX that goes all the way to N. Is it N, 3 dimensional things by chance?
}

parameters {
  vector [4] gamma;
  real beta;
}

model {
  for(i in 1:N) {
    TDX[i] ~ like0(beta, gamma, 0.9); // This is the same as
    target += like_lpmf(TDX[i] | beta, gamma, 0.9); // This, which is the same as
    target += like_notlpmf(TDX[i], beta, gamma, 0.9); // This if the _lpmf syntax isn't used
    // You'd only use one of the above
  }

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

What I typed isn’t gonna produce a working model, but hopefully it’ll get you closer to what you want.

Btw, what is the custom probability function you’re trying to use? Stan has quite a few.

If you have a function that ends in _lpdf, like foo_lpdf, then you can use it in sampling statements as just foo. That is,

functions {
  real foo_lpdf(real y, real sigma) { return -(y / sigma)^2; }
}
parameters {
  real y;
}
model {
  y ~ foo(1.5);
}

You had

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

which won’t work.

Also, if you’re going to use integer data, it needs to be _lpmf.

Hi,

Thank you both for your support, and the newest manual is a helpful guide. My answers to your questions and I’ve updated the model

// Also, TDX is an integer, so it’s probably a pmf, not a pdf that you want
Thanks!

// TDX that goes all the way to N. Is it N, 3 dimensional things by chance?
TDX is a matrix N rows 3 cols, the first col is to, the second is dO, and the third x. I still can’t read the data, though. It gives me this error: Indexed expression must have at least as many dimensions as number of indexes supplied: indexed expression dimensionality = 0; indexes supplied = 1 I tried both int TDX[3]; and int TDX[] in the data section. I wish I thought it was a problem with Stan, but I’m almost certainly culpable.

Btw, what is the custom probability function you’re trying to use? Stan has quite a few.
I am writing my own probability function. The most complex is here https://github.com/nzgwynn/Amelia/blob/master/MyModel.stan.

Thanks for your patience.
gwynn

TDX is a matrix N rows 3 cols

int TDX[N, 3] is probably what you’re looking for then

Then TDX[i] is a 3 element array.

Do you have eqs./names for the custom probability function you’re trying to write? Maybe someone will recognize them and have a simple way to build them.

Gave me the same error:
Indexed expression must have at least as many dimensions as number of indexes supplied:
indexed expression dimensionality = 0; indexes supplied = 1

This model hasn’t been tried before, that’s why it’s hard (at least that’s what I tell myself)!! We don’t have a name for it yet but my first draft for the simplest model is “Discrete Proportional Hazard Models for Mismeasured Outcomes and Incorrect Estimates of Mismeasurement” but that’s probably not what we’ll call it. Who knows?

In what format should the R data be for Stan? The data is made like this:
Adata = list(
TDX = as.array(as.matrix(a data frame)),
N = number of rows in the data frame)
Perhaps that is the problem?

Thanks again for your patience,
gwynn

did you change the function arguments?

if TDX is a 2-D array, the function arguments list should be:

real like0_lpmf(int TDX[,], real beta, vector gamma, vector phi) {

The Stan compiler checks function argument types. When passing an array object to a function, you specify the number of array dimensions. one dimensional array: [] two dimensional array: [,], three dim array [,,] and so forth.

The message you are getting indicates that the function argument list has a scalar type for TDX and therefore the compiler objects to the indexes.

It didn’t like that either:
ERROR at line 6

4: ## Definitions
5: ##################
6: real like0_lpmf(int TDX[,], real beta, vector gamma, vector phi) {
^
7: int to;

PARSER EXPECTED: argument declaration or close paren ) to end argument declarations

Thanks again for your help, though.

My bad - the declaration should be int[,] TDX.
had to RTFM myself:

Dimensionality Declaration
Arguments and return types may be arrays, and these are indicated with optional brackets and commas as would be used for indexing. For example, int denotes a sin- gle integer argument or return, whereas real indicates a one-dimensional array of reals, real[ , ] a two-dimensional array and real[ , , ] a three-dimensional array; whitespace is optional, as usual.
The dimensions for vectors and matrices are not included, so that matrix is the type of a single matrix argument or return type. Thus if a variable is declared as matrix a, then a has two indexing dimensions, so that a[1] is a row vector and a[1, 1] a real value. Matrices implicitly have two indexing dimensions. The type declaration matrix[,] b specifies that b is a two-dimensional array of matrices, for a total of four indexing dimensions, with b[1, 1, 1, 1] picking out a real value.