Skip to content

probability plots comparing datasets with different sizes #2896

Closed

Description

@anntzer

Currently, it is not possible to make a probability plot to compare differently-sized datasets:

ProbPlot(np.random.rand(123)).ppplot(other=np.random.rand(23))
<elided>
/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
    221         y = _check_1d(y)
    222         if x.shape[0] != y.shape[0]:
--> 223             raise ValueError("x and y must have same first dimension")
    224         if x.ndim > 2 or y.ndim > 2:
    225             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension

That would be a nice addition. At least the PP-plot is unambiguously defined; QQ-plots would probably require the user to pass in the quantiles to use.

Activity

  1. added this to the 0.9 milestone on Jun 17, 2016
  2. josef-pkt commented on Jun 17, 2016

    @josef-pkt
    Member

    I'm putting this now as bug for 0.9, it's a serious limitation

  3. yl565 commented on Jul 22, 2016

    @yl565
    Contributor

    Hi, I would like to do it as my first PR. I'm thinking about plotting the data points with two colors, each represent samples from one dataset

  4. josef-pkt commented on Jul 22, 2016

    @josef-pkt
    Member

    @yl565 A PR would be great

    If you have questions just ask and I look into it, otherwise I wait for a PR.

    There might be different versions of the probability plots, e.g. if we have the two datasets on different axis, then there will not be different points for different datasets (and no need for two colors).
    (If two or multiple datasets are plotted against a joint distribution, then they would have distinct points. I don't think I have looked at that case yet.)

  5. yl565 commented on Jul 22, 2016

    @yl565
    Contributor

    I think I'm confused about the P-P plot mentioned here. What I'm thinking about is as follows:

    Assuming two datasets X, Y are drawn from the same distribution, so for each sorted sample xi (i=1...n), we can estimate its percentile, p1(xi) in Y. And the points to be plotted are ( (i-1/2)/n, p1(xi) ). Conversely, we can estimate the percentile of yk (k=1...m) in X and plot points (p2(yk), (k-1/2)/m).

  6. josef-pkt commented on Jul 22, 2016

    @josef-pkt
    Member

    @yl565 I haven't really looked at the details yet. I'm more familiar with goodness of fit testing than graphics. And I usually have problems recalling the difference between pp-plots and qq-plots.

    If it makes sense, and especially if there is a reference than we can include it. As I learned from @phobson work is that there are many variants of probability plots used in different fields.

    What I was thinking, without having this checked yet, is to use the pooled data points and plot p1(x) on the horizontal and p2(x) on the vertical axis for x in pooled array, where p1, p2 are the empirical cdf of the first and second array.

  7. yl565 commented on Aug 6, 2016

    @yl565
    Contributor

    @josef-pkt I wasn't able to find any papers/books mention about pp/qq plots with two different data sets, but I found the following slides http://cs.gmu.edu/~menasce/cs700/files/FittingDistributionsComparingDataSets.pdf

    In slide 5, it mentioned;

    A Q-Q plot is a plot of the type (Q1(p), Q2(p)) where Q1(p) is the quantile function of data set 1 and Q2(p) is the quantile function of data set 2. The values of p are (i-0.5)/n where n is the size of the smaller data set.
    For P-P plot I should be similar.

    Do you think this worth implementing?

  8. phobson commented on Aug 6, 2016

    @phobson
    Contributor

    There is already this basic functionality in statsmodels. I had a hand in its creation and I'll admit it's interface and implementation leave much to be desired. 
    More recently I've implemented at more robust and generic probability scale for matplotlib here:http://phobson.github.io/mpl-probscale/tutorial/getting_started.html#ready-made-probability-plots

    Ultimately I'd like to see the scale included in matplotlib (or at least under their GH organization) so that statsmodels and just worry about the plotting. 

    Sorry if this was unintelligible. I'm on my phone.

        _____________________________
    

    From: Yichuan Liu [email protected]
    Sent: Saturday, August 6, 2016 09:06
    Subject: Re: [statsmodels/statsmodels] probability plots comparing datasets with different sizes (#2896)
    To: statsmodels/statsmodels [email protected]
    Cc: Paul Hobson [email protected], Mention [email protected]

    @josef-pkt I wasn't able to find any papers/books mention about pp/qq plots with two different data sets, but I found the following slides http://cs.gmu.edu/~menasce/cs700/files/FittingDistributionsComparingDataSets.pdf

    In slide 5, it mentioned;

    A Q-Q plot is a plot of the type (Q1(p), Q2(p)) where Q1(p) is the quantile function of data set 1 and Q2(p) is the quantile function of data set 2. The values of p are (i-0.5)/n where n is the size of the smaller data set.
    For P-P plot I should be similar.
    Do you think this worth implementing?

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub, or mute the thread.

  9. josef-pkt commented on Aug 6, 2016

    @josef-pkt
    Member

    @phobson The question is how to extend the current 2-sample probability plot to unequal number of observations, and add the corresponding other plots.

    @yl565 Yes, I think something like that would be the appropriate plot.
    e.g. for p = 0.5 we would have a point where the median of x1 on the horizontal and the median of x2 on the vertical axis (assuming the sample sizes are odd).
    It's a bit more difficult to decide on where the points are if there is no observation with the exact p.
    (There might also be an option to use linear interpolation as in the median case when nobs is even.)

    To @phobson axis scaling. Once we have decided which points or lines to plot, then it would be relatively straight forward to rescale the axes for different interpretation.

    BTW: My experience is also that it's difficult to find references with a clear descriptions, including the math formulas, for various versions of the probability plots.

  10. yl565 commented on Aug 6, 2016

    @yl565
    Contributor

    @josef-pkt Here I found two more places mentioned Q-Q plot:
    http://www.itl.nist.gov/div898/handbook/eda/section3/eda33o.htm
    https://www.r-bloggers.com/exploratory-data-analysis-quantile-quantile-plots-for-new-yorks-ozone-pollution-data/

    If the 2 sample sizes are equal, then the Q-Q plot simply plots the sorted data of one data set against the sorted data of the other data set.
    If the 2 sample sizes are different, then the quantiles are usually selected to correspond to the sorted values from the smaller data set. The quantiles for the larger data set are then interpolated. Thus, it is possible that none of the data from the larger data set are used in the Q-Q plot.

  11. yl565 commented on Aug 8, 2016

    @yl565
    Contributor

    @josef-pkt Hi, can I ask how may I test the gofplots? I've been running

    numpy.testing.run_module_suite('statsmodels/graphics/tests/test_gofplots.py')

    It seems to be just creating and closing figures. Is it just testing if any errors will be raised? Is there another script or something to test if the plots are correct or not? I'm fairly new to unit testing. Sorry if this is a dump question.

  12. josef-pkt commented on Aug 8, 2016

    @josef-pkt
    Member

    It's definitely not a dumb question.
    So far we use almost only smoke tests for plots, i.e. check that they don't raise an exception. (and visually inspect them or add a notebook to the documentation so that a user/reader might find problems if a plot looks wrong.)
    One possibility is to find the x and y values in the figure and axis objects and compare with verified or regression test data.

    (The long term option would be to use matplotlib testing facility to compare entire plots with a saved copy. I haven't looked at that in years, and it was not easy to do with older matplotlib versions.)

    In most cases the plotting functions just do the plotting. If there are calculations for the plots, then we can unit test the results of those calculations directly.

  13. modified the milestones: 0.9, 0.10 on Apr 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions