default.txt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Multivariate Regression Example
  2. // Taken from stan-reference-2.8.0.pdf p.66
  3. data {
  4. int<lower=0> N; // num individuals
  5. int<lower=1> K; // num ind predictors
  6. int<lower=1> J; // num groups
  7. int<lower=1> L; // num group predictors
  8. int<lower=1,upper=J> jj[N]; // group for individual
  9. matrix[N,K] x; // individual predictors
  10. row_vector[L] u[J]; // group predictors
  11. vector[N] y; // outcomes
  12. }
  13. parameters {
  14. corr_matrix[K] Omega; // prior correlation
  15. vector<lower=0>[K] tau; // prior scale
  16. matrix[L,K] gamma; // group coeffs
  17. vector[K] beta[J]; // indiv coeffs by group
  18. real<lower=0> sigma; // prediction error scale
  19. }
  20. model {
  21. tau ~ cauchy(0,2.5);
  22. Omega ~ lkj_corr(2);
  23. to_vector(gamma) ~ normal(0, 5);
  24. {
  25. row_vector[K] u_gamma[J];
  26. for (j in 1:J)
  27. u_gamma[j] <- u[j] * gamma;
  28. beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau));
  29. }
  30. {
  31. vector[N] x_beta_jj;
  32. for (n in 1:N)
  33. x_beta_jj[n] <- x[n] * beta[jj[n]];
  34. y ~ normal(x_beta_jj, sigma);
  35. }
  36. }
  37. # Note: Octothorpes indicate comments, too!