Mimicking the "ppoints" R function in python

The function of R ppoints is described as:

 Ordinates for Probability Plotting Description: Generates the sequence of probability points '(1:m - a)/(m + (1-a)-a)' where 'm' is either 'n', if 'length(n)==1', or 'length(n)'. Usage: ppoints(n, a = ifelse(n <= 10, 3/8, 1/2)) ... 

I am trying to reproduce this function in python and I have a few doubts.

1- The first m in (1:m - a)/(m + (1-a)-a) always an integer: int(n) (i.e. an integer n ) if length(n)==1 and length(n) otherwise.

2- The second m in the same equation is NOT an integer if length(n)==1 (it takes a real value n ) and IS is an integer ( length(n) ) otherwise.

3- n in a = ifelse(n <= 10, 3/8, 1/2) is the real number n if length(n)==1 and the integer length(n) otherwise.

This is not entirely clear in this description, and I am very grateful if someone can confirm that this is so.


Add

Well, this was originally published at https://stats.stackexchange.com/ , because I was hoping to get input from extras that work with the ppoints function. Since it has been ported here, I will insert below the function I wrote for replicating ppoints in python . I tested it and both seemed to return the same results, but it would be great if someone could clarify the above points, because they are not fully understood by the function description.

 def ppoints(vector): ''' Mimics R function 'ppoints'. ''' m_range = int(vector[0]) if len(vector)==1 else len(vector) n = vector[0] if len(vector)==1 else len(vector) a = 3./8. if n <= 10 else 1./2 m_value = n if len(vector)==1 else m_range pp_list = [((m+1)-a)/(m_value+(1-a)-a) for m in range(m_range)] return pp_list 
+6
source share
1 answer

I would do this with numpy:

 import numpy as np def ppoints(n, a): """ numpy analogue or `R` `ppoints` function see details at http://stat.ethz.ch/R-manual/R-patched/library/stats/html/ppoints.html :param n: array type or number""" try: n = np.float(len(n)) except TypeError: n = np.float(n) return (np.arange(n) + 1 - a)/(n + 1 - 2*a) 

Output Example:

 >>> ppoints(5, 1./2) array([ 0.1, 0.3, 0.5, 0.7, 0.9]) >>> ppoints(5, 1./4) array([ 0.13636364, 0.31818182, 0.5 , 0.68181818, 0.86363636]) >>> n = 10 >>> a = 3./8. if n <= 10 else 1./2 >>> ppoints(n, a) array([ 0.06097561, 0.15853659, 0.25609756, 0.35365854, 0.45121951, 0.54878049, 0.64634146, 0.74390244, 0.84146341, 0.93902439]) 

To test the implementation, you can use the R script .

+4
source

Source: https://habr.com/ru/post/959063/


All Articles