entered.stan 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. vector<lower=0>[N] productivity;
  19. vector<lower=0>[N] productivity_solo;
  20. }
  21. transformed data {
  22. vector[N] z_m = z_scale(to_vector(m));
  23. vector[N] z_soc_cap = z_scale(soc_cap);
  24. vector[N] z_soc_div = z_scale(soc_div);
  25. vector[N] z_int_div = z_scale(int_div);
  26. vector[N] z_res_soc_div = z_scale(res_soc_div);
  27. vector[N] z_age = z_scale(age);
  28. vector[N] z_productivity = z_scale(productivity);
  29. vector[N] z_productivity_solo = z_scale(productivity_solo);
  30. }
  31. parameters {
  32. real beta_soc_cap;
  33. real beta_soc_div;
  34. real beta_int_div;
  35. real beta_stable;
  36. real beta_age;
  37. real beta_productivity;
  38. real beta_productivity_solo;
  39. vector[K] beta_x;
  40. real mu;
  41. real<lower=0> tau;
  42. real<lower=1> sigma;
  43. }
  44. model {
  45. vector[N] beta_research_area;
  46. for (k in 1:N) {
  47. beta_research_area[k] = tau*beta_x[primary_research_area[k]+1];
  48. }
  49. beta_soc_cap ~ normal(0, 1);
  50. beta_soc_div ~ normal(0, 1);
  51. beta_int_div ~ normal(0, 1);
  52. beta_x ~ double_exponential(0, 1);
  53. beta_stable ~ normal(0, 1);
  54. beta_age ~ normal(0, 1);
  55. beta_productivity ~ normal(0, 1);
  56. beta_productivity_solo ~ normal(0, 1);
  57. mu ~ normal(0, 1);
  58. tau ~ exponential(1);
  59. sigma ~ pareto(1,1.5);
  60. 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_age*z_age + beta_productivity*z_productivity + beta_productivity_solo*z_productivity_solo + beta_research_area + mu);
  61. }
  62. generated quantities {
  63. real R2 = 0;
  64. {
  65. vector[N] beta_research_area;
  66. for (k in 1:N) {
  67. beta_research_area[k] = beta_x[primary_research_area[k]+1]*tau;
  68. }
  69. 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_age*z_age + beta_productivity*z_productivity + beta_productivity_solo*z_productivity_solo + beta_research_area + mu);
  70. R2 = mean(square(to_vector(m)-pred))/variance(m);
  71. R2 = 1-R2;
  72. }
  73. }