# Function for categorical response with no exponentiation stan

Hi, I’m trying to find a distribution similar to categorical but without the exp component in rstan. My probability model is a divided-by-total model (i.e., p=x1/x1+x2 or p=x2/x1+x2) but x does not exponentiation like the categorical function, does anyone know if there is an existing distribution like this? Thank you!

I could be wrong but it sounds like you are confusing the categorical distribution with the categorical logit distribution; only the latter being a softmax, the former is what you want:

1 Like

Thank you for your reply! Yeah, I figured that out and I used Bernoulli function instead of categorical cuz I only have two categorical output, but now I’m getting another error below and I’ve been dealing with that for hours and still does not have any clue. It would be great if you have any suggestions

"> Rejecting initial value: Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: bernoulli_lpmf: Probability parameter is nan, but must be finite! Chain 1: Initialization between (-2, 2) failed after 100 attempts.

Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
Error in sampler\$call_sampler(args_list[[i]]) : Initialization failed.
character(0)
error occurred during calling the sampler; sampling not done

Check your input data ; I don’t imagine you can have zeros in it . Putting constraints on your data on the data section is a useful way to make sure it is what you expect.

I do have zeros in my data and this is because I coded 1 to represent 1 category and 0 to be another category. Why this is a problem? Do you think changing to other numbers such as 12 and 21 will help?

Hi I just recoded my data to be 12 and 21 and I got the following error and line 258 is my model:

res[i,j] ~ bernoulli(response[i,j]);

Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: bernoulli_lpmf: n is 21, but must be in the interval [0, 1] (in ‘model2ac3db81453_mupp2’ at line 258)

If you’re fitting Bernoulli data then it needs to be 0 or 1 coded like the message says .

Chain 1: Exception: bernoulli_lpmf: Probability parameter is nan, but must be finite! Chain 1: Initialization between (-2, 2) failed after 100 attempts.

Looks like your theta parameter is out of range. Needs to be between zero to one in your case.

Can you paste the model here? If not just try constraining it to zero to one and pick a prior which has a density which covers that range.

I tried imposing a lower and upper bound to theta, but it did not work. The pasted has the imposing constraint

I think there is a problem in the following section. response depends on pst and pts, which depend on spr which is declared in this section but never set to anything (and therefore nan by default).

1 Like

The variables are scoped by the { } brackets. So the spr variable you’ve declared in the previous loop is out of scope in the next one. If you’d like to use the variable in both loops then the variable needs to be declared in the scope that both the loops are in. E.g. at the top of the model section.

That looks a bit suspicious?
Did you know that you can construct matrices like this:

``````matrix[3,3] Sigma =
[[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]];
``````

And in this case looks like you want is

``````cov_matrix[12] SigmaAB = diag_matrix(rep_vector(1.0, 12));
``````
1 Like

Thank you for your advice. I moved the below line to the beginning of the model section and now I’m getting another error below. It is still about sampling but it seems like it’s initial value problem. I tried a few other values and they don’t work. I pasted my initial value R code, any advice is appreciated!

real spr[n_student, n_item];

Chain 1: Rejecting initial value:
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.
Chain 1: Stan can’t start sampling from this initial value.
Chain 1:
Chain 1: Initialization between (-2, 2) failed after 100 attempts.
Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
Error in sampler\$call_sampler(args_list[[i]]) : Initialization failed.
character(0)
error occurred during calling the sampler; sampling not done

init_fun <- function() {
list(a=rep(1,S), b=rep(0,S), t=rep(-1,S), theta=matrix(rep(0, len=I*D), nrow = I))
}

Thanks! You are right! It makes my code much shorter lol