Hi,
We developed the model with two covariates and three time-points as below, as time-intervals between three time-points are varied for each individual, I also defined time as variable “yrs” as a covariate.
functions {
//this function gets a matrix input all vars except one which is outcome
matrix select_columns(matrix input, int nRows, int nCols, int dropColIndex) {
matrix[nRows, nCols - 1] ret;
for(i in 1:nRows){
for(j in 1:nCols){
if(j < dropColIndex) ret[i, j] = input[i, j];
else if (j > dropColIndex) ret[i, j - 1] = input[i, j];
}
}
return ret;
}
// personIndex is the person in the data with its position
matrix select_person(matrix input, int nTime, int nCols, int personIndex) {
matrix[nTime - 1, nCols] ret;
int startIndex = (nTime * (personIndex - 1)) + 1;
// we need data from startIndex by nTime-1 (lagged) and also subtract the startIndex to move to next person
for(r in startIndex:(startIndex + nTime - 2)){
for(c in 1:nCols){
ret[r - startIndex + 1, c] = input[r, c];// e-startIndex=0 then
}
}
return ret;
}
}
data {
int<lower=1> N_time;
int< lower=1 > N; //total population;
int<lower=0> G; //number of covariates, excluding bodn
int<lower=2> K_G[G]; //number of observable categories
int<lower=2> K_Sum; //sum of observable predictors excluding body
int<lower=2> K_body; //number of observable categories
int<lower=1> Y[N_time * N, G];
int<lower=1,upper=K_body> Y_body[N_time * N];
real<lower=0> G_years[N_time * N];
matrix[N_time * N, G] X; // We don't have predictors for the first time point
}
transformed data {
int<lower=1> Y_tr[N_time, G, N];
int<lower=1> Y_body_tr[N_time, N];
for(t in 1:N_time){
for(n in 1:N){
for(g in 1:G){
Y_tr[t, g, n] = Y[t * n, g];
}
Y_body_tr[t, n] = Y_body[t * n];
}
}
}
parameters {
vector[G - 1] beta[G];
vector[G] beta_body;
real beta_years;
real b[G];
real b_body[G];
real b_body_body;
vector[N_time] f[G, N];
vector[N_time] f_body[N];
real<lower = 0> sigma[G];
real<lower = 0> sigma_body;
// with K_Sum we produce a sum of cut-point sof all G variables and then each time we subtract
// G cutpoints from the sum
ordered[K_Sum - G] c;
ordered[K_body - 1] c_body;
}
model {
int position = 0; //for cut-point selection
for (i in 1:G){
beta[i] ~ normal(0, 1);
b[i] ~ normal(0, 1);
b_body[i] ~ normal(0, 1);
sigma[i] ~ cauchy(0,0.5);
for(n in 1:N){
f[i, n][1] ~ normal(0, 5);
}
}
beta_body ~ normal(0, 1);
beta_years ~ normal(0, 1);
b_body_body ~ normal(0, 1);
sigma_body ~ normal(0, 1);
for(n in 1:N){
f_body[n][1] ~ normal(0, 5);
}
//Propagate the state information
for(t in 2:N_time) {
for (g in 1:G){
for(n in 1:N){
f[g, n][t] ~ normal( select_person(select_columns(X, N_time * N, G, g),
N_time, G - 1, n)[t - 1,] * beta[g] +
b[g] * f[g, n][t - 1] +
b_body[g] * f_body[n][t - 1] + beta_years * G_years[n * t], sigma[g]);
}
}
for(n in 1:N){
f_body[n][t] ~ normal( select_person(X, N_time, G, n)[t - 1, ] * beta_body +
b_body_body * f_body[n][t - 1] + beta_years * G_years[n * t], sigma_body);
}
}
for (g in 1:G){
vector[K_G[g] - 1] cp = c[(position + 1):(position + K_G[g] - 1)];
position += position + K_G[g] - 1;
cp ~ student_t(3, 0, 1); // prior for cut-point
for(n in 1:N){
Y_tr[, g, n] ~ ordered_logistic(f[g, n], cp);
}
}
c_body ~ student_t(3, 0, 1);
for(n in 1:N){
Y_body_tr[, n] ~ ordered_logistic(f_body[n], c_body);
}
}
I appreciate your feedback on this.
The output looks like below:
beta_e_hr 0.050395909 0.6672185 -1.24885134 1.25779763
beta_hr_e 0.512087322 0.6372881 -0.52652329 1.89903187
beta_body_e 0.755302199 0.7433808 -0.51206939 2.34644795
beta_body_hr 0.376309329 0.5188516 -0.55336491 1.42843827
beta_years 0.055748186 0.1765089 -0.25565982 0.37867082
b_e_e 0.247697540 0.4390430 -0.59364752 1.08782187
b_hr_hr 0.341984254 0.4071307 -0.46687935 1.09842227
b_e_body -0.835428520 0.5538218 -2.02667535 0.23969166
b_hr_body -0.520789651 0.7067315 -1.88771354 0.78589185
b_body_body -0.000964786 0.6562512 -1.16369311 1.25138771
f_e_n1_t1 -4.309393434 2.6136138 -9.88533566 0.50273342
f_hr_n1_t1 1.864706667 1.8866074 -1.23764746 6.20032112
f_e_n2_t1 -1.802023365 1.7879316 -5.43283475 1.81219151
f_hr_n2_t1 4.418231434 2.8910659 -0.65202836 10.88769634
f_e_n3_t1 -0.538218374 1.8782298 -3.76102981 3.80766239
f_hr_n3_t1 4.015153954 2.6181371 -0.49278353 9.70408960
f_e_n4_t1 -4.720660029 2.6004952 -10.07332466 -0.38849620
f_hr_n4_t1 -3.671408473 3.1290634 -10.67296449 1.57139916
f_e_n1_t2 -1.034875571 1.5588196 -4.16177197 2.09016662
f_hr_n1_t2 2.735759315 2.8921214 -0.76002867 11.48318328
f_e_n2_t2 -4.418297951 2.5775154 -9.43458532 -0.34621993
f_hr_n2_t2 -0.354805627 2.0865423 -4.47516567 3.68655972
f_e_n3_t2 -3.232843959 2.5141742 -9.53302301 0.35976341
f_hr_n3_t2 1.306214148 1.4669948 -1.44666155 4.39781256
f_e_n4_t2 -1.917104784 1.4182010 -5.17641266 0.66321080
f_hr_n4_t2 -1.554761259 2.1645973 -6.69465708 1.84760814
f_e_n1_t3 -1.122146605 1.5466718 -4.29318127 2.15248253
f_hr_n1_t3 2.906326979 3.0378513 -1.18167060 11.07972257
f_e_n2_t3 -3.072469728 2.9283578 -10.85647339 1.54530070
f_hr_n2_t3 1.181675570 1.5492420 -1.91342947 4.21035962
f_e_n3_t3 -2.100910942 1.7284324 -5.70743849 1.38905440
f_hr_n3_t3 -0.032990289 1.6439964 -3.48733198 2.87561716
f_e_n4_t3 -0.297312014 1.7325890 -3.66683649 3.16950395
f_hr_n4_t3 1.375329315 1.8175420 -2.11799206 5.13158080
f_body_n1_t1 0.409607207 1.4789244 -2.75101440 3.59299201
f_body_n2_t1 3.656919862 2.0129381 0.42547093 8.42773947
f_body_n3_t1 2.249041015 1.5623943 -0.88685460 5.23952809
f_body_n4_t1 1.920366698 1.7185490 -0.44175115 6.77178836
f_body_n1_t2 1.602497719 1.5366898 -1.51546581 4.53498282
f_body_n2_t2 1.490072507 2.1342403 -2.07665200 5.71590120
f_body_n3_t2 3.004104444 1.2065585 0.88768375 5.40344697
f_body_n4_t2 1.006368311 1.1745803 -1.92303071 3.13812846
f_body_n1_t3 2.935316123 1.5960795 0.12325283 6.08925689
f_body_n2_t3 3.207557836 1.4854862 0.65255579 6.09422554
f_body_n3_t3 1.825944726 1.4909447 -0.75891584 5.28242235
f_body_n4_t3 4.493960648 2.0041912 1.13737928 8.48069745
sigma_e 1.163451959 1.3358080 0.05728349 4.57031299
sigma_hr 1.097063883 1.0925747 0.04970679 3.92241782
sigma_body 1.105672017 0.4808567 0.33428851 2.18500816
c_e_1 -2.590221808 0.9329944 -4.56878218 -0.89342448
c_e_2 -0.736207512 0.5757990 -1.93693628 0.30522640
c_e_3 0.233032975 0.5397744 -0.81525620 1.34900019
c_hr_1 0.559681445 0.6149305 -0.56188720 1.85774239
c_hr_2 2.351823838 1.0160333 0.64800451 4.61528595
c_body_1 -3.319405639 2.2604276 -9.87711969 -0.74233403
c_body_2 -1.774732378 1.0330290 -4.45708587 -0.34920066
c_body_3 -1.147109826 0.6979263 -2.84662593 -0.07616401
c_body_4 -0.782233757 0.5568800 -2.01395094 0.16376616
c_body_5 -0.546834694 0.4918412 -1.57788496 0.37887587
c_body_6 -0.269197560 0.4437017 -1.16517754 0.58749350
c_body_7 -0.016720799 0.4336173 -0.80421459 0.81480846
c_body_8 0.818056044 0.5055725 -0.07427233 1.92403837
c_body_9 1.453760796 0.6194285 0.37590679 2.80068910
c_body_10 1.763228035 0.6651253 0.57346015 3.28159577
c_body_11 3.661580482 1.0968645 1.83732355 5.86169802
lp__ -89.908971873 14.7324540 -117.60427823 -63.69930211
@martinmodrak, @asael_am
Now, my questions are
1- whether the beta for the ordinal predictor is for the entire time-series or should be time-specific?
2-Can we say the beta coefficients are the maximum beta for the ordinal variable or I should include mo function? What about I add the monotonic effect to this syntax? @paul.buerkner
3- There are 1200 people with three time-points, but if I want to add the fourth time-point, 400 people do not have the data as they either died or have not been observed yet. Can this be modeled as truncation because the whole data does not exist for 400 people?
Thanks a lot for your inputs.