I am new to rstan and having difficulty specifying a hierarchical model for changepoint analysis in fish movement data. I would like assistance with model specification or reparameterization, as I am encountering the following warnings after running `rstan::sampling()`

:

```
1: There were 16 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
2: There were 4 chains where the estimated Bayesian Fraction of Missing Information was low. See
https://mc-stan.org/misc/warnings.html#bfmi-low
```

I have already increased `adapt_delta`

, but the warnings persist.

### Background:

The goal of my analysis is to detect changepoints (or whether such points exist) in the positions (i.e. state) of river fish over time. My dataset consists of 8 observations per individual (N = 8) for 5 individuals (K = 5), with data collected over consecutive days. The data is structured as follows:

```
> data
$N
[1] 8
$K
[1] 5
$Y
[1] 231.84559 234.53067 234.51740 233.56482 237.17536 229.79460
[7] 228.59779 227.76857 93.28173 99.40232 104.81858 102.88056
[13] 103.60429 98.21964 95.98356 98.00442 609.43382 608.35827
[19] 593.83546 591.99762 610.21665 601.23710 605.45246 607.57787
[25] 604.33400 607.19565 600.49975 594.15830 604.08196 595.68660
[31] 602.18625 597.53365 779.93654 779.93654 779.93654 779.93654
[37] 779.93654 776.38965 779.93654 775.96977
$zero_Dist
[1] 231.84559 93.28173 609.43382 604.33400 779.93654
```

The variable Y represents the position, specifically the distance from the most downstream point in my research area.

### Model Description:

I used Inverse Transform Sampling for generating samples from a Cauchy distribution and specified the prior for `state_raw`

using a normal distribution with individual-specific standard deviations. Below is my current Stan code:

```
data {
int<lower=0> N; // Number of observations per individual
int<lower=0> K; // Number of individuals
vector[N*K] Y; // Vector containing the observed values
vector[K] zero_Dist; // Initial position for each individual on the first day
}
parameters {
real<lower=0, upper=20> s_v; // Observation error standard deviation
real<lower=0, upper=20> s_w; // State error standard deviation
vector<lower= -pi()/2, upper= pi()/2>[(N-1)*K] state_raw; // Raw state deviations
vector<lower=0>[K] s_K; // Individual-level standard deviations for state_raw
vector[K] s_zero_Dist_raw; // Initial state deviations for each individual
}
transformed parameters {
vector<lower=0, upper=850>[K] state_zero; // Initial state for each individual
vector<lower=0, upper=850>[N*K] state; // State vector for all individuals
for (k in 1:K) {
// Prior distribution for state_zero (reparameterization)
state_zero[k] = zero_Dist[k] + 3 * s_zero_Dist_raw[k];
// State for the first day
state[1 + (k-1)*N] = state_zero[k];
// State for the second day and beyond
for (i in 2:N) {
state[i + (k-1)*N] = state[i-1 + (k-1)*N] + s_w * tan(state_raw[i-1 + (k-1)*(N-1)]); // Inverse Transform Sampling
}
}
}
model {
// Priors
s_v ~ normal(5, 3);
s_w ~ normal(5, 3);
s_zero_Dist_raw ~ normal(0, 1);
// Hierarchical model for state_raw
for (k in 1:K) {
for (i in 1:(N-1)) {
state_raw[i + (k-1)*(N-1)] ~ normal(0, s_K[k]); // Hierarchical definition of state_raw for individual-specific variation
}
}
// Observation model
for (i in 1:(N*K)) {
Y[i] ~ normal(state[i], s_v);
}
}
"
```

I am also new to this forum and please tell me if more information is needed.

I would appreciate suggestions for reparameterization to address the divergent transitions and BFMI warnings, or any insights on improving the hierarchical structure of my model. Thank you!