从R中数据的完全高斯拟合获得分位数

从R中数据的完全高斯拟合获得分位数,r,normal-distribution,quantile,R,Normal Distribution,Quantile,我一直在努力研究R如何计算分位数和数据的正常拟合。 我的数据(NDVI值)遵循截断正态分布(见图) 我感兴趣的是从数据和拟合正态分布曲线中获得最低的第10百分位值(p=0.1) 在我的理解中,因为数据被截断了,所以两者应该是完全不同的:我期望数据的分位数高于正态分布计算的分位数,但事实并非如此。就我对分位数函数的理解而言,数据中的分位数应该是默认分位数函数: q=quantile(y, p=0.1) 而正态分布的分位数为: qx=quantile(y, p=0.1, type=9) 然而,这

我一直在努力研究R如何计算分位数和数据的正常拟合。 我的数据(NDVI值)遵循截断正态分布(见图)

我感兴趣的是从数据和拟合正态分布曲线中获得最低的第10百分位值(p=0.1)

在我的理解中,因为数据被截断了,所以两者应该是完全不同的:我期望数据的分位数高于正态分布计算的分位数,但事实并非如此。就我对分位数函数的理解而言,数据中的分位数应该是默认分位数函数:

q=quantile(y, p=0.1)
而正态分布的分位数为:

qx=quantile(y, p=0.1, type=9)
然而,这两个结果在所有情况下都非常接近,这让我想知道R适合数据的哪种类型的分布来计算分位数(截断正态分布?)

我还尝试根据拟合正态曲线计算分位数,如下所示:

fitted=fitdist(as.numeric(y), "norm", discrete = T)
fit.q=as.numeric(quantile(fitted, p=0.1)[[1]][1])
但是没有什么不同

因此,我的问题是: R与用于计算分位数的数据拟合的曲线是什么,特别是对于类型=9?如何根据完全正态分布(包括下尾)计算分位数

我不知道如何生成一个可复制的例子,但数据可在


谢谢

R是在确定分位数时使用数据的经验排序,而不是假设任何特定的分布

截断数据的第10个百分位与数据的正态分布恰好非常接近,尽管第1个百分位有点不同。例如:

# Load data
df = read.csv("data.csv", header=TRUE, stringsAsFactors=FALSE)

# Fit a normal distribution to the data
df.dist = fitdist(df$x, "norm", discrete = T)
现在让我们得到拟合分布的分位数和原始数据。除了第10个百分位之外,我还包括了第1个百分位。您可以看到,拟合正态分布的第10个百分位数略低于数据的百分位数。然而,拟合正态分布的第一个百分位要低得多

您还可以通过直接对数据进行排序,并获得正态分布的第1和第10个百分位,其平均值和sd等于来自
fitdist
的拟合值,从而看到这一点:

# 1st and 10th percentiles of data by direct ranking
df$x[order(df$x)][round(c(0.01,0.1)*5780)]
[1] 2064 2469

# 1st and 10th percentiles of fitted distribution 
qnorm(c(0.01,0.1), df.dist$estimate[1], df.dist$estimate[2])
[1] 1632.829 2459.039
让我们绘制原始数据(蓝色)和拟合正态分布生成的伪数据(红色)的直方图。重叠区域为紫色

# Histogram of data (blue)
hist(df$x, xlim=c(0,8000), ylim=c(0,1600), col="#0000FF80")

# Overlay histogram of random draws from fitted normal distribution (red)
set.seed(685)
set.seed(685)
x.fit = rnorm(length(df$x), df.dist$estimate[1], df.dist$estimate[2])
hist(x.fit, add=TRUE, col="#FF000080")

或者我们可以绘制数据(蓝色)的经验累积分布函数(ecdf)和拟合正态分布(红色)的随机抽取。水平灰线表示第10个百分位:

plot(ecdf(df$x), xlim=c(0,8000), col="blue")
lines(ecdf(x.fit), col="red")
abline(0.1,0, col="grey40", lwd=2, lty="11")

现在我已经讲完了,我想知道你是否期望
fitdist
返回正态分布的参数,如果你的数据真的来自正态分布,而不是被截断的话。相反,
fitdist
返回一个正态分布,其中包含手头(截断的)数据的平均值和sd,因此与我们可能“预期”的位置相比,
fitdist
返回的分布向右移动

c(mean=mean(df$x), sd=sd(df$x))
或者,另一个简单的例子:
x
正态分布,平均值为0,标准差为1
xtrunc
删除所有小于-1的值,
xtrunc.dist
xtrunc
上的
fitdist
的输出:

set.seed(55)
x = rnorm(6000)
xtrunc = x[x > -1]
xtrunc.dist = fitdist(xtrunc, "norm")

round(cbind(sapply(list(x=x,xtrunc=xtrunc), function(x) c(mean=mean(x),sd=sd(x))),
      xtrunc.dist=xtrunc.dist$estimate),3)

          x xtrunc xtrunc.dist
mean -0.007  0.275       0.275
sd    1.009  0.806       0.806
您可以在下面的ecdf图中看到,截断数据和拟合到截断数据的正态分布具有大约相同的第10个百分位,而非受控数据的第10个百分位(正如我们所预期的)向左移动


美丽而完整的解释!作为后续,我如何构建适合我的数据的完整正态分布(相同的m和sd),但没有您提到的正确的偏移?
        1%      10% 
  2064.177 2469.400
# 1st and 10th percentiles of data by direct ranking
df$x[order(df$x)][round(c(0.01,0.1)*5780)]
[1] 2064 2469

# 1st and 10th percentiles of fitted distribution 
qnorm(c(0.01,0.1), df.dist$estimate[1], df.dist$estimate[2])
[1] 1632.829 2459.039
# Histogram of data (blue)
hist(df$x, xlim=c(0,8000), ylim=c(0,1600), col="#0000FF80")

# Overlay histogram of random draws from fitted normal distribution (red)
set.seed(685)
set.seed(685)
x.fit = rnorm(length(df$x), df.dist$estimate[1], df.dist$estimate[2])
hist(x.fit, add=TRUE, col="#FF000080")
plot(ecdf(df$x), xlim=c(0,8000), col="blue")
lines(ecdf(x.fit), col="red")
abline(0.1,0, col="grey40", lwd=2, lty="11")
c(mean=mean(df$x), sd=sd(df$x))
     mean        sd 
3472.4708  790.8538
df.dist$estimate
     mean        sd 
3472.4708  790.7853
set.seed(55)
x = rnorm(6000)
xtrunc = x[x > -1]
xtrunc.dist = fitdist(xtrunc, "norm")

round(cbind(sapply(list(x=x,xtrunc=xtrunc), function(x) c(mean=mean(x),sd=sd(x))),
      xtrunc.dist=xtrunc.dist$estimate),3)

          x xtrunc xtrunc.dist
mean -0.007  0.275       0.275
sd    1.009  0.806       0.806