Using panel.mathdensity and panel.densityplot in lattice graphics to plot Bayesian prior and posterior

StackOverflow https://stackoverflow.com/questions/19452213

  •  01-07-2022
  •  | 
  •  

Question

I am trying to plot a Bayesian prior and posterior distribution using lattice graphics. I would like to have both distributions in one panel, for direct comparison.

I've tried different solutions all day, including qqmath but I didn't get them to work. Here's the attempt that has been most successful so far:

# my data
d <- dgamma(seq(from=0.00001,to=0.01,by=0.00001),shape = .1, scale = .01)

# my plot
densityplot(~d,
        plot.points=FALSE,
        panel = function(x,...) {
          panel.densityplot(x,...)
          panel.mathdensity(
            dmath = dgamma,
            args = list(shape = .1, scale=.01)
            )
        }
        )

Even though the code runs through nicely, it doesn't do what I want it to. It plots the posterior (d) but not the prior.

I added stop("foo") to densityplot(...) to stop execution if an error occurs and I searched online for the error message:

Error in eval(substitute(groups), data, environment(formula)) : foo

But there are only a few results and they seem unrelated to me.

So, here's my question: Can anyone help me with this approach to achieve what I want?

Was it helpful?

Solution

I asked a similar question which leads to the same result. I got an answer and it was useful. You can find everything here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top