entered.stan 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. array[N] int<lower=0,upper=1> m;
  15. matrix<lower=0,upper=1>[N,K] x;
  16. vector[N] stable;
  17. array [N] int<lower=0,upper=K-1> primary_research_area;
  18. }
  19. transformed data {
  20. //vector[N] z_m = z_scale(m);
  21. vector[N] z_soc_cap = z_scale(soc_cap);
  22. vector[N] z_soc_div = z_scale(soc_div);
  23. vector[N] z_int_div = z_scale(int_div);
  24. vector[N] z_res_soc_div = z_scale(res_soc_div);
  25. }
  26. parameters {
  27. real beta_soc_cap;
  28. real beta_soc_div;
  29. real beta_int_div;
  30. real beta_stable;
  31. vector[K] beta_x;
  32. real mu;
  33. real<lower=0> tau;
  34. real<lower=1> sigma;
  35. // vector[K] mu_x;
  36. // vector<lower=0>[K] sigma_x;
  37. // real mu_pop;
  38. // real<lower=0> sigma_pop;
  39. }
  40. model {
  41. vector[N] beta_research_area;
  42. for (k in 1:N) {
  43. beta_research_area[k] = tau*beta_x[primary_research_area[k]+1];
  44. }
  45. beta_soc_cap ~ normal(0, 1);
  46. beta_soc_div ~ normal(0, 1);
  47. beta_int_div ~ normal(0, 1);
  48. beta_x ~ double_exponential(0, 1);
  49. beta_stable ~ normal(0, 1);
  50. mu ~ normal(0, 1);
  51. tau ~ exponential(1);
  52. sigma ~ pareto(1,1.5);
  53. m ~ bernoulli_logit(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);
  54. // m ~ beta(mu_pop, sigma_pop);
  55. // for (k in 1:N) {
  56. // m[k] ~ normal(mu_x[primary_research_area[k]+1], sigma_x[primary_research_area[k]+1]);
  57. // }
  58. // mu_x ~ normal(0, 1);
  59. // mu_pop ~ normal(0, 1);
  60. // sigma_x ~ exponential(1);
  61. // sigma_pop ~ exponential(1);
  62. }
  63. generated quantities {
  64. // real R2 = 0;
  65. // {
  66. // vector[N] beta_research_area;
  67. // for (k in 1:N) {
  68. // beta_research_area[k] = beta_x[primary_research_area[k]+1];
  69. // }
  70. // vector[N] pred = inv_logit(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);
  71. // R2 = mean(square(z_m-pred));
  72. // R2 = 1-R2;
  73. // }
  74. }