确定R中分布的高密度区域
背景:确定R中分布的高密度区域,r,statistics,distribution,bayesian,confidence-interval,R,Statistics,Distribution,Bayesian,Confidence Interval,背景: curve(df(x, 10, 90), 0, 3, ylab = 'Density', xlab = 'F value', lwd = 3) Mode = ( (10 - 2) / 10 ) * ( 90 / (90 + 2) ) abline(v = Mode, lty = 2) CI = qf( c(.025, .975), 10, 90) arrows(CI[1], .05, CI[2], .05, code = 3, angle = 90, length = 1.4,
curve(df(x, 10, 90), 0, 3, ylab = 'Density', xlab = 'F value', lwd = 3)
Mode = ( (10 - 2) / 10 ) * ( 90 / (90 + 2) )
abline(v = Mode, lty = 2)
CI = qf( c(.025, .975), 10, 90)
arrows(CI[1], .05, CI[2], .05, code = 3, angle = 90, length = 1.4, col= 'red' )
points(Mode, .05, pch = 21, bg = 'green', cex = 3)
通常,R给出已知分布的分位数。在这些分位数中,较低的2.5%到较高的97.5%覆盖了这些分布下95%的面积
问题:
curve(df(x, 10, 90), 0, 3, ylab = 'Density', xlab = 'F value', lwd = 3)
Mode = ( (10 - 2) / 10 ) * ( 90 / (90 + 2) )
abline(v = Mode, lty = 2)
CI = qf( c(.025, .975), 10, 90)
arrows(CI[1], .05, CI[2], .05, code = 3, angle = 90, length = 1.4, col= 'red' )
points(Mode, .05, pch = 21, bg = 'green', cex = 3)
假设我有一个F分布(df1=10,df2=90)。在R中,我如何确定该分布下的95%面积,以便该95%仅覆盖高密度区域,而不是R通常给出的95%(请参见下面的我的R代码)
注意:显然,最高密度是“模式”(下图中的虚线)。所以我想,我们必须从“模式”向尾部移动
这是我的R代码:
curve(df(x, 10, 90), 0, 3, ylab = 'Density', xlab = 'F value', lwd = 3)
Mode = ( (10 - 2) / 10 ) * ( 90 / (90 + 2) )
abline(v = Mode, lty = 2)
CI = qf( c(.025, .975), 10, 90)
arrows(CI[1], .05, CI[2], .05, code = 3, angle = 90, length = 1.4, col= 'red' )
points(Mode, .05, pch = 21, bg = 'green', cex = 3)
的第25.2节给出了完整的R代码,用于确定以三种方式指定的分布的最高密度区间:作为累积密度函数、网格近似值或样本。对于累积密度函数,该函数称为HDIofICDF()
。它位于该书网站上的实用程序脚本中,DBDA2E utilities.R
(链接如上)。代码如下:
HDIofICDF = function( ICDFname , credMass=0.95 , tol=1e-8 , ... ) {
# Arguments:
# ICDFname is R’s name for the inverse cumulative density function
# of the distribution.
# credMass is the desired mass of the HDI region.
# tol is passed to R’s optimize function.
# Return value:
# Highest density interval (HDI) limits in a vector.
# Example of use: For determining HDI of a beta(30,12) distribution, type
# > HDIofICDF( qbeta , shape1 = 30 , shape2 = 12 )
# Notice that the parameters of the ICDFname must be explicitly named;
# e.g., HDIofICDF( qbeta , 30 , 12 ) does not work.
# Adapted and corrected from Greg Snow’s TeachingDemos package.
incredMass = 1.0 - credMass
intervalWidth = function( lowTailPr , ICDFname , credMass , ... ) {
ICDFname( credMass + lowTailPr , ... ) - ICDFname( lowTailPr , ... )
}
optInfo = optimize( intervalWidth , c( 0 , incredMass ) , ICDFname=ICDFname ,
credMass=credMass , tol=tol , ... )
HDIlowTailPr = optInfo$minimum
return( c( ICDFname( HDIlowTailPr , ... ) ,
ICDFname( credMass + HDIlowTailPr , ... ) ) )
}
你试过这个包裹吗 下面是您的示例:
library(hdrcde)
hdr.den(rf(1000,10,90),prob=95)
您可以使用各种高密度区域,它适用于多模态pdf
hdr.den(c(rf(1000,10,90),rnorm(1000,4,1)),prob=c(50,75,95))
及
您甚至可以将其与多变量分布一起用于可视化2D高密度区域:
hdrs=c(50,75,95)
x=c(rf(1000,10,90),rnorm(1000,4,1))
y=c(rf(1000,5,50),rnorm(1000,7,1) )
par(mfrow=c(1,3))
hdr.den(x,prob=hdrs,xlab="x")
hdr.den(y,prob=hdrs,xlab="y")
hdr.boxplot.2d(x,y,prob=hdrs,shadecol="red",xlab="x",ylab="y")
使用stat.extend
软件包中的HDR.f
功能
为R中的所有基本发行版及其扩展包中的一些发行版提供HDR函数。它对分布使用基于分位数函数的方法,并自动调整分布的形状(单峰、双峰等)。下面是如何使用函数计算您感兴趣的HDR
#Load library
library(stat.extend)
#Compute HDR for an F distribution
HDR.f(cover.prob = 0.9, df1 = 10, df2 = 20)
Highest Density Region (HDR)
90.00% HDR for F distribution with 10 numerator degrees-of-freedom and
20 denominator degrees-of-freedom
Computed using nlm optimisation with 9 iterations (code = 3)
[0.220947190373167, 1.99228812929142]
是的,它适用于任何icdf函数,但它确实假定为单峰pdf。