我实际上是R和统计数据的新手..这样的事情可以在R中完成
确定两个样本(2个矢量)的密度估计值.我使用R做了这个,并使用核密度估计获得了2个样本的2个密度曲线.
无论如何还要定量比较两个样本的密度估计值的相似/不相似程度.
我试图找出哪些数据样本展示具有与特定分布类似的分布.
我正在使用R语言...有人可以帮忙.. ??
这是我的R代码:
x <- c(5.89,6.81,6.69,7.61,5.44,6.17)
y <- c(1,0.09392423,0.07090779,0.0687634,0.06819157,0.06261615)
plot(density(x, bw=0.050, adjust=1, kernel="gaussian",weights=(y),n=1000))
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用D3.js或任何其他等效的JavaScript库来实现相同的结果.
有人可以帮忙吗?
我正在使用scipys gaussian_kde获取某些双峰数据的概率密度。但是,由于我的数据是有角度的(以度为单位的方向),所以当值接近极限时会出现问题。下面的代码给出了两个示例kde,当域为0-360时,由于无法处理数据的循环性质,因此处于估计状态。pdf需要在单位圆上定义,但我在scipy.stats中找不到适合此类数据的任何内容(存在冯·米斯分布,但仅适用于单峰数据)。外面有没有人遇到过这个?是否有任何可用于估算单位圆上的双峰pdf的信息(基于python的首选)?
import numpy as np
import scipy as sp
from pylab import plot,figure,subplot,show,hist
from scipy import stats
baz = np.array([-92.29061004, -85.42607874, -85.42607874, -70.01689348,
-63.43494882, -63.43494882, -70.01689348, -70.01689348,
-59.93141718, -63.43494882, -59.93141718, -63.43494882,
-63.43494882, -63.43494882, -57.52880771, -53.61564818,
-57.52880771, -63.43494882, -63.43494882, -92.29061004,
-16.92751306, -99.09027692, -99.09027692, -16.92751306,
-99.09027692, -16.92751306, -9.86580694, -8.74616226,
-9.86580694, -8.74616226, -8.74616226, -2.20259816,
-2.20259816, -2.20259816, -9.86580694, -2.20259816,
-2.48955292, -2.48955292, -2.48955292, -2.48955292,
4.96974073, 4.96974073, 4.96974073, 4.96974073,
-2.48955292, -2.48955292, -2.48955292, -2.48955292,
-2.48955292, -9.86580694, -9.86580694, -9.86580694,
-16.92751306, -19.29004622, -19.29004622, -26.56505118,
-19.29004622, -19.29004622, -19.29004622, -19.29004622]) …Run Code Online (Sandbox Code Playgroud) 我想将密度曲线叠加到我构造的频率直方图上.对于我使用的频率直方图,aes(y=..counts../40)因为40是我的总样本数.aes(y=..density..*0.1)因为我的binwidth是0.1,我曾经强迫密度介于0和1之间.但是,密度曲线不适合我的数据,它排除了等于1.0的值(注意直方图显示bin =(1.0,1.1)的累积值,但密度曲线结束于1.0)
这是我的数据
data<-structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的内核函数:
x <- 1:100
y <- rnorm(100, mean=(x/2000)^2)
plot(x,y)
kernel <- ksmooth(x,y, kernel="normal", bandwidth=10)
print(kernel$y)
Run Code Online (Sandbox Code Playgroud)
如果我试图在x值范围之外的某个点进行预测,它会给我NaN,因为它试图推断超出数据:
x <- 1:100
y <- rnorm(100, mean=(x/2000)^2)
plot(x,y)
kernel <- ksmooth(x,y, kernel="normal", bandwidth=10, x.points=c(130))
print(kernel$y)
> print(kernel$y)
[1] NA
Run Code Online (Sandbox Code Playgroud)
即使我改变range.x它也不会让步:
x <- 1:100
y <- rnorm(100, mean=(x/2000)^2)
plot(x,y)
kernel <- ksmooth(x,y, kernel="normal", bandwidth=10, range.x=c(1,200) , x.points=c(130))
print(kernel$y)
> print(kernel$y)
[1] NA
Run Code Online (Sandbox Code Playgroud)
如何获得ksmooth超出数据的推断函数?我知道这在理论上是一个坏主意,但实际上这个问题一直存在.
我有一个像这样的数据框“foo”
Date Return
1998-01-01 0.02
1998-01-02 0.04
1998-01-03 -0.02
1998-01-04 -0.01
1998-01-05 0.02
...
1998-02-01 0.1
1998-02-02 -0.2
1998-02-03 -0.1
etc.
Run Code Online (Sandbox Code Playgroud)
我想在这个数据框中添加一个新列,显示相应回报的密度值。我试过:
foo$density <- for(i in 1:length(foo$Return)) density(foo$Return,
from = foo$Return[i], to = foo$Return[i], n = 1)$y
Run Code Online (Sandbox Code Playgroud)
但它没有用。我真的很难将“函数”应用于每一行。但也许还有另一种方法可以做到,而不是使用密度()?
我本质上想做的是将密度()中的拟合密度值提取到 foo 中的返回值。如果我只是做 plot(density(foo$Return)) 它会给我曲线,但是我希望将密度值附加到回报上。
@乔里斯:
foo$density <- density(foo$Return, n=nrow(foo$Return))$y
Run Code Online (Sandbox Code Playgroud)
计算一些东西,但似乎返回错误的密度值。
谢谢你帮我解决!达尼
有没有办法让R中的density()函数使用计数与概率?
例如,在使用直方图函数检查密度分布时,我有两个选择hist:
hist(x,freq=F) #"graphic is a representation of frequencies, the counts component of the result"
hist(x,freq=T) #"probability densities, component density, are plotted (so that the histogram has a total area of one)"
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以使用该density功能执行类似的操作?
在我的具体示例中,我有许多直径不同的树木。(我会注意到,我将数据保持为连续的大小比例,而不是将其分为离散的大小类)。当我将density函数与该数据一起使用时(即plot(density(dat$D,na.rm=T,from=0))),它为我提供了每种尺寸的概率(当然是平滑的)的密度估计。我对将这些数据报告为茎/面积与概率的关系更感兴趣,因此我更喜欢密度估计值来使用计数。
想法?
更新:
这是一些真实的示例数据:
dat <- c(6.6, 7.1, 8.4, 27.4, 11.9, 18.8, 8.9, 25.4, 8.9, 8.6, 11.4, 19.3, 7.6, 42.2, 20.8, 25.1, 38.1, 42.2, 5.2, 34.3, 42.7, 34, 37.3, 45.5, 39.4, 25.1, 30.7, 23.1, 43.4, 19.6, 30.5, 23.9, 10.7, …Run Code Online (Sandbox Code Playgroud) r histogram frequency-distribution kernel-density density-plot
我希望能够从使用 Python 的 Seaborn 生成的内核密度图中提取特征参数。虽然有一个关于获得分布中位数的很好的例子,但我想看看这是否可以推广到一维数据的多峰分布,特别是在二维情况下。
下面是一个最小的例子,我从中手动推导出一维情况下每个峰值的值。我希望使用可用的对象找到更系统和适用于 2D 的东西。
import numpy as np
import scipy
import pandas as pd
import seaborn as sns
sns.set(style="white", color_codes=True, font_scale=2)
x1 = np.random.normal(-1.5,1,1000)
y1 = np.random.normal(1.5,1,1000)
x2 = np.random.normal(1.5,1,1000)
y2 = np.random.normal(-1.5,1,1000)
x = np.concatenate((x1,x2))
y = np.concatenate((y1,y2))
d = {'x': pd.Series(x), 'y': pd.Series(y)}
data = pd.DataFrame(d)
px = sns.kdeplot(data.x, shade=True)
x,y = px.get_lines()[0].get_data()
xysel = np.array([(x,y) for x,y in zip(x,y) if x < 0])
imax = np.argmax(xysel[:,1])
x_median = xysel[imax,0]
y_median = …Run Code Online (Sandbox Code Playgroud) 我有一个包含 ~900 行的数据框;我正在尝试为某些列绘制 KDEplots。在某些列中,大多数值是相同的最小值。当我包含太多最小值时,KDEPlot 会突然停止显示最小值。例如,以下包括 600 个值,其中 450 个是最小值,并且绘图看起来不错:
y = df.sort_values(by='col1', ascending=False)['col1'].values[:600]
sb.kdeplot(y)
Run Code Online (Sandbox Code Playgroud)
但是包括 451 个最小值给出了非常不同的输出:
y = df.sort_values(by='col1', ascending=False)['col1'].values[:601]
sb.kdeplot(y)
Run Code Online (Sandbox Code Playgroud)
最终,我想绘制不同列的双变量 KDEPlots,但我想先了解这一点。
kernel-density ×10
r ×6
python ×2
seaborn ×2
statistics ×2
c++ ×1
d3.js ×1
density-plot ×1
frequency ×1
gaussian ×1
ggplot2 ×1
histogram ×1
javascript ×1
pandas ×1
scipy ×1
smooth ×1