模仿';p点';python中的R函数
模仿';p点';python中的R函数,python,r,Python,R,Rp点功能描述为: 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 <=
R
p点功能描述为:
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))
...
我会用numpy实现这一点:
import numpy as np
def ppoints(n, a):
""" numpy analogue or `R`'s `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)
样本输出:
>>> 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])
>点(5,1./2)
数组([0.1,0.3,0.5,0.7,0.9])
>>>P点(5、1/4)
阵列([0.136364,0.31818182,0.5,0.68181818,0.863636])
>>>n=10
>>>a=3./8。如果n>>点(n,a)
阵列([0.06097561,0.15853659,0.25609756,0.35365854,0.45121951,
0.54878049, 0.64634146, 0.74390244, 0.84146341, 0.93902439])
您可以使用它来测试实现。明显的问题:假设您复制/移植了实际的、非常简单的
R
代码,用于ppoints
,那么当您运行python版本时会发生什么呢?您是否得到相同的值?您是否阅读了?ppoints
上列出的参考资料?实际上,我是根据?ppoints
中的描述从头开始起草代码的,并使用相同的浮点数集检查R中ppoints
给出的结果。我没有想到要查看函数的R代码。。。也许我应该这么做。@Gabriel你复制了一个用例;在genral中,a是参数,而不是表达式3./8。如果n是@alko,我知道。我刚刚复制了我将要使用的默认大小写。@Gabriel一般来说,您的代码还可以,这里有一些小注释:似乎在R
(我不熟悉)中,len(n)=1表示标量,因此您必须在python中以不同的方式处理它。其次,您的代码(在py2中)不能很好地处理整型参数,您需要将它们转换为float。其他一切似乎都好。
>>> 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])