简体   繁体   English

Plot 理论逆卡方分布

[英]Plot theoretical inverise-chi-square distribution

I want to compare my 'empirical' data with the theoretical inverse-chi-square distribution .我想将我的“经验”数据与理论逆卡方分布进行比较。 How do I plot the theoretical distribution?我怎么plot的理论分布?

Assume the following data:假设以下数据:

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

which leads to an 'empitical' distribution as follows:这导致“经验”分布如下:

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

在此处输入图像描述

My gut tells me that I should use the dinvchisq -function that can be found in the invgamma package.我的直觉告诉我应该使用dinvchisq中的invgamma函数。 But cannot fit it properly.但不能正确安装。 Does anyone know how to tackle this matter?有谁知道如何解决这个问题?

EDIT:编辑:

Adding solution, thanks to @marvinschmit and @BenBolker.感谢@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")

You need a vector of quantiles for the d... density functions.您需要d...密度函数的分位数向量。 I will call the quantile vector x :我将调用分位数向量x

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

Output: Output:

输出:chisq密度

Note that I used basic R plotting because a pretty ggplot is not relevant to the question.请注意,我使用了基本的R绘图,因为漂亮的 ggplot 与问题无关。 You can simply construct a dataframe df=data.frame(x=x,d=d) and use it for pretty ggplot plotting.您可以简单地构建一个 dataframe df=data.frame(x=x,d=d)并将其用于漂亮的 ggplot 绘图。

EDIT: Use lines() to superpose the theoretical distribution over an empirical histogram.编辑:使用lines()将理论分布叠加在经验直方图上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM