# Generating and utilizing random interger in the model block

Can someone please help with this? I have a data chain ‘mu’ that I want to randomly select the values at each iteration and utilize the selected values i the model block. So I created the transformed data block where a random integer is created as an index to be used to select the corresponding value of mu. But so far, it seems that the model block has been utilizing only one index for mu. How can I get the index to change at each iteration?
data {
int N;
int K;
int s1;
int s2;
real x[N];
real y[N];
real mu;//Use when mu data is used
int id[N];
//real id_l[K];
}

transformed data {
real unif = uniform_rng(0,1);
real idn = ceil(unif * 9999);
int idx;
for (n in 1:9999) {
if (n == idn) {
idx = n;
break;
}
}
}

parameters {

real beta;
real<lower=0> sigma_1;
real<lower=0> sigma_2;
//real<lower=0> mu;//use when estimating mu
real<lower=1> muu;
real<lower=0> sigmau;
vector[K] U_raw;
}

transformed parameters {
real nu[N];
//real U[N];
//real k;
vector[K] U=U_raw*sigmau+muu;
// U = vector();
//U=U_rep[id_l U_ ];

``````    for(n in 1:N){

nu[n]=U[id[n]]+beta*x[n]; #first model with log transformed data
//nu[n]=exp(U[id[n]])*(x[n])^beta;##second model with original data values

}

}
``````

model {

``````    U_raw ~std_normal();
for(n in 1:s1){

y[n] ~ logistic(nu[n],sigma_1);

}
for(n in s2:N){

//y[n] ~ logistic(nu[n]+mu,sigma_2);#Use this if we are estimating mu
print("idx",idx);

y[n] ~ logistic(nu[n]+mu[idx],sigma_2);

}
}
``````

I do not believe it is possible to do what you’re describing in Stan. The `transformed data` block is only run once per sampling run, as you’ve noted, and `rng` functions cannot be used in the model block directly.

2 Likes

Okay, thank you for the information

Just for extra context: even if you were sure that this random subsampling of data between iterations actually makes sense and the corresponding Markov chain corresponded to some well-defined posterior distribution (which is far from obvious, but not impossible), there will be a remaining technical problem: Stan’s algorithm requires adaptation of the mass matrix and step size to work. This adaptation is generally sensitive to specific data (the same model with different data often requires quite different step size/mass). So even if you went to Stan’s C++ codebase and interleaved Stan’s HMC steps with your data subsampling (which would be possible in C++) your approach would very likely mess-up the adaptation making HMC perform poorly.

2 Likes