【问题标题】:Plot theoretical inverise-chi-square distribution绘制理论逆卡方分布
【发布时间】:2021-01-13 14:42:12
【问题描述】:

我想将我的“经验”数据与理论逆卡方分布进行比较。如何绘制理论分布?

假设以下数据:

require(invgamma)
set.seed(10)
y<-rinvchisq(1000, 10)

这导致如下“经验”分布:

as.tibble(y) %>%
  ggplot(aes(y)) +
  geom_histogram(bins=100) 

我的直觉告诉我,我应该使用dinvchisq-函数,它可以在invgamma 包中找到。但不能正确安装。有谁知道如何解决这个问题?

编辑:

感谢@marvinschmit 和@BenBolker 添加解决方案。

require(invgamma)
set.seed(10)
y = rinvchisq(1000, 10)

x = seq(0,1, by=.001)
d = invgamma::dinvchisq(x, df=10)
df = data.frame(x=x,d=d)

as.tibble(y) %>%
  ggplot(aes(x = y)) +
  geom_histogram(bins=100, aes(y=..density..)) +
  geom_line(data = df, aes(x = x, y = d), color = "blue")

【问题讨论】:

    标签: r probability distribution chi-squared


    【解决方案1】:

    d... 密度函数需要一个分位数向量。我将分位数向量称为x

    x = seq(0,1, by=.001)
    d = dinvchisq(x, df=10)
    plot(x,d, type="l")
    

    输出:

    请注意,我使用了基本的R 绘图,因为漂亮的 ggplot 与问题无关。您可以简单地构建一个数据框df=data.frame(x=x,d=d) 并将其用于漂亮的 ggplot 绘图。

    编辑:使用lines() 将理论分布叠加到经验直方图上。

    【讨论】:

    • OP 可能在密度与计数方面存在一些困难......
    • 我认为@BenBolker 现在解决了我的问题。我该如何调整它,以便我可以在我的采样上绘制理论分布?
    • 你需要ggplot还是可以是base R?告诉我,我提供代码。
    • 谢谢!最好在ggplot中。但是对你来说最简单的。
    • 如果您在 ggplot 中对数据使用 geom_histogram(aes(y=..density..)),那么您将获得以密度而非计数比例绘制的结果:然后您可以覆盖 @marvinschmitt 建议的曲线
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2021-09-05
    • 1970-01-01
    • 2018-05-25
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多