我正在将我的一些R文件翻译成Python,我在使用Python中的Epanechnikov内核设置KDE图中的带宽时遇到了一些问题。
我试图使用函数seaborn.kdeplot(),以及statsmodels.nonparametric.kde.kdensity(),但从未得到正确的结果。
到目前为止,我没有找到一个模式,如何缩放或标准化Python中的带宽。此外,如果我使用高斯内核,R和Python的图是相同的。那么也许Epanechnikov内核的计算方式不同?
我使用以下代码在Python中创建KDE图:
import seaborn as sns
sns.kdeplot(y,kernel = 'epa', bw = 0.1)
和
from statsmodels.nonparametric.kde import kdensity
density = kdensity(y, kernel='epa', bw=0.1)
plt.plot(density[1],density[0], color = 'red')
两者的输出都不是很平滑:https://user-images.githubusercontent.com/48911472/55010186-1382fc80-4fe4-11e9-999f-263ea9bf62c9.png
但是,它应该看起来像R中的输出(或者在STATA中它们是相同的),它更平滑:
plot(density(y,bw=0.1, kernel ="epanechnikov"), col="red")
https://user-images.githubusercontent.com/48911472/55010088-e3d3f480-4fe3-11e9-8d84-ebd54c5b8bc9.png
尝试增加网格大小超过sns.kdplot,默认值为100,而我相信R的密度大约为512.希望它有帮助