【发布时间】:2015-11-19 15:56:26
【问题描述】:
我正在尝试将 Stata 模型移植到 Python,并在 Stata 的 centile 和 Python 的 pandas.DataFrame.describe 之间找到一些差距:
- 统计:1%:-.1657010273898333,99%:.1683179750819993
- Python:1%:-0.1647677302502512、99:0.1607038771234249
我不知道他们是如何根据官方文档(http://www.stata.com/help.cgi?centile、http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.describe.html)计算的。但是当我在 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 文档 (https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.html) 指出以下内容:
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 库。
【问题讨论】:
-
Stata使用的方法可以在[[R] centile](stata.com/manuals13/rcentile.pdf)的方法和公式部分找到。
-
实现它的代码是
stats:::quantile.default
标签: python r pandas statistics stata