在python中模仿'ppoints'R函数

2024-09-30 20:38:17 发布

您现在位置:Python中文网/ 问答频道 /正文

Rppoints函数描述如下:

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

我一直试图在python中复制这个函数,但我有几个疑问。在

1-如果(1:m - a)/(m + (1-a)-a)中的第一个m总是一个整数:int(n)(即,n整数),否则length(n)==1和{}。在

2-同一等式中的第二个m如果length(n)==1(它假设n的实值),则它是一个整数(length(n)),否则它是一个整数(length(n))。在

3-在a = ifelse(n <= 10, 3/8, 1/2)中的n实数n如果length(n)==1,则是整数length(n)。在

这一点在描述中根本没有说清楚,如果有人能证实这一点,我将非常感激。在


添加

这篇文章最初是在https://stats.stackexchange.com/上发布的,因为我希望得到处理ppoints函数的静态人员的输入。因为它已经迁移到这里,所以我将粘贴到我写的函数下面,在python中复制ppoints。我已经对它进行了测试,结果似乎都是一样的,但是如果有人能澄清上面的观点,我会很高兴的,因为它们根本没有被函数的描述弄清楚。在

^{pr2}$

Tags: ofthe函数for整数descriptionplottinglength
1条回答
网友
1楼 · 发布于 2024-09-30 20:38:17

我会用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)

样本输出:

^{pr2}$

可以使用R fiddle来测试实现。在

相关问题 更多 >