2021年5月1日星期六

How to plot density functions with different length in R

I want to plot three different functions, where two of them are continous and only one is discrete. This is how I generate the three of them:

Packages I am using:

library(tidyr)  library(ggplot2)  library(MCMCpack)  library(plotfunctions)  
X=rpois(20,20) #data generation  alpha=sum(X)+64  theta=seq(0,30,by=0.01)    #Priori  n=length(X)  priori=dgamma(theta,64,4)    #Likelihood  theta1=seq(0,30)  likelihood=dpois(X,theta)    #Posteriori    posteriori=dgamma(theta,shape = alpha, rate = n+4)  

This is the plot I am using:

plot(theta,priori,type="l",col="black",ylim = c(0,0.5),xlab = expression(paste(theta)),ylab = "Density")  add_bars(theta,likelihood)  lines(theta,posteriori,type="l",col="green")  legend(21,0.5,legend=c("Priori","Likelihood","Posteriori"),col = c("black","red","green"),lty = 1:3,bty="n")  

When I try to run it, I get the follwing message:

Error in add_bars(theta, likelihood) : Different lengths of x and y.

How can I include the likelihood density within the plot?

https://stackoverflow.com/questions/67352414/how-to-plot-density-functions-with-different-length-in-r May 02, 2021 at 10:11AM

没有评论:

发表评论