0

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
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
user3083324
  • 585
  • 8
  • 23

2 Answers2

1

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

Community
  • 1
  • 1
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
0

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)))

jlhoward
  • 58,004
  • 7
  • 97
  • 140