Time-series in Stan, I am new to Stan and need hints to develop the model. THANKS

@asael_am
Hello,
I ran your code.
A couple of questions:
1- What I understood is the code is a code for a repeated measure for one person ( there was no looping for the number of individuals).
2- The data is in long format, right?

So with this token, for a generic data as below

id	time	e	hr	bon
1	1	1	2	8
1	2	2	3	11
1	3	3	3	11
1	4	5	4	10

offset <- 2 # column number we ignore to start with in data.frame

nth <- as.integer(11)


data_bon <-list (
  N = nrow(data_csv),
  D = 2,
  y = as.array(data_csv[, "bon"]),
  p = 3,
  nthres = nth,
  x = as.matrix(data_csv[, (1 + offset):(offset + 2)], rownames.force = NA)
)


data {
  int<lower=2> nthres;      // number of thresholds
  int<lower=0> N;           // number of data items
  int<lower=1> D;           // number of regressor variables
  int<lower=0> p;           // number of autoregressive coefficents
  matrix[N,D] x;                 // regressor data in a matrix
  int<lower=1,upper=nthres> y[N];// outcome time series
  
  }
parameters {
  real theta0;
  vector[D] beta;
  ordered[nthres] Intercept; 
  vector<lower=-1,upper=1>[p] phi;
}
transformed parameters{
  // form parameter;
  vector[N] theta;

  theta = x * beta;
  for (i in 1:N) {
    // constant in autorregresive
    theta[i] += + theta0;                             
    // Autoregressive part
    if(p > 0) for(j in 1:p) if(i > j) theta[i] +=  y[i-j]*phi[j];
   
  }
}
model {
  target += normal_lpdf(phi|0,0.5);
  target += normal_lpdf(beta| 0,1);
  target += normal_lpdf(Intercept | 0,10);
  target += ordered_logistic_lpmf(y|theta, Intercept);
}

and the results are

                      mean    se_mean        sd         2.5%         25%          50%
theta0          6.05083793 0.18218977 4.7225481  -2.45601200   2.6989507   5.88226463
beta[1]         0.05891928 0.03595626 0.9972350  -1.91102536  -0.6256622   0.07992466
beta[2]        -0.22489832 0.03413903 0.9243351  -1.93378050  -0.8606048  -0.20425814
Intercept[1]  -15.82924254 0.26063241 5.6535578 -28.29973037 -19.3954881 -15.45379435
Intercept[2]  -10.56605088 0.12927146 4.4399404 -19.31396617 -13.3723420 -10.45228431
Intercept[3]   -7.29034039 0.10717417 4.1156654 -15.43819591 -10.0767843  -7.38588243
Intercept[4]   -4.67818672 0.10796824 3.8041731 -12.19126058  -7.3074708  -4.56105732
Intercept[5]   -2.36494641 0.09727661 3.6234013  -9.15883921  -4.8871701  -2.30541060
Intercept[6]   -0.03352899 0.10584216 3.6771769  -7.15484537  -2.4607416   0.00188459
Intercept[7]    2.08107561 0.11679291 3.7372556  -5.11887969  -0.3678580   2.09622741
Intercept[8]    5.68207277 0.13567748 3.8958760  -1.65207017   2.7898973   5.67823170
Intercept[9]    6.98947730 0.12545235 3.9298268  -0.28491536   4.1641930   6.84170598
Intercept[10]   9.55464230 0.12401111 4.0633199   2.00991320   6.5827207   9.63606982
Intercept[11]  17.32928191 0.18058059 5.8664985   6.73634647  13.4729556  16.93757762
phi[1]          0.56543382 0.01177349 0.2685494  -0.02187386   0.3972104   0.59387129
phi[2]         -0.07473985 0.01222218 0.3013170  -0.67846314  -0.2695174  -0.07868584
phi[3]         -0.21309316 0.01581580 0.3642518  -0.86264280  -0.4797770  -0.22058142
theta[1]        5.65996056 0.13791274 4.1647586  -2.03755919   2.6111322   5.60531775
theta[2]       10.18343109 0.12429771 4.1346106   2.41715481   7.1571874  10.06207776
theta[3]       11.28181377 0.13723356 4.3630684   3.25439068   8.1563613  11.13605511
theta[4]        8.90305231 0.12764299 4.3760935   0.56697446   5.9668145   8.82654073
lp__          -46.82198511 0.21402505 3.5702248 -54.76456947 -48.8048916 -46.33826929
                       75%       97.5%     n_eff      Rhat
theta0          9.24229201  15.6236864  671.9003 1.0024450
beta[1]         0.75413159   2.0703808  769.2118 0.9996928
beta[2]         0.40909431   1.5709185  733.0887 1.0001862
Intercept[1]  -11.79663717  -6.2759799  470.5295 0.9999744
Intercept[2]   -7.60552494  -1.9692638 1179.6385 0.9994127
Intercept[3]   -4.31153205   0.4554698 1474.6869 0.9995625
Intercept[4]   -2.13647482   2.7373154 1241.4479 0.9991600
Intercept[5]    0.03317037   4.8361339 1387.4457 0.9990185
Intercept[6]    2.48364431   6.6368272 1207.0121 0.9994116
Intercept[7]    4.60798435   9.2595222 1023.9356 0.9997249
Intercept[8]    8.35263168  13.4646941  824.5077 0.9997787
Intercept[9]    9.71929549  15.1773088  981.2716 0.9999141
Intercept[10]  12.19482859  17.7019915 1073.5959 0.9990707
Intercept[11]  20.91592393  29.8830983 1055.3969 1.0003892
phi[1]          0.77509512   0.9750913  520.2810 1.0076334
phi[2]          0.12745142   0.5161664  607.7849 1.0009013
phi[3]          0.02433008   0.5359634  530.4212 0.9992130
theta[1]        8.40285139  13.6804988  911.9497 1.0010187
theta[2]       12.99233049  18.4470858 1106.4784 0.9990081
theta[3]       14.26037077  20.4522761 1010.7957 0.9990170
theta[4]       11.74867580  17.9808806 1175.3826 0.9991229
lp__          -44.26685907 -41.1199757  278.2671 1.0035558

If this is correct, then I need to add a loop for more individuals, and another loop to change the response variables. Right?
Should the data be in a long format?
Thanks,

2 Likes