How to plot Bayesian prior and posterior distributions in one panel using R?

限于喜欢 提交于 2019-12-14 03:48:39

问题


I've tried several approaches, including qqmath, lattice densityplot() and a number of panel functions like panel.mathdensity and panel.densityplot. However, I couldn't get them to do what I want them to do.

An internet search on this topic produces results which focus either on base plots in R or don't draw both distributions in one panel. I could use R base graphics, however, I also want to plot several distribution pairs and one panel for each pair.

The books "RGraphics" and "Lattice: Multivariate Data Visualization with R" couldn't enlighten me in this area either.

The data usually looks something like this:

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

I'm open to either lattice or ggplot package, although I have more experience using lattice.

Please let me know if you need any more information to help me out here.


回答1:


You have only given one data-object but are asking for two to be constructed, so I'll try for two items of the same distribution with different parameters. When working with lattice or ggplot2 one need to construct a dataframe of the proper arrangement which is usually a "long" format for the data. The "group" parameter specifies plotting in the same panel with different colors.

require(lattice)
?lattice  # essential reading
dfrm <- data.frame(dgam = data, param="s.1.01")
dfrm <- rbind(dfrm, data.frame(dgam =
                                 dgamma( seq(from=0.00001,to=0.01,by=0.00001), 
                                 shape = .2, scale = .01), 
                               param="s.2.01") )
dfrm <- cbind( dfrm, X.val=seq(from=0.00001,to=0.01,by=0.00001) )
str(dfrm)
#'data.frame':  2000 obs. of  3 variables:
# $ dgam : num  5263 2817 1954 1507 1231 ...
# $ param: Factor w/ 2 levels "s.1.01","s.2.01": 1 1 1 1 1 1 1 1 1 1 ...
# $ X.val: num  1e-05 2e-05 3e-05 4e-05 5e-05 6e-05 7e-05 8e-05 9e-05 1e-04 ...
xyplot( dgam ~ X.val , 
        group=param, 
        data=dfrm, type="l")


来源:https://stackoverflow.com/questions/19476744/how-to-plot-bayesian-prior-and-posterior-distributions-in-one-panel-using-r

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