# Summing arrays of a vector

Hi Stan users and experts,

May you help me with the following:

1- For model identification, I’m trying to apply a sum-to-zero constaint to the difficulty paramter (beta) such that the difficulty value for
the last item (J) is the negative sum of all (J-1) items. This should be held for each class (k).
However, based on the error I get, the sum can only be done for a vector, not a vector with arrays, vector [ ]
Any idea how to solve this issue? Here is part of my code that is related to this:

data {
int<lower=1> K ; // number of classes
int<lower=1> N ; // number of persons
int<lower=1> J ; // number of items
int<lower=0,upper=1> y[N,J] ; // response data set
}

parameters {
simplex[K] pi ; // mixing proportions
vector[N] theta ; // ability parm
ordered[K] mu ; // mean ability parm
ordered[K] beta[J] ; // difficulty parm
}

transformed parameters {

vector[K] beta_c[J] ;

for (j in 1:(J-1)) {
for (k in 1:K){
beta_c[j,k] = beta[j,k] ;
}}

``````for (k in 1:K){
for (j in 1:(J-1)) {
beta_c[J,k] = -1*sum(beta[j,k]);      // (Error location)
}}
``````

}

2- Since the number of items in my model do not exceed 30 items, does soft centering (i.e. beta ~ (0,1) ) is adequate for moel identifcation or is
it necessary to apply the sum-to-zero constaint to this paramter?

3- Last quations, with respect to label switching (in mixture model), I apply ordere constaint to 2 paramters, mu and beta,
is this correct, or I should apply this constrain to one parameter only ?

Thank you!

Isn’t this just a single number inside the summation? I don’t think you want to loop over both K and J here.

Sorry for the short reponse above, but only have a second right now. Hope that helps point you in the right direction.

I’ve tried removing the loops but it is not working. An error message appeared saying
“variable “k” does not exist” or “variable “j” does not exist”

I mean for example: for k=1 , beta30= -(beta1+… +beta29)
for k=2 , beta30= -(beta1+… +beta29)
Since each class (k) has its own set of beta’s

Yeah, so since you have the previous loops already filling in all the other elements, here you would need a loop over K but not J. For each k take the sum of J-1 things, the values for beta 1:(J-1), and insert the negative into the Jth slot.

You could also do it in the same loops you have above if you switch the inner and outer loops and add this outside the loop for J but inside the loop for K.

There are other ways to code this that may be more efficient but if this runs fast enough then you don’t need to worry about that.

I haven’t had a chance to look over the rest of your questions yet. Hopefully someone else will also chime in about the other questions too.

I’m not sure if I understand exactly what you mean. Could you please translate it to Stan code :)
As far as I understand, I may need another vector without arrays; for example
vector[J-1] beta
Is this correct?

Thanks

I haven’t tested this but one option is to declare `vector[J-1] beta[K];` in parameters and then in transformed parameters

``````vector[J] beta_c[K];

for (k in 1:K) {
beta_c[k, 1:(J-1)] = beta[k];
beta_c[k, J] = -sum(beta[k]);
}
``````

Does that do what you want?

Yes, Yes it works now. Many Thanks jonah.

I just made a slight change, in the brackets, after testing the code to avoid the error message, as shown below:

for (k in 1:K) {
beta_c[k, 1:(J-1)] = beta[k] ;
beta_c[k, J] = -sum(beta[k]) ;
}

Thanks again