Define Dirichlet parameters in advance settings

Oh, yes. I got the error. I fixed it.

Now I need to know, what is the type of theta_1 and theta_2. Earlier, I used them as arrays in my user-defined function. Is this still correct?




// Create user define function

functions{

  real tenismodel_lpdf(matrix dat, vector[] theta, vector[] alpha, int[] ServerID, int[] ReceiverID, int[] t1, int[] t2, int[] t3){

  vector[rows(dat)] prob;
  real  t;
  real  x;
  int s;
  int re;
  real out;
  real a;
  real f;
  real ws;
  real rs;
  real ms;
  real wr;
  real rr;
  real mr;
  real pr;
    
  for (i in 1:rows(dat)){
    
    t <- dat[i, 1];
    x <- dat[i, 2];
    s <- ServerID[i];
    re <- ReceiverID[i];
    a <- theta[s, 1];
    f <- theta[s, 2];

    
    if(x == -1){
      
      prob[i] <- f; 
      
    }else if(t == 1){
      
      prob[i] <- a^x * f^(1 - x);

    }else if(fmod(t, 2) == 0){
     
     vector [t1[i]] p1 ;

     for(j in 1:t1[i]) {
       
        rs <- alpha[s, 2*j];
        rr <- alpha[re, (2*j + 1)];
        
        p1[j] <- (rr) * (rs);
     }
     
       pr <-  prod(p1);
      
        ws <- alpha[s, 1];
        ms <- alpha[s, 3];
        wr <- alpha[re, 1];
        mr <- alpha[re, 3];
      prob[i] <- ((1- a - f) * pr * (mr)^x * (wr)^(1-x)); 
    
    }else {
      
      vector [t2[i]] p1;
     
     for(j in 1:t2[i]) {
       
        rs <- alpha[s, 2*j];
        rr <- alpha[re, 2*j];
        
        p1[j] <- (rr);
     }
     
     vector [t3[i]] p2;
     
     for(j in 1:t3[i]) {
       
        rs <- alpha[s, (2*j + 1)];
        rr <- alpha[re, (2*j + 1)];
        
        p2[j] <- (rs);
     }
     
     pr <- prod(p1) * prod(p2);
     ws <- alpha[s, 1];
     ms <- alpha[s, 3];
     wr <- alpha[re, 1];
     mr <- alpha[re, 3];
     
       prob[i] <- ((1- a - f) * pr * (ms)^(1 - x) * (ws)^x);
    }
  }

  out <- sum(log(prob));
  return out;

 }
}


// The input data is a vector 'y' of length 'N'.
data {
  int N; // number of observations (rally lengths)
  int M; // number of players
  int t1 [N]; // number of touches
  int t2 [N]; // number of touches
  int t3 [N]; // number of touches
  int K;
  matrix [N, 2] dat; // touches, indicator, servereID and receiverID 
  int ServerID [N];
  int ReceiverID [N];
  int <lower = 2> m; 
  row_vector [3] beta1;
  row_vector [3] beta2;


}

parameters {
    
     simplex [m] theta_1 [M];
     
     simplex [m] theta_2 [M, K];
  
}

// The model to be estimated.
model {
  // Define priors

 theta_1 ~ dirichlet(beta1);
 
  for(i in 1:M){
  for(j in 1:K){
    
    theta_2[i,j] ~ dirichlet(beta2);
  }
}


  dat ~ tenismodel(theta_1, theta_2, ServerID, ReceiverID, t1, t2, t3);

}



Hi Jsocolar,

I fix the problem in the Dirichlet prior settings in the parameter section. Since now my theta_2 is a two dimension array, in my tenismodel function is that correct I define theta_2 as a vector?. I think it is not, because, I am getting the error,

Ill-typed arguments to '~' statement. No distribution 'tenismodel' was found with the correct signature..

I think this is again may be the reason that my theta_2 is not a vector. Do you have any suggestion for this?

Your tenismodel_lpdf function doesn’t have an argument named theta_2 so I’m not sure exactly what you are passing where. Of course the arguments that you pass to tenismodel_lpdf need to match the types that tenismodel_lpdf expects to receive. Maybe you need to update your declaration of the argument types in tenismodel_lpdf. Maybe you need to pass different types that match the declared arguments, for example by looping through the data and extracting elements from the arrays. The error is telling you that the types you are passing don’t match the types that tenismodel_lpdf is expecting.

Thank you for the explanation @jsocolar. My theta_2 is the alpha argument in the tenismodel function. I want to know, simplex [m] theta_2 [M, K]; can I consider this as a vector [] or is this is a matrix?. I have defined alpha as an array. I think this is incorrect right?.

Each element of theta_2 (that is, theta_2[i,j] for some particular i and j) is a vector. theta_2 itself is neither a vector nor a matrix; it is an array of vectors.

@jsocolar, then when defining the function argument alpha related to theta_2, what can we do?. I can not understand, how we can define it in a function argument, if it is an array of vectors.

Either you need to write your function tenismodel_lpdf to take an array of vectors, or you need to run the function in a loop, passing in one element of the array theta_2 at each iteration of the loop.

@jsocolar thank you very much. I will try both methods. Do we have an example that we can create a function that take an array of vectors as an argument in a function.

To pass a one-dimensional array of vectors called, say, var1, to a function that returns a real, the signature would look like,

real function(array[] vector var1) {
  return 42.0;
}

If you want more dimensions in the array, add commas inside the brackets. You can read more in the Stan Reference Guide here.

@ssp3nc3r thanks for this suggestion and it works. However, if array is two dimensional how can I read one value of that. If array[] vector var1, we can read the first element as var1[1,1]. If it is a two-dimensional array, array[ , ] vector var1 how can I get a specific value from this array?. It is not var[1, 1] right?.

var[1,1,1] would access the first element of the vector that is stored in the array at [1,1]:

1 Like

Thank you @ssp3nc3r . I will try this one.

@ssp3nc3r , do we have a section in stan manual that defines, how to create a real value vector?. If I use int [] a this tells my vector is integer values. But I have vectors that contains decimal values in my function. So when I use int[] t1 in my function I am getting an error,

  Exception: int variable contained non-int values; processing stage=data initialization; variable name=t1; base type=int (in 'string', line 94, column 2 to column 13)
failed to create the sampler; sampling not done```.

I think this is because my t1 contains decimal values as well.

Yes, we do.