From 9780c5c0e65c67ecc95207060fcbe5b08e0f695e Mon Sep 17 00:00:00 2001
From: Riku-Laine <28960190+Riku-Laine@users.noreply.github.com>
Date: Mon, 20 Jan 2020 11:54:04 +0200
Subject: [PATCH] Updated stan code

---
 .../code_linear_dependency.stan               | 53 +++++++++++--------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/analysis_and_scripts/code_linear_dependency.stan b/analysis_and_scripts/code_linear_dependency.stan
index a85bded..71298d3 100644
--- a/analysis_and_scripts/code_linear_dependency.stan
+++ b/analysis_and_scripts/code_linear_dependency.stan
@@ -1,7 +1,7 @@
 data {
-  int<lower=1> D; // Dimensions of the features an coefficient vectors
-  int<lower=0> N_obs;  // Number of "observed observations" (with T = 1)
-  int<lower=0> N_cens; // Number of "censored observations" (with T = 0)
+  int<lower=1> D; // Dimensions of the features and coefficient vectors
+  int<lower=0> N_obs;  // Number of observed observations (with T = 1)
+  int<lower=0> N_cens; // Number of censored observations (with T = 0)
   int<lower=0> M; // Number of judges
   real<lower=0> sigma_tau; // Prior for the variance parameters.
   int<lower=1, upper=M> jj_obs[N_obs];   // judge_ID array
@@ -18,11 +18,12 @@ parameters {
   vector[N_obs] Z_obs;
   vector[N_cens] Z_cens;
   
-  // Intercepts
-  vector<lower=0>[M] sigma_T;
+  // Intercepts and their variance parameters.
+  real<lower=0> sigma_T;
   real<lower=0> sigma_Y;
 
   vector[M] alpha_T_raw;
+  // vector[M] alpha_T;
   real alpha_Y_raw;
 
   // Temporary variables to compute the coefficients
@@ -31,10 +32,8 @@ parameters {
   real<lower=0> a_ZT; // Presume latent variable has a positive coefficient.
   real<lower=0> a_ZY;
 
-  real<lower=0> tau_XT;
-  real<lower=0> tau_XY;
-  real<lower=0> tau_ZT;
-  real<lower=0> tau_ZY;
+  real<lower=0> tau_X; // RL 08DEC2019: Combine taus for X.
+  real<lower=0> tau_Z; // RL 24OCT2019: Combine taus for Z.
 
 }
 
@@ -49,13 +48,18 @@ transformed parameters {
   // Intercepts
   vector[M] alpha_T;
   real alpha_Y;
-
-  beta_XT = a_XT / sqrt(tau_XT);
-  beta_XY = a_XY / sqrt(tau_XY);
-  beta_ZT = a_ZT / sqrt(tau_ZT);
-  beta_ZY = a_ZY / sqrt(tau_ZY);
+ 
+  beta_XT = a_XT / sqrt(tau_X); // RL 08DEC2019: Combine taus for X.
+  beta_XY = a_XY / sqrt(tau_X);
+  beta_ZT = a_ZT / sqrt(tau_Z); // RL 24OCT2019: Combine taus for Z.
+  beta_ZY = a_ZY / sqrt(tau_Z);
+  
+  // beta_XT = a_XT * tau_X; // RL 08DEC2019: Combine taus for X.
+  // beta_XY = a_XY * tau_X;
+  // beta_ZT = a_ZT * tau_Z; // RL 24OCT2019: Combine taus for Z.
+  // beta_ZY = a_ZY * tau_Z;
   
-  alpha_T = sigma_T .* alpha_T_raw;
+  alpha_T = sigma_T * alpha_T_raw;
   alpha_Y = sigma_Y * alpha_Y_raw;
 
 }
@@ -65,11 +69,13 @@ model {
   Z_obs ~ normal(0, 1);
   Z_cens ~ normal(0, 1);
   
-  // Intercepts
+  // Variance parameter for the intercepts
   sigma_T ~ normal(0, sigma_tau);
   sigma_Y ~ normal(0, sigma_tau);
   
+  // Intercepts
   alpha_T_raw ~ normal(0, 1);
+  // alpha_T ~ logistic(0, 1);
   alpha_Y_raw ~ normal(0, 1);
   
   // According to 
@@ -90,11 +96,11 @@ model {
   a_ZY ~ normal(0, 1);
 
   // nu = 6 -> nu/2 = 3
-  tau_XT ~ gamma(3, 3);
-  tau_XY ~ gamma(3, 3);
-  tau_ZT ~ gamma(3, 3);
-  tau_ZY ~ gamma(3, 3);
-  
+  tau_X ~ gamma(3, 3);
+  tau_Z ~ gamma(3, 3);
+  // tau_X ~ normal(0, 1);
+  // tau_Z ~ normal(0, 1);
+
   // Compute the regressions for the observed observations
   for(i in 1:N_obs){
     dec_obs[i] ~ bernoulli_logit(alpha_T[jj_obs[i]] + X_obs[i] * beta_XT + beta_ZT * Z_obs[i]);
@@ -107,9 +113,10 @@ model {
 }
 
 generated quantities {
+  // Array for predictions
   int<lower=0, upper=1> y_est[N_cens];
-  
-  // Generate a draw from the posterior predictive.
+
+  // Generate a draw from the posterior predictive for the outcome.
   for(i in 1:N_cens){
     y_est[i] = bernoulli_logit_rng(alpha_Y + X_cens[i] * beta_XY + beta_ZY * Z_cens[i]);
   }
-- 
GitLab