Search code examples
rprobabilitydistributionchi-squared

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?

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) 

enter image description here

My gut tells me that I should use the dinvchisq-function that can be found in the invgamma package. But cannot fit it properly. Does anyone know how to tackle this matter?

EDIT:

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

Solution

  • You need a vector of quantiles for the d... density functions. I will call the quantile vector x:

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

    Output:

    output: chisq density

    Note that I used basic R plotting because a pretty ggplot is not relevant to the question. You can simply construct a dataframe df=data.frame(x=x,d=d) and use it for pretty ggplot plotting.

    EDIT: Use lines() to superpose the theoretical distribution over an empirical histogram.