我是这个网站的新手,也是我在R的第一步。
我在github上找到了类似的代码(用于二项分布),并试图应用于泊松分布。结果是......呃......:/ 任何人都可以帮助我让这些情节出现吗?有什么问题?
我需要的是具有先验,后验和最大可能性的情节。 我也试图使用dgamma(x,a,b),但结果出乎意料..
GammaPriorPostPlot <- function(a,b,n,p){
standarizedLikelihood = dgamma(dane2, n*p+1, n*(1-p)+1)
prior = dgamma(dane2, a, b)
posterior = dgamma(dane2, a+n*p, b+n*(1-p))
maxDensity <- max(standarizedLikelihood, prior, posterior) # Use to make the y-axis high enough
plot(dane2, standarizedLikelihood, type = 'l', lwd = 3, col = "blue", xlim <- c(0,1), ylim <- c(0, maxDensity), xlab = "theta",
ylab = 'density', main = 'Gamma(a,b)')
lines(dane2, posterior, lwd = 3, col = "red")
lines(dane2, prior, lwd = 3, col = "green")
legend(x = 0.01,
y = maxDensity*0.95,
legend = c("likelihood", "Prior", "Posterior"),
col = c("blue","green","red"),
lwd = c(3,3,3), cex = 0.7)
}
manipulate(GammaPriorPostPlot(a,b,n,p),
a = slider(1, 100, step=1, initial = 100, label = "a"),
b = slider(1, 100, step=1, initial = 2, label = "b"),
n = slider(1, 1000, step=1, initial = 10, label = "num of observations"),
p = slider(0, 1, step=0.01, initial = 0.4, label = "success probability")
我的数据是:
rok t l.kobiet
2002 1 51
2003 2 45
2004 3 41
2005 4 46
2006 5 44
2007 6 67
2008 7 81
2009 8 63
2010 9 61
2011 10 44
2012 11 59
2013 12 48
我的数据只占第三列(女性人数,l.kobiet)