确定R中分布的高密度区域

确定R中分布的高密度区域

本文介绍了确定R中分布的高密度区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

通常,R给出众所周知的分布的分位数.在这些分位数中,较低的2.5%直到较高的97.5%覆盖了这些分布下95%的面积.

Normally, R gives quantiles for well-known distributions. Out of these quantiles, the lower 2.5% up to the upper 97.5% covers 95% of the area under these distributions.

问题:

假设我有一个F分布(df1 = 10,df2 = 90).在R中,如何确定此分布下95%的区域,以使这95%仅覆盖HIGH DENSITY区域,而不是R通常给出的95%(请参见下面的我的R代码) ?

Suppose I have a F distribution (df1 = 10, df2 = 90). In R, how can I determine the 95% of the area under this distribution such that this 95% only covers the HIGH DENSITY area, not the 95% that R normally gives (see my R code Below)?

注意:显然,最高密度是模式"(下图中的虚线).所以我想,必须从模式"转向尾巴.

Note: Clearly, the highest density is the "mode" (dashed line in the pic below). So I guess, one must move from "mode" toward the tails.

这是我的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)

推荐答案

DBDA2E的第25.2节 a>提供了完整的R代码,用于确定三种指定分布的最高密度区间:作为累积密度函数,作为网格近似或作为样本.对于累积密度函数,该函数称为HDIofICDF().它位于本书网站上的实用程序脚本DBDA2E-utilities.R中(上面链接).这是代码:

Section 25.2 of DBDA2E gives complete R code for determining highest density intervals for distributions specified three ways: as cumulative density functions, as grid approximations, or as samples. For a cumulative density function, the function is called HDIofICDF(). It's in the utilities script, DBDA2E-utilities.R at the book's web site (linked above). Here's the code:

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 , ... ) ) )
}

这篇关于确定R中分布的高密度区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!