【问题标题】:Conway Maxwell Distribution Density Plot康威麦克斯韦分布密度图
【发布时间】:2018-09-16 08:44:55
【问题描述】:

我编写了自己的代码来模拟康威麦克斯韦分布样本。 这是 pmf (Guikema & Goffelt, 2008): 但是,我在绘制密度图时遇到了一些问题。

rcomp <- function(n,lamb,v)
{
  u <- runif(n)
  w <- integer(n)

  for(i in 1:n) {
    z=sum(sapply(  0:100, function(j) (( ((lamb)^j) / (factorial(j)) )^v)  ))
    x <- seq(1, 50, 1) #seq of 1 to 50, increase by 1
    px <- (((lamb^x)/factorial(x))^v)/z 
    # px is pmf of re-parameter conway maxwell 
    w[i] <- if (u[i] < px[1]) 0 else (max (which (cumsum(px) <= u[i])))
  }
  return (w)
}
dcomp <- function(x,lamb,v) {
z=sum(sapply(  0:100, function(j) (( ((lamb)^j) / (factorial(j)) )^v)  ))
px <- (((lamb^x)/factorial(x))^v)/z

return(px)
}

因为我想绘制密度图以检查lamb 或v 是位置参数,所以我得到的图很奇怪。

x = rcomp(100,6,0.2);  pdf = dcomp(x,6,0.2)
x1 = rcomp(100,6,0.5);  pdf1 = dcomp(x1,6,0.5)
x2 = rcomp(100,6,0.7);  pdf2 = dcomp(x2,6,0.7)
plot(x2, pdf2, type="l", lwd=1,lty=1,col="blue")

我该如何解决这个问题?

来源:Guikema & Goffelt (2008),用于风险分析的灵活计数数据回归模型。风险分析 28(1): 215.

【问题讨论】:

  • 在计算 pdf 之前对 x2 排序:x2 &lt;- rcomp(100, 6, 0.7); x2 &lt;- sort(x2); pdf2 &lt;- dcomp(x2, 6, 0.7)。另外,在上面的示例代码中,为什么要计算 xx1 以及它们各自的 pdf 值? (没什么问题,只是不是minimal example。)
  • 感谢您的建议,实际上,我想在一张图中绘制 3 条曲线(x、x1 和 x2)。我会先尝试排序。
  • 然后将plot 用于第一个,lines 用于其他,就像我在回答问题时所做的那样。

标签: r simulation


【解决方案1】:

如果您希望图表按轴顺序连接点,则必须对 x 坐标的值进行排序。
但是请注意,可能有更好的方法来绘制您想要的密度。见红色曲线。我首先创建一个向量x,其中包含一定范围内的值,然后计算这些值的 PDF。这些对(x, y)lines 绘制的函数。

set.seed(2673)    # Make the results reproducible

x2 <- rcomp(100, 6, 0.7)
x2 <- sort(x2)
pdf2 <- dcomp(x2, 6, 0.7)

plot(x2, pdf2, type = "l", lwd = 1, lty = 1, col = "blue")

x <- seq(0, 50, length.out = 100)
y <- dcomp(x, 6, 0.2)

lines(x, y, type = "l", col = "red")

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 2017-04-10
    • 2020-08-26
    • 2017-08-09
    • 1970-01-01
    • 2018-11-24
    • 2015-08-05
    • 1970-01-01
    • 2015-02-21
    相关资源
    最近更新 更多