# Linear model with measurement error in X and Y

**URL:** <https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [March 30, 2018, 12:19am UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729 "2018-03-30T00:19:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![emittman](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@emittman](https://discourse.mc-stan.org/u/emittman)\
**Post date:** [March 30, 2018, 12:19am UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/1 "2018-03-30T00:19:27Z")

</div>

My question is somewhat related to other examples, such as here: [Measurement error? model](http://discourse.mc-stan.org/t/measurement-error-model/1842),  
but my efforts to follow the guidance I’ve read have not been successful.

I am considering the case where we want to estimate a linear relationship between two variables, X and Y, where both are measured/observed with error. To help identify the parameters, I assume that I know the standard deviation of the error in measuring X.

I am simulating data by selecting points on a line and perturbing them. Even when I specify the correct value of sigma\_X, I am seeing loads of divergent transitions.

I am interested in using Stan to fit more general versions of this model. Could you all help me get it off the ground?

[meas\_err\_v0.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/c/cba4671d8875c00438b111e4ca2285f2137344fc.stan) (417 Bytes)  
[simple\_ex.csv](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/d/d14fcaf114d14a77f5097f5b8d0f66261924ca10.csv) (1.4 KB)  
[error\_in\_both\_X\_and\_Y.R](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/e/e0c3c4512b96cab5c7e609e304362df267e2c477.R) (939 Bytes)

Warning messages:  
1: There were 230 divergent transitions after warmup. Increasing adapt\_delta above 0.99 may help. See  
[http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup](http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup)  
2: There were 4 chains where the estimated Bayesian Fraction of Missing Information was low. See  
[http://mc-stan.org/misc/warnings.html#bfmi-low](http://mc-stan.org/misc/warnings.html#bfmi-low)  
3: Examine the pairs() plot to diagnose sampling problems

> sessionInfo()  
> R version 3.4.3 (2017-11-30)  
> Platform: x86\_64-w64-mingw32/x64 (64-bit)  
> Running under: Windows \>= 8 x64 (build 9200)

Matrix products: default

locale:  
[1] LC\_COLLATE=English\_United States.1252 LC\_CTYPE=English\_United States.1252  
[3] LC\_MONETARY=English\_United States.1252 LC\_NUMERIC=C  
[5] LC\_TIME=English\_United States.1252

attached base packages:  
[1] stats graphics grDevices utils datasets methods base

other attached packages:  
[1] plyr\_1.8.4 rstan\_2.17.3 StanHeaders\_2.17.2 ggplot2\_2.2.1

loaded via a namespace (and not attached):  
[1] Rcpp\_0.12.15 codetools\_0.2-15 grid\_3.4.3 gtable\_0.2.0 stats4\_3.4.3  
[6] scales\_0.5.0 KernSmooth\_2.23-15 pillar\_1.1.0 rlang\_0.2.0 lazyeval\_0.2.1  
[11] labeling\_0.3 tools\_3.4.3 munsell\_0.4.3 yaml\_2.1.16 compiler\_3.4.3  
[16] inline\_0.3.14 colorspace\_1.3-2 gridExtra\_2.3 tibble\_1.4.2

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 31, 2018, 2:58pm UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/2 "2018-03-31T14:58:29Z")

</div>

You’re missing a zero lower bound on the variance parameter sigma\_y. You can also nix the for loops by defining things as vectors rather that arrays of reals.

---

<div class="post-metadata">

**Author:** ![emittman](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@emittman](https://discourse.mc-stan.org/u/emittman)\
**Post date:** [April 2, 2018, 9:59pm UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/3 "2018-04-02T21:59:02Z")

</div>

Oops. Thanks for your help. After making these changes, issues remain:

Warning messages:  
1: There were 25 divergent transitions after warmup. Increasing adapt\_delta above 0.9999 may help. See  
[http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup](http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup)  
2: There were 2 chains where the estimated Bayesian Fraction of Missing Information was low. See  
[http://mc-stan.org/misc/warnings.html#bfmi-low](http://mc-stan.org/misc/warnings.html#bfmi-low)  
3: Examine the pairs() plot to diagnose sampling problems

Furthermore, fitting this model over several similarly simulated data sets, beta is consistently estimated to be less than 1. I’m looking for an explanation as to why.

[meas\_err\_v0.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/3/3252631fa0c0e6ab1a4b613e08afd6eb739a6338.stan) (381 Bytes)

---

<div class="post-metadata">

**Author:** ![ZHENG\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@ZHENG\_Yi](https://discourse.mc-stan.org/u/ZHENG_Yi)\
**Post date:** [April 3, 2018, 2:08am UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/4 "2018-04-03T02:08:40Z")

</div>

I have made a change of your model. I sincerely hope it could help.

I suppose that you don’t need to set adapt\_delta that big. 0.9 is OK.

[meas\_err\_v0.change\_by\_ZY.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/3/327fb32a03c264339218a5c70b0dca89531e8380.stan) (565 Bytes)

---

<div class="post-metadata">

**Author:** ![emittman](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@emittman](https://discourse.mc-stan.org/u/emittman)\
**Post date:** [April 3, 2018, 9:56pm UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/5 "2018-04-03T21:56:37Z")

</div>

Thanks, I think your changes make the model easier to comprehend. Unfortunately, I continue to get divergent transitions. Here is an updated .stan model file incorporating all suggested changes so far.

[meas\_err\_v1.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/f/f6d924a884ab7b0ed41715559e50593bc5c66be3.stan) (368 Bytes)

---

<div class="post-metadata">

**Author:** ![ZHENG\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@ZHENG\_Yi](https://discourse.mc-stan.org/u/ZHENG_Yi)\
**Post date:** [April 4, 2018, 1:57am UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/6 "2018-04-04T01:57:00Z")

</div>

I reparammeterize “xraw” in the model, set adapt\_delta to 0.95, run two chains of 10,000 iterations each, and still get about 20 divergent transitions.  
As suggested in Brief Guide to Stan website, " it might actually work if the number of divergent transitions is small" . I don’t know whether it’s OK.  
I suppose the problem rises when the parameter “mu” is the product of two parameters “xraw” and “beta”

[error\_in\_both\_X\_and\_Y.n.R](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/c/c94f53d08227952111d4dc0d0051b99c07c4646f.R) (1.3 KB)  
[meas\_err\_v1.n.stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/f/f233df0b2bac876dcc95921f1a2fee73fa9dc85e.stan) (510 Bytes)

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [April 4, 2018, 3:42am UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/7 "2018-04-04T03:42:19Z")

</div>

> [@ZHENG\_Yi](#):
>
> As suggested in Brief Guide to Stan website, " it might actually work if the number of divergent transitions is small" . I don’t know whether it’s OK.

In case it helps, we discuss this in the MCMC diagnostics section in [[1709.01449] Visualization in Bayesian workflow](https://arxiv.org/abs/1709.01449) and show a few visualizations that sometimes can be used to tell whether or not a small number of divergences is a problem.

---

<div class="post-metadata">

**Author:** ![ZHENG\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@ZHENG\_Yi](https://discourse.mc-stan.org/u/ZHENG_Yi)\
**Post date:** [April 4, 2018, 7:21am UTC](https://discourse.mc-stan.org/t/linear-model-with-measurement-error-in-x-and-y/3729/8 "2018-04-04T07:21:17Z")

</div>

Thanks a lot !  
According to my limited experience, divergent transitions easily emerges if there are transformed parameters which are non-linear functions of two or more pre-declared parameters. When I run my own stan model of Major Axis Regression, there are many divergent transitions. I don’t know how to handle the problem and hope for your help. [Jacobian determinent](http://discourse.mc-stan.org/t/jacobian-determinent/3753/2)  
Thanks again!
