Linear model with measurement error in X and Y

My question is somewhat related to other examples, such as here: Measurement error? model,
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 (417 Bytes)
simple_ex.csv (1.4 KB)
error_in_both_X_and_Y.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
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
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

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.

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
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
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 (381 Bytes)

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 (565 Bytes)

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 (368 Bytes)

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 (1.3 KB)
meas_err_v1.n.stan (510 Bytes)

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

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
Thanks again!