Answers conditions inside model block

Hello, everybody!

I have an array of categorical answers y (in Y), where each column is associated with a time t (in T) and each row with an individual i (in N).
For each time t , I have the design matrix X and an auxiliar quantity n (that is an index for my padding values process, solved in Padding values in response arrays with different sizes)

data{
  int<lower = 1> N; // Sample size 
  int<lower = 1> K; // Categories
  int<lower = 1> D; // Covariates dimension
  int<lower = 1> T; // Time dimension
  int n[T]; // `Real` sample size
  matrix[N, D] X[T]; // Covariates
  int Y[N, T] ; // Answers
}

As I’m modelling a transition probability matrix for a markov process, my aim is to estimate the vectors of probabilities for two states. In other words, two rows of that matrix.

To do that, I assume that the answers (states at time t) follow a multinomial distribution and use the categorical_logit_lpmf function to estimate the parameters (betas) associated with the covariates.

parameters{
  matrix [D, K-1] beta3_aux;
  matrix [D, K-1] beta4_aux;

}

transformed parameters{
  matrix [D, K] beta3;
  matrix [D, K] beta4;

  for(d in 1:D){
  beta3[d] = append_col(-sum(beta3_aux[d]), beta3_aux[d]);
  beta4[d] = append_col(-sum(beta4_aux[d]), beta4_aux[d]);
  }
}

So, as I have to estimate two different vectors of parameters using the same vector of answers at time t, I have to take into account the state to not use informations worng.

My difficult is being to create a conditional statment that works fine in Stan.

I tried in that whay (the indexes are correct, I guess).


model{

  for(t in 2:T){
    for(i in 1:n[t]){
    
        if(Y[i,t] == 3) {
        target += categorical_logit_lpmf(Y[i,t] |  (X[t-1,i,]*beta3)');
        }
        else {
        target += categorical_logit_lpmf(Y[i,t] |  (X[t-1,i,]*beta4)');
        }

    }
  }
  
}

but it stops at the first interation

Chain 1: Iteration:   1 / 500 [  0%]  (Warmup)

In summary, I need some help to review and, maybe, optmize the code, because it is not working in that way :/
I was using categorical_logit_glm_lpmf function when I was not separating observations according to states and it was really good. I wish I could continue to use that, but I don’t think it is possible to use conditionals on that.

Thanks a lot!

1 Like

Hi,
I don’t think I understand your aims and model fully, but one thing that is strange is that Y[i,t] is a single integer, but you then call categorical_logit_lpmf(Y[i,t] | ... ), so you are observing a categorical distribution with just one category! And if you have just one category, than the categorical likelihood is always 1 and there is no connection between the observed values and data.

Best of luck with your model!

Hi, @martinmodrak, thanks for your answer :)
I think you are right at your point.
I was using as example what is presented on https://mc-stan.org/docs/2_20/stan-users-guide/multi-logit-section.html., but there it is not used the _lpmf function.
Will review my code to fix it.
Despite that, I think I solved my original problem with conditions.

1 Like

Oh, I misunderstood the code and confused categorical with multinomial - you are correct, the categorical should work with just a single integer, sorry for any confusion.

Glad you were able to resolve the problem!

I’m using categorical , the response must be the category :)
Your answer made me think that, maybe, I don’t need to use the i index. I will try to improve my code without it.
Thanks for the discussion ;)