user3083324
user3083324

Reputation: 585

qqplot against theoretical quantiles

I want to be able to do qqplots against theoretical quantiles. I already know qqnorm() does that for the normal distribution, but that's a pretty a limited tool to me. Also, I noticed that some people simulate random processes from a given distribution and use it to compare to sample data, which is not entirely rigorous because it depends on the method of simulation. So my example would be to compare sample data to geometric distribution:

#sample data
var=rgeom(prob=0.3, n=100)

#QQplot against a theoretical geometric distribution with prob=0.5

Upvotes: 0

Views: 1795

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81733

You can produce such a plot easily with the ggplot2 package:

library(ggplot2)
qplot(sample = var, geom = "point", stat = "qq", 
      distribution = qgeom, dparams = list(prob = 0.5))

The theoretical quantiles (x axis) are derived from a geometric distribution with a probability of 0.5. enter image description here

Update (based on question in comment):

A line can be added in the following way though it is not very well suited for geometric distribution. The approach is based on this answer.

qv <- quantile(var, c(.25, .75))
qt <- qgeom(prob = 0.5, c(.25, .75))
slope <- diff(qv)/diff(qt)
int <- qv[1] - slope * qt[1]

qplot(sample = var, geom = "point", stat = "qq", 
      distribution = qgeom, dparams = list(prob = 0.5)) + 
  geom_abline(slope = slope, intercept = int)

enter image description here

Upvotes: 1

jlhoward
jlhoward

Reputation: 59415

A Q-Q plot is a plot of quantiles of a ramdom sample against quantiles of the test distribution. So,

plot(qgeom(seq(0,1,length=100),.3),quantile(var,seq(0,1,length=100)))

Upvotes: 0

Related Questions