# Missing data

**URL:** <https://discourse.mc-stan.org/t/missing-data/13412>\
**Category:** Modeling\
**Created:** [February 29, 2020, 7:19pm UTC](https://discourse.mc-stan.org/t/missing-data/13412 "2020-02-29T19:19:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [February 29, 2020, 7:19pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/1 "2020-02-29T19:19:25Z")

</div>

Hi all  
How can I solve the problem of missing data (NA) in this example?

data \<- read.table(‘data.txt’,sep="")  
mle \<- read.table(‘mle.txt’,sep="")  
ns \<- ncol(data)  
nt \<- nrow(data)

# pass prior standard devs as data

gev\_data = list(ns=ns, nt=nt, y=data)

initf\_manual = list(list(mu=mle[,1], sigma=mle[,2], xi=rep(0.01,ns)))

# run stan

model\_file = “model\_loc\_0.stan”  
stan\_model = stan(model\_file, data = gev\_data, ini = initf\_manual,iter = 3500,  
chains = 1, control=list(adapt\_delta=0.85))

The san model:

```
functions{
  vector gev_v_log_v(vector y, real mu, real sigma, real xi) {
    real inv_xi;
    real neg_inv_xi; 
    real inv_xi_p1; 
    vector[rows(y)] z;
    vector[rows(y)] lp;
    int N;
    N = rows(y);
    z = 1 + (y - mu) * xi / sigma;
    inv_xi = 1 / xi;
    neg_inv_xi = -inv_xi; 
    inv_xi_p1 = 1 + inv_xi; 
    
    for(n in 1:N){
        lp[n] = log(sigma) + inv_xi_p1*log(z[n]) + pow(z[n],neg_inv_xi);
      }
    
    return -lp;
  }
}

data {
  int<lower=1> ns; // number of sites
  int<lower=1> nt; // number of times
  vector [nt] y[ns]; //data
}

parameters {
  real<lower=0> mu[ns];
  real<lower=0> sigma[ns];
  real<lower=-1,upper=1> xi[ns];
}

model {
  vector[nt] lp[ns];

  for (i in 1:ns){
       lp[i] = gev_v_log_v(y[i], mu[i], sigma[i], xi[i]);
  }
}

```

[data.txt|attachment](uploa[data.txt](https://discourse.mc-stan.org/uploads/short-url/pztMiBLXTY6rYKHZqhjyhZ3TiFn.txt) (46.2 KB) [mle.txt](https://discourse.mc-stan.org/uploads/short-url/naAzXUxXNscThiqCk6YUmeR2B2z.txt) (2.5 KB) d://pztMiBLXTY6rYKHZqhjyhZ3TiFn.txt) (46.2 KB)

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/emiruz/32/5518_2.png) [@emiruz](https://discourse.mc-stan.org/u/emiruz)\
**Post date:** [March 1, 2020, 12:45pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/2 "2020-03-01T12:45:24Z")

</div>

Hi! Have a look here for some general advise on how to approach this: [https://mc-stan.org/docs/2\_20/stan-users-guide/missing-data.html](https://mc-stan.org/docs/2_20/stan-users-guide/missing-data.html)

I think the typical route is to declare your missing data separately to your observed data . Your observed data is declared in the data block, missing data is declared in the parameters block and treated as a parameter with the same distribution as your observed data. The example at the very top of the link above should give you a good sense of what to do.

Note also, so far as I can see you model is incomplete since it doesn’t use a sampling statement or target+=.

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [March 1, 2020, 1:14pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/3 "2020-03-01T13:14:19Z")

</div>

Thank you so much Emir for your clarifications  
But the problem is that all observed datasets contained missing data

---

<div class="post-metadata">

**Author:** ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)\
**Post date:** [March 1, 2020, 1:32pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/4 "2020-03-01T13:32:15Z")

</div>

You could use the GNU R package `AMELIA II` to impute your dataset. From that you have something defined to work on and use that for comparison of your Stan Imputation.

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/emiruz/32/5518_2.png) [@emiruz](https://discourse.mc-stan.org/u/emiruz)\
**Post date:** [March 1, 2020, 2:31pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/5 "2020-03-01T14:31:49Z")

</div>

You could also have a look at Bayesian PPCA for missing value imputation in the case of multivariate data with missing values. I personally don’t know a way to do imputation for multivariate data in Stan directly.

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [March 1, 2020, 7:44pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/6 "2020-03-01T19:44:20Z")

</div>

Thank you so much I will try with it

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [March 1, 2020, 7:45pm UTC](https://discourse.mc-stan.org/t/missing-data/13412/7 "2020-03-01T19:45:29Z")

</div>

Thank you so much Emir  
I will try to use it

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [March 2, 2020, 7:14am UTC](https://discourse.mc-stan.org/t/missing-data/13412/8 "2020-03-02T07:14:04Z")

</div>

Hi Lazhar,

is there missingness only in the outcome or is there missingness also in one or more predictors?

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [May 8, 2020, 3:55am UTC](https://discourse.mc-stan.org/t/missing-data/13412/9 "2020-05-08T03:55:21Z")

</div>

Hi Torkar  
Thank you so much for your message and help  
I solve the problem by removing the missing data and take it as one large vector
