change.stan 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. functions {
  2. vector z_scale(vector x) {
  3. return (x-mean(x))/sd(x);
  4. }
  5. }
  6. data {
  7. int<lower=1> N;
  8. int<lower=1> K;
  9. vector<lower=0>[N] soc_cap;
  10. vector<lower=0>[N] soc_div;
  11. vector<lower=0>[N] int_div;
  12. vector[N] res_soc_div;
  13. //vector<lower=0>[N] age;
  14. vector<lower=0>[N] m;
  15. matrix<lower=0,upper=1>[N,K] x;
  16. vector[N] stable;
  17. //vector<lower=0>[N] age;
  18. array [N] int<lower=0,upper=K-1> primary_research_area;
  19. }
  20. transformed data {
  21. vector[N] z_m = z_scale(m);
  22. vector[N] z_soc_cap = z_scale(soc_cap);
  23. vector[N] z_soc_div = z_scale(soc_div);
  24. vector[N] z_int_div = z_scale(int_div);
  25. vector[N] z_res_soc_div = z_scale(res_soc_div);
  26. }
  27. parameters {
  28. real beta_soc_cap;
  29. real beta_soc_div;
  30. real beta_int_div;
  31. real beta_stable;
  32. //real beta_age;
  33. vector[K] beta_x;
  34. real mu;
  35. real<lower=0> tau;
  36. //real<lower=1> sigma;
  37. real<lower=0> sigma;
  38. vector<lower=0,upper=1>[K] mu_x;
  39. vector<lower=1>[K] eta;
  40. real<lower=0,upper=1> mu_pop;
  41. real<lower=1> eta_pop;
  42. }
  43. model {
  44. vector[N] beta_research_area;
  45. for (k in 1:N) {
  46. beta_research_area[k] = beta_x[primary_research_area[k]+1]*tau;
  47. }
  48. beta_soc_cap ~ normal(0, 1);
  49. beta_soc_div ~ normal(0, 1);
  50. beta_int_div ~ normal(0, 1);
  51. beta_x ~ double_exponential(0, 1);
  52. beta_stable ~ normal(0, 1);
  53. //beta_age ~ normal(0, 1);
  54. mu ~ normal(0, 1);
  55. tau ~ exponential(1);
  56. //sigma ~ pareto(1, 1.5);
  57. sigma ~ exponential(1);
  58. //m ~ beta_proportion(inv_logit(beta_soc_cap*z_soc_cap + beta_soc_div*res_soc_div + beta_int_div*z_int_div + beta_stable*stable + beta_research_area + mu), sigma);
  59. z_m ~ normal(beta_soc_cap*z_soc_cap + beta_soc_div*z_res_soc_div + beta_int_div*z_int_div + beta_stable*stable + beta_research_area + mu, sigma);
  60. eta ~ pareto(1, 1.5);
  61. mu_x ~ uniform(0, 1);
  62. eta_pop ~ pareto(1, 1.5);
  63. mu_pop ~ uniform(0, 1);
  64. for (k in 1:N) {
  65. m[k] ~ beta_proportion(mu_x[primary_research_area[k]+1], eta[primary_research_area[k]+1]);
  66. }
  67. m ~ beta_proportion(mu_pop, eta_pop);
  68. }
  69. generated quantities {
  70. real R2 = 0;
  71. {
  72. vector[N] beta_research_area;
  73. for (k in 1:N) {
  74. beta_research_area[k] = beta_x[primary_research_area[k]+1]*tau;
  75. }
  76. //vector[N] pred = inv_logit(beta_soc_cap*z_soc_cap + beta_soc_div*res_soc_div + beta_int_div*z_int_div + beta_stable*stable + beta_research_area + mu);
  77. vector[N] pred = beta_soc_cap*z_soc_cap + beta_soc_div*z_res_soc_div + beta_int_div*z_int_div + beta_stable*stable + beta_research_area + mu;
  78. R2 = mean(square(z_m-pred))/variance(z_m);
  79. R2 = 1-R2;
  80. }
  81. }