# Fitting issues for model with measurement error

**URL:** <https://discourse.mc-stan.org/t/fitting-issues-for-model-with-measurement-error/39249>\
**Category:** Modeling\
**Created:** [April 9, 2025, 4:41pm UTC](https://discourse.mc-stan.org/t/fitting-issues-for-model-with-measurement-error/39249 "2025-04-09T16:41:00Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![cjung](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@cjung](https://discourse.mc-stan.org/u/cjung)\
**Post date:** [April 10, 2025, 12:34pm UTC](https://discourse.mc-stan.org/t/fitting-issues-for-model-with-measurement-error/39249/5 "2025-04-10T12:34:25Z")

</div>

I think I solved it:

So annoyingly, while tighter priors like lognormal(0,1) do a bit better, I still got low E-BFMI warnings and very low ESS for my variance parameters sigma and tau - so the problem really seemed to be something inherent to the model. Simpler models [such as the one I copied from here](https://discourse.mc-stan.org/t/errors-in-variables-model-consistently-underestimates-sigma/30453/2) worked fine, until x\_obs and y\_obs get separate variances. I realized that with the data given, the two variance components of the model remain unidentifiable unless I give them very different scales and very different priors to go with those scales ([see here](https://discourse.mc-stan.org/t/warnings-for-an-errors-in-variables-model-and-the-risk-of-underestimating-the-slope/32870/2)).

I validated this by changing to a repeated measures design, which instantly fixes it as tau can now be estimated from the data:

```r
N <- 100
reps <- 3

sigma <- 0.5
tau <- 0.3
alpha <- 1.3
beta <- -0.4

# true covariate values
x <- runif(n, -3, 3)

#repeatedly sampling true x
idxs <- rep(1:n,each=reps)

x_obs <- rnorm(idxs, x[idxs], tau)
y <- rnorm(idxs, alpha + beta*x[idxs], sigma)

```

and the model:

```stan
data {
  int<lower=0> N;
  int<lower=0> reps;
  vector[N*reps] x_obs;
  vector[N*reps] y;
  array[N*reps] int<lower=0,upper=N> idxs;
}

parameters {
  vector[N] x;
  real alpha;
  real beta;
  real<lower=0> sigma;
  real<lower=0> tau;
}

model {
  //priors
  alpha ~ normal(0, 5);
  beta ~ normal(0, 5);
  sigma ~ lognormal(0,1);
  tau ~ lognormal(0,1);
  x ~ normal(0,5);
  
  //likelihood
  for(i in 1:N*reps){
  x_obs[i] ~ normal(x[idxs[i]],tau);
  y[i] ~ normal(alpha + beta * x[idxs[i]], sigma);

}

}

```

In hindsight, I missed that [tau is data, not a parameter, in the example here](https://mc-stan.org/docs/stan-users-guide/measurement-error.html).

Solved thanks to the forum history!

---

_[View the full topic](https://discourse.mc-stan.org/t/fitting-issues-for-model-with-measurement-error/39249)._
