Skip to content
Snippets Groups Projects
code_jung_binarized.stan 3.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • Riku-Laine's avatar
    Riku-Laine committed
    data {
      int<lower=1> D; // Dimensions
      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> M; // Number of judges
      int<lower=1> K; // Number of groups
      real<lower=0> sigma_tau;
      int<lower=1, upper=M> jj_obs[N_obs];   // judge_ID
      int<lower=1, upper=M> jj_cens[N_cens]; // judge_ID
      int<lower=1, upper=K> kk_obs[N_obs];   // Grouping
      int<lower=1, upper=K> kk_cens[N_cens]; // Grouping
      int<lower=0, upper=1> dec_obs[N_obs];   // Positive decisions
      int<lower=0, upper=1> dec_cens[N_cens]; // Negative decisions
      row_vector[D] X_obs[N_obs];   // Private features of "observed observations" (with T = 1)
      row_vector[D] X_cens[N_cens]; // Private features of "censored observations" (with T = 1)
      int<lower=0, upper=1> y_obs[N_obs]; // Observed outcomes
    }
    
    parameters {
      vector[N_obs] Z_obs;
      vector[N_cens] Z_cens;
      real alpha_T[M];  // Judge-specific intercepts
      
    
      vector[D] beta_XT_raw[K]; 
      vector[D] beta_XY_raw[K];
    
    Riku-Laine's avatar
    Riku-Laine committed
      
      vector<lower=0>[K] beta_ZT_raw; // Coefficient for the latent variable.
      vector<lower=0>[K] beta_ZY_raw; // Coefficient for the latent variable.
      
      real<lower=0> tau_XT;
      real<lower=0> tau_XY;
      real<lower=0> tau_ZT;
      real<lower=0> tau_ZY;
    }
    
    transformed parameters{
    
      vector[D] beta_XT[K]; 
      vector[D] beta_XY[K];
    
    
    Riku-Laine's avatar
    Riku-Laine committed
      vector<lower=0>[K] beta_ZT_cumulative;
      vector<lower=0>[K] beta_ZY_cumulative;
      
      vector<lower=0>[K] beta_ZT;
      vector<lower=0>[K] beta_ZY;
      
      if(K >= 2){
    
        beta_XT[1] = beta_XT_raw[1];
        beta_XY[1] = beta_XY_raw[1];
        
        for(i in 2:K){ // random walk prior here
          beta_XT[i] = tau_XT * beta_XT_raw[i-1]; // ith group
          beta_XY[i] = tau_XY * beta_XY_raw[i-1];
        }
        
        beta_ZT[1] = beta_ZT_raw[1];
        beta_ZY[1] = beta_ZY_raw[1];
        
    
    Riku-Laine's avatar
    Riku-Laine committed
        beta_ZT[2:] = tau_ZT * beta_ZT_raw[2:];
        beta_ZY[2:] = tau_ZY * beta_ZY_raw[2:];
    
        
      } else {
        // if only one group, constrain variances with the tau prior
        beta_XT[1] = tau_XT * beta_XT_raw[1];
        beta_XY[1] = tau_XY * beta_XY_raw[1];
        beta_ZT = tau_ZT * beta_ZT_raw;
        beta_ZY = tau_ZY * beta_ZY_raw;
    
    Riku-Laine's avatar
    Riku-Laine committed
      }
      
      beta_ZT_cumulative = cumulative_sum(beta_ZT);
      beta_ZY_cumulative = cumulative_sum(beta_ZY);
    }
    
    model {
    
      Z_obs  ~ normal(0, 1);
      Z_cens ~ normal(0, 1);
    
    Riku-Laine's avatar
    Riku-Laine committed
      
      tau_XT ~ normal(0, sigma_tau);
    
      tau_XY ~ normal(0, sigma_tau);
    
    Riku-Laine's avatar
    Riku-Laine committed
      tau_ZT ~ normal(0, sigma_tau);
    
      tau_ZY ~ normal(0, sigma_tau);
    
    Riku-Laine's avatar
    Riku-Laine committed
      
    
      for(i in 1:K){
        beta_XT_raw[i] ~ normal(0, 1);
        beta_XY_raw[i] ~ normal(0, 1);
    
    Riku-Laine's avatar
    Riku-Laine committed
      }
    
      beta_ZT_raw ~ normal(0, 1);
      beta_ZY_raw ~ normal(0, 1);
    
    Riku-Laine's avatar
    Riku-Laine committed
    
      for(i in 1:N_obs){
        dec_obs[i] ~ bernoulli_logit(alpha_T[jj_obs[i]] + X_obs[i] * beta_XT[kk_obs[i]] + beta_ZT_cumulative[kk_obs[i]] * Z_obs[i]);
        y_obs[i] ~ bernoulli_logit(X_obs[i] * beta_XY[kk_obs[i]] + beta_ZY_cumulative[kk_obs[i]] * Z_obs[i]);
      }
      
      for(i in 1:N_cens)
        dec_cens[i] ~ bernoulli_logit(alpha_T[jj_cens[i]] + X_cens[i] * beta_XT[kk_cens[i]] + beta_ZT_cumulative[kk_cens[i]] * Z_cens[i]);
    }
    
    generated quantities {
    
      int<lower=0, upper=1> y_est[N_cens];
    
    Riku-Laine's avatar
    Riku-Laine committed
      
      for(i in 1:N_cens){
    
        y_est[i] = bernoulli_logit_rng(X_cens[i] * beta_XY[kk_cens[i]] + beta_ZY_cumulative[kk_cens[i]] * Z_cens[i]);