So, me and a colleague have to impute some data, x, given a categorical variable. We arrived at two different approaches:

a) as in the tutorial: split x into x_obs and x_mis, and treat x_mis as parameters. Something like this:

data {
  int<lower=0> N_obs;
  int<lower=0> N_mis;
  int J; // number of package types
  real<lower = 0.01> x_obs[N_obs];
  int<lower = 1, upper = J> ptype_obs[N_obs];
  int<lower = 1, upper = J> ptype_mis[N_mis];
parameters {
  real<lower = 0.01> x_mis[N_mis];
  matrix<lower = 0.01>[J] mu_x;
  matrix<lower = 0.01, upper=5>[J] sigma_x;

transformed parameters {
  vector<lower = 0.01>[N_obs] x_obs_mu;
  vector<lower = 0.01>[N_obs] x_obs_sigma;
  vector<lower = 0.01>[N_mis] x_mis_mu;
  vector<lower = 0.01>[N_mis] x_mis_sigma;

  for(i in 1:N_obs){
  for(i in 1:N_mis){

model {
  x_obs ~ normal(x_obs_mu,x_obs_sigma);
  x_mis ~ normal(x_mis_mu,x_mis_sigma);

b) saying "There is some real value, x_real, and both x_obs and x_mis tend around it. Here is sample code:

data {
  int N;
  int J;
  real x[N];
  real volume[N];
  int<lower = 0, upper = 1> x_available[N];
  int<lower = 1, upper = J> ptype[N];
parameters {
  vector<lower = 0>[N] x_real;
  vector<lower = 0.1>[J] mu_x_package;
  vector<lower = 0.0>[J] sigma_x_package;

transformed parameters {
  vector<lower = 0.01>[N] x_real_mu;
  vector<lower = 0.01>[N] x_real_mu1;
  vector<lower = 0.01>[N] x_real_sigma;
  vector<lower = 0.01>[N] x_real_sigma1;

  for(i in 1:N){


model {

Now, approach b) is very inelegant in that there is x_real_mu and x_real_mu1, and both are necessary for some reason, which I just can't understand why. If I remove either one, the model converges quickly, but produces nonsensical results.

On the other hand, approach a) is somewhat redundant in that I have to duplicate most of my work.

Which approach is better and why? Also, for b), why do we need both x_real_mu, and x_real_mu1 ?

  • $\begingroup$ You might get better answers if you describe the two imputation strategies in more detail: not everyone here will be able to read your code. $\endgroup$ – mkt - Reinstate Monica Oct 27 '17 at 9:32
  • $\begingroup$ In the model a) it looks very unusual to me that the observed and missing values have separate sampling statements. In my understanding that is not what the Stan manual suggests. $\endgroup$ – winerd Dec 20 '17 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.