time | count | treatment | group | pair_id |
---|---|---|---|---|
0 | 59 | 0 | 1 | 1 |
4 | 118.8333 | 0 | 1 | 2 |
8 | 143.8333 | 0 | 1 | 3 |
12 | 133.25 | 0 | 1 | 4 |
16 | 118.1667 | 0 | 1 | 5 |
20 | 105.6667 | 0 | 1 | 6 |
24 | 96 | 0 | 1 | 7 |
28 | 96.66666 | 0 | 1 | 8 |
32 | 88.08334 | 0 | 1 | 9 |
36 | 82.58334 | 0 | 1 | 10 |
40 | 76.91666 | 0 | 1 | 11 |
44 | 76 | 0 | 1 | 12 |
48 | 77.25 | 0 | 1 | 13 |
0 | 17.5 | 0 | 2 | 14 |
4 | 25.58333 | 0 | 2 | 15 |
8 | 29.41667 | 0 | 2 | 16 |
12 | 28.16667 | 0 | 2 | 17 |
16 | 26.25 | 0 | 2 | 18 |
20 | 23.5 | 0 | 2 | 19 |
24 | 22.41667 | 0 | 2 | 20 |
28 | 24.75 | 0 | 2 | 21 |
32 | 26 | 0 | 2 | 22 |
36 | 25.16667 | 0 | 2 | 23 |
40 | 24.33333 | 0 | 2 | 24 |
44 | 26.16667 | 0 | 2 | 25 |
48 | 27.16667 | 0 | 2 | 26 |
0 | 13.625 | 0 | 3 | 27 |
4 | 16.125 | 0 | 3 | 28 |
8 | 16.125 | 0 | 3 | 29 |
12 | 17.375 | 0 | 3 | 30 |
16 | 14.5 | 0 | 3 | 31 |
20 | 14.875 | 0 | 3 | 32 |
24 | 15.375 | 0 | 3 | 33 |
28 | 15.875 | 0 | 3 | 34 |
32 | 14.875 | 0 | 3 | 35 |
36 | 18.25 | 0 | 3 | 36 |
40 | 15.875 | 0 | 3 | 37 |
44 | 17.75 | 0 | 3 | 38 |
48 | 19 | 0 | 3 | 39 |
0 | 7.333333 | 0 | 4 | 40 |
4 | 10.08333 | 0 | 4 | 41 |
8 | 9.083333 | 0 | 4 | 42 |
12 | 8.583333 | 0 | 4 | 43 |
16 | 7.666667 | 0 | 4 | 44 |
20 | 7.5 | 0 | 4 | 45 |
24 | 7.333333 | 0 | 4 | 46 |
28 | 5.583333 | 0 | 4 | 47 |
32 | 7.083333 | 0 | 4 | 48 |
36 | 7.166667 | 0 | 4 | 49 |
40 | 8.333333 | 0 | 4 | 50 |
44 | 10.91667 | 0 | 4 | 51 |
48 | 11.33333 | 0 | 4 | 52 |
0 | 51.25 | 1 | 1 | 1 |
4 | 93 | 1 | 1 | 2 |
8 | 134 | 1 | 1 | 3 |
12 | 142.125 | 1 | 1 | 4 |
16 | 135.5 | 1 | 1 | 5 |
20 | 125.625 | 1 | 1 | 6 |
24 | 113.125 | 1 | 1 | 7 |
28 | 107.25 | 1 | 1 | 8 |
32 | 103.125 | 1 | 1 | 9 |
36 | 104.375 | 1 | 1 | 10 |
40 | 103.125 | 1 | 1 | 11 |
44 | 110.375 | 1 | 1 | 12 |
48 | 114.25 | 1 | 1 | 13 |
0 | 12.91667 | 1 | 2 | 14 |
4 | 22.16667 | 1 | 2 | 15 |
8 | 25.08333 | 1 | 2 | 16 |
12 | 26.41667 | 1 | 2 | 17 |
16 | 26.33333 | 1 | 2 | 18 |
20 | 26.16667 | 1 | 2 | 19 |
24 | 25.83333 | 1 | 2 | 20 |
28 | 28.08333 | 1 | 2 | 21 |
32 | 27.75 | 1 | 2 | 22 |
36 | 26.08333 | 1 | 2 | 23 |
40 | 28.58333 | 1 | 2 | 24 |
44 | 27.91667 | 1 | 2 | 25 |
48 | 28.58333 | 1 | 2 | 26 |
0 | 10.91667 | 1 | 3 | 27 |
4 | 11.83333 | 1 | 3 | 28 |
8 | 13.41667 | 1 | 3 | 29 |
12 | 14.5 | 1 | 3 | 30 |
16 | 16 | 1 | 3 | 31 |
20 | 17.08333 | 1 | 3 | 32 |
24 | 16.33333 | 1 | 3 | 33 |
28 | 19.08333 | 1 | 3 | 34 |
32 | 17.16667 | 1 | 3 | 35 |
36 | 16.25 | 1 | 3 | 36 |
40 | 18.08333 | 1 | 3 | 37 |
44 | 20.16667 | 1 | 3 | 38 |
48 | 22.33333 | 1 | 3 | 39 |
0 | 3.5 | 1 | 4 | 40 |
4 | 6.666667 | 1 | 4 | 41 |
8 | 6.666667 | 1 | 4 | 42 |
12 | 8 | 1 | 4 | 43 |
16 | 8.666667 | 1 | 4 | 44 |
20 | 10.41667 | 1 | 4 | 45 |
24 | 10.41667 | 1 | 4 | 46 |
28 | 10.91667 | 1 | 4 | 47 |
32 | 10.75 | 1 | 4 | 48 |
36 | 11.5 | 1 | 4 | 49 |
40 | 11.58333 | 1 | 4 | 50 |
44 | 11.16667 | 1 | 4 | 51 |
48 | 13 | 1 | 4 | 52 |
This is my raw data. the data has 4 groups, within each group they are treated (denoted as 1) or untreated (denoted as 0) randomly. They are also paired within each group, so I set up multilevel models. The aim is to **estimate the effect of treatment (theta) on count in each group.
I wrote stan codes for analyzing each group individually and all groups collectively. However, both stan codes do not converge completely. Can someone help me to solve this issue?
stan code for analyzing each group individually:
[edit: escaped Stan code]
data {
int n; // number of obs
int J; // number of pairs
vector[n] T; // treatment
vector[n] y; // count
int<lower=1, upper=13> pair_id[n]; // pair indicator
}
parameters {
vector[J] alpha;
real theta;
real sigma_y;
real mu_a;
real sigma_a;
}
model {
theta ~ normal(0,10);
sigma_y ~ uniform(0,10);
sigma_a ~ uniform(0,10);
mu_a ~ normal(0, 10);
alpha ~ normal(mu_a, sigma_a);
for (i in 1:n) {
y[i] ~ normal(alpha[pair_id[i]]+T[i]*theta, sigma_y);
}
}
stan code for analyzing all groups collectively:
data {
int n; // number of obs
int J; // number of pairs
int K; // number of groups
vector[n] T; // treatment
vector[n] y; // count
int<lower=0, upper=K> group[n]; // group indicator
int<lower=0, upper=K> pair_group[J]; // pair group indicator
int<lower=0, upper=J> pair_id[n]; // pair indicator
}
parameters {
vector[J] alpha;
real theta[K];
real sigma_y[K];
real mu_a[K];
real sigma_a[K];
}
model {
for (k in 1:K) {
theta[k] ~ normal(0,10);
sigma_y[k] ~ uniform(0,10);
sigma_a[k] ~ uniform(0,10);
mu_a[k] ~ normal(0, 10);
}
for (j in 1:J) {
alpha[j] ~ normal(mu_a[pair_group[j]], sigma_a[pair_group[j]]);
}
for (i in 1:n) {
y[i] ~ normal(alpha[pair_id[i]] + T[i]*theta[group[i]], sigma_y[group[i]]);
}
}