For more on divergences, see resources including Runtime warnings and convergence problems, Identity Crisis, and Taming Divergences in Stan Models. If the number of divergences is not too large, it might be fruitful to increase Stanâ€™s adapt_delta parameter to a value as high as 0.95 or 0.99; exactly how to do so depends on what interface you are using.

One thing that I notice is that in your simulation you have x arising from a uniform distribution, but in your model you have x arising from a normal distribution. Heuristically, the normal prior on x in a measurement error model seems to perform ok even when the true generating distribution isnâ€™t normal, but itâ€™s possible that this mismatch is partially or wholly responsible for the difficult geometry that is giving you problems in fitting.

Thank you. I changed the normal priors on sigma_x and sigmay to inv_gamma(1, 1), and this helped to removing divergent transitions after warmup. However, I donâ€™t know why.

This is strongly suggestive of a funnel geometry. When you trim off the possibility of really small standard deviations via the prior (unlike the half-normal, the inverse gamma sees small values as very unlikely), you trim off the neck of the funnel where curvature is highest and exploration is difficult.