Python 参数类型为6的R分位数的等效值

Python 参数类型为6的R分位数的等效值,python,r,pandas,statistics,stata,Python,R,Pandas,Statistics,Stata,我正在尝试将一个Stata模型移植到Python中,并在Stata的centile和Python的pandas.DataFrame.description之间找到一些差距: 统计:1%:-1657010273898333,99%:.1683179750819993 Python:1%:-0.16476773025025299:0.1607038771234249 我不知道他们是如何根据官方文件(,)计算的。但当我在R中尝试相同的数据集时: > quantile(d[, c('V1')]

我正在尝试将一个Stata模型移植到Python中,并在Stata的
centile
和Python的
pandas.DataFrame.description
之间找到一些差距:

  • 统计:1%:-1657010273898333,99%:.1683179750819993
  • Python:1%:-0.16476773025025299:0.1607038771234249
我不知道他们是如何根据官方文件(,)计算的。但当我在R中尝试相同的数据集时:

> quantile(d[, c('V1')], c(0.01, 0.99), type=5)
    1%        99% 
-0.1650828  0.1652275 
> quantile(d[, c('V1')], c(0.01, 0.99), type=6)
   1%       99% 
-0.165701  0.168318 
使用参数
type=6
,结果似乎与Stata相同。分位数()的API文档指示以下内容:

Type 6
     m = p. p[k] = k / (n + 1). Thus p[k] = E[F(x[k])]. This is used by Minitab and by SPSS.

我找不到任何具有相同实现的现有Python库

感谢罗伯托·费勒!我已经基于编写了一个Python函数,它产生的结果与Stata相同:

def centile(arr, percentiles=[50]):
  result = {}

  s = np.sort(arr)
  n = len(s)

  for percent in percentiles: 
    R = float(n + 1) * percent / 100
    r, f = int(R), R - int(R)

    result['{0}%'.format(percent)] = float(s[r - 1]) + f * (s[r] - s[r - 1])

  return result

感谢罗伯托·费勒!我已经基于编写了一个Python函数,它产生的结果与Stata相同:

def centile(arr, percentiles=[50]):
  result = {}

  s = np.sort(arr)
  n = len(s)

  for percent in percentiles: 
    R = float(n + 1) * percent / 100
    r, f = int(R), R - int(R)

    result['{0}%'.format(percent)] = float(s[r - 1]) + f * (s[r] - s[r - 1])

  return result

感谢罗伯托·费勒!我已经基于编写了一个Python函数,它产生的结果与Stata相同:

def centile(arr, percentiles=[50]):
  result = {}

  s = np.sort(arr)
  n = len(s)

  for percent in percentiles: 
    R = float(n + 1) * percent / 100
    r, f = int(R), R - int(R)

    result['{0}%'.format(percent)] = float(s[r - 1]) + f * (s[r] - s[r - 1])

  return result

感谢罗伯托·费勒!我已经基于编写了一个Python函数,它产生的结果与Stata相同:

def centile(arr, percentiles=[50]):
  result = {}

  s = np.sort(arr)
  n = len(s)

  for percent in percentiles: 
    R = float(n + 1) * percent / 100
    r, f = int(R), R - int(R)

    result['{0}%'.format(percent)] = float(s[r - 1]) + f * (s[r] - s[r - 1])

  return result

如果希望得到与R的分位数相同的结果,请使用
numpy.percentile

import numpy as np

np.percentile(range(1, 101), 100*(3/8))
# 38.125, same as R quantile(1:100, 3/8)

如果希望得到与R的分位数相同的结果,请使用
numpy.percentile

import numpy as np

np.percentile(range(1, 101), 100*(3/8))
# 38.125, same as R quantile(1:100, 3/8)

如果希望得到与R的分位数相同的结果,请使用
numpy.percentile

import numpy as np

np.percentile(range(1, 101), 100*(3/8))
# 38.125, same as R quantile(1:100, 3/8)

如果希望得到与R的分位数相同的结果,请使用
numpy.percentile

import numpy as np

np.percentile(range(1, 101), 100*(3/8))
# 38.125, same as R quantile(1:100, 3/8)

Stata使用的方法可以在[[R]百分位数]()的方法和公式部分找到。实现它的代码是
stats:::分位数。默认值
Stata使用的方法可以在[[R]百分位数]()的方法和公式部分找到。实现它的代码是
stats:::分位数。默认值
Stata使用的方法可以在[[R]中找到centile](),Methods and formulas部分。实现它的代码是
stats:::quantile.default
Stata使用的方法可以在[[R]centile]()中找到,方法和公式部分。实现它的代码是<代码>统计:::分位数。默认值<代码>这将给出与R分位数不同的结果:)这将给出与R分位数不同的结果:)这将给出与R分位数不同的结果:)这将给出与R分位数不同的结果:)