Hi all!

I am trying to fit a model where I calculate a parameter for each J unit and then use the variance of that parameter in each C group as a prior for another parameter. I am attaching a minimum version of the model that gets at the problem I am facing in calculating the variance in the parameter for each group. But I give the 1st parameter as data in the example as to not complicate the model. I have also included some R code to generate the data to run this example.

The problem I am having is in getting the units into their indicated groups to calculate the variance.

PS: This is my first post so I apologize if the formatting is incorrect in any way.

```
data{
int<lower=0> J; // number of units
int<lower=1> D; // number of dimensions
int<lower=0> C; // Number of groups
int<lower=1, upper=C> Q[J]; // unit group indicator
matrix[J,D] position; //position for unit j in dimension d
}
parameters {
vector[C] zeta;
}
transformed parameters {
matrix[C,D] variance_position;
for (c in 1:C){
for (d in 1:D){
variance_position[c,d] = variance(position[Q[c],d]); // calculate variace of position for each group
}
}
}
model{
for (c in 1:C){
target+= normal_lpdf(zeta|variance_position[c,1]+variance_position[c,2],1);
}
}
```

R code to create the data

D <-2

J <- 30

C <- 5

c<- rep(1:C,3)

position <- matrix(data=rnorm(60,0,1),nrow=J,ncol=D)

data_d <- data.frame(Country = c, Group = c(1:J), position)

var1 <- data_d %>% group_by(Country) %>% summarise(var(X1))

var2 <- data_d %>% group_by(Country) %>% summarise(var(X2))

data_d %>% summarise(var(X2))

stan <- list(

J = J,

D = D,

Q = as.integer(data_d$Country),

C = C,

position = position)