2d density plot from curves

烂漫一生 提交于 2019-12-14 03:58:17

问题


I have a multi-parameter function on which I infer the parameters using MCMC. This means that I have many samples of the parameters, and I can plot the functions:

# Simulate some parameters. Really, I get these from MCMC sampling.
first = rnorm(1000)  # a
second = rnorm(1000)  # b

# The function (geometric)
geometric = function(x, a, b) b*(1 - a^(x + 1)/a)  

# Plot curves. Perhaps not the most efficient way, but it works.
curve(geometric(x, first[1], second[1]), ylim=c(-3, 3))  # first curve
for(i in 2:length(first)) {
  curve(geometric(x, first[i], second[i]), add=T, col='#00000030')  # add others
}

How do I make this into a density plot instead of plotting the individual curves? For example, it's hard to see just how much denser it is around y=0 than around other values.

The following would be nice:

  1. The ability to draw observed values on top (points and lines).
  2. Drawing a contour line in the density, e.g. the 95% Highest Posterior Density interval or the 2.5 and 97.5 quantiles.

来源:https://stackoverflow.com/questions/37877490/2d-density-plot-from-curves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!