Demo entry 5960161

C

   

Submitted by anonymous on Sep 10, 2016 at 01:44
Language: C++. Code size: 2.3 kB.

functions{
  // sum a matrix, by rows
  vector row_sums(matrix X) 
  {
  vector[rows(X)] s ;  
  s <- X * rep_vector(1, cols(X)) ;
  return s;
  }
} /* end user functions{} block */
  
  data{
    int<lower=1> N; // number of domains
    row_vector[N] y; // Set of N, T x 1 multivariate observations
    row_vector<lower=0>[N] sigma_y; // known standard deviations
    row_vector[N] x; // matrix of (time-indexed) predictors
  } /* end data block */
  
  parameters{
    row_vector[N] u; /* random effects */
      real mu; /* mean (global intercept) of u_i */
      real<lower=0> tau_mu; /* precision in prior for mu */
        real<lower=0> lambda; 
        real<lower=0> lambda_u; 
        real beta;
        real<lower=0> nu;
        row_vector<lower=0>[N] delta;
  } /* end parameters block */
  
  transformed parameters{
    row_vector[N] fitted_values; /* fitted values */
      row_vector<lower=0>[N] sqrt_delta;
      { /* block to declare local variables, xb */
          row_vector[N] xb;
        for( i in 1:N )
        {
          xb[i]                 <- x[i] * beta;
          fitted_values[i]      <- xb[i] + u[i];
          sqrt_delta[i]         <- sqrt( delta[i] );
        } /* end loop i over domains */
      } /* end local block to declare xb */
  } /* end transformed parameters block */
  
  model{
    // prior N x T random effects, u 
    { /* local block for priors for T x T CAR precision */
      lambda_u    ~ gamma( 1.0, 1.0 );
    tau_mu      ~ gamma( 1.0, 1.0 );
    /* prior for T x 1 mu */
      mu   ~ normal(0,inv(sqrt(tau_mu))); 
    u    ~ normal(mu,inv(sqrt(lambda_u))); /* N x 1 vectorized */
    } /* end local block for cluster parameters */
      
      // prior for time-indexed fixed effect, beta
    // prior on precision, lambda
    lambda                    ~ gamma( 1.0, 1.0 );
    /* Implement a beta ~ N_{T}(0,Q^{-1})  */
      beta                      ~ normal(0,inv(sqrt(lambda)));
    
    // observed response likelihood
    nu        ~ inv_gamma( 1.0, 1.0 );
    delta     ~ inv_gamma( 0.5*nu, 0.5*nu ); /* N x 1 vectorized */
      y         ~ normal( fitted_values, sigma_y .* sqrt_delta );
    
  } /* end model{} block */
  

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).