应用函数来查找分布的高密度区域(编码) [英] Applying a function to find high density area of a distribution (coding)

查看:247
本文介绍了应用函数来查找分布的高密度区域(编码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

高密度区域分布在-r / 42987104#42987104> 一个名为HDIofICDF的R函数也见下文)它为任何分布(单峰曲线)提供了两个极限值,从一个极限值到另一个极限值覆盖了该分布的高密度区域的95%。

该代码要求用户放置反向cdf 分布。对于R认可的分销,这可以通过 q分销名称来实现,例如, qf qchisq 等,以及,然后 qdistribution name 。例如,对于F分布,可以通过以下方式找到两个极限值:
$ b $ HDIofICDF(qf,df1 = 10,df2 = 90) code> OR对于卡方分布,可以使用: HDIofICDF(qchisq,df = 10)



编码问题:

假设我已经创建了自己的发行版并拥有逆cdf 这个分配。我使用逆cdf (类似于R中的 qdistribution name )(仅作为示例来显示我的<反向cdf 使用):
$ b $ invcdf.posterior(p = .025,t = 2.81,N1 = 10,N2 = 10,rscale = 1)



现在:给出我的 invcdf.posterior 及其上面的参数,我如何使用 HDIofICDF 函数获得我的分布的两个限制值?



这是我的R代码,我有:

  HDIofICDF = function(ICDFname,credMass = 0.95, tol = 1e-8,...){

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,...)
HD IlowTailPr = optInfo $ minimum
return(c(ICDFname(HDIlowTailPr,...),
ICDFname(credMass + HDIlowTailPr,...)))
}
### ################################################## #####################
##使用示例1:##

HDIofICDF(qf,df1 = 10, df2 = 90)##这是一个F分配工作OK



这是我如何尝试应用HDIofICDF函数对我自己的分发:
$ b $ pre $ HD $ f $ c HDIofICDF(invcdf.posterior,t = 2.81,N1 = 10,N2 = 10,rscale = 1)##不工作

我得到的错误是:

eval(expr,envir,enclos)中的错误:
参数t丢失,没有默认
调用从:eval(expr,envir,enclos)

解决方案

> HDIofICDF 函数有一个参数 tol



由于部分参数匹配,您调用 HDIofICDF(invcdf.posterior,t = 2.81,N1 = 10,N2 = 10,rscale = 1) t 是指 tol 。 (你可以通过修改 HDIofICDF 函数来打印 tol )来验证。)因此,当 invcdf.posterior 随后被调用,R抱怨缺少 t 参数。



下面是一个玩具示例,说明发生了什么:

  fun1 < - 函数(a,b)a + b 
fun2 < - 函数(aaa,...)fun1(...)
fun2(a = 1,b = 2)
#fun1(...) :参数a缺失,没有默认
fun2(aaa = 99,a = 1,b = 2)
#[1] 3

如上例所示,为了解决您的问题,请在您的调用中明确指定 tol HDIofICDF ,例如

  HDIofICDF(invcdf.posterior,t = 2.81, N1 = 10,N2 = 10,rscale = 1,tol = 1e-8)


Background:

Recently, I came across an R function called HDIofICDF (also see below) that provides two limit-values for any distribution (a unimodal curve) such that from one limit-value to the other limit-value covers 95% highly dense area of that distribution.

The code requires that user put Inverse cdf of a distribution. For R-recognized distributions, this is achieved by "qdistribution name" e.g., qf, qchisq etc., and "," and then the arguments needed for the "qdistribution name". For example for an F distribution finding the two limit values is possible via:

HDIofICDF( qf , df1 = 10 , df2 = 90 ) OR for a Chi-Square Distribution, one can use: HDIofICDF( qchisq, df = 10)

Coding Question:

Suppose I have created my own distribution and have the Inverse cdf of this distribution. My Inverse cdf (similar to a "qdistribution name" in R) is given when I use (just as an example to show what arguments my Inverse cdf uses):

invcdf.posterior(p = .025, t = 2.81, N1 = 10, N2 = 10, rscale = 1 )

Now: given my invcdf.posterior and its arguments above, how can I use the HDIofICDF function to obtain the two limit-values for my distribution?

Here is my R codes I have:

HDIofICDF = function( ICDFname , credMass=0.95 , tol=1e-8 , ... ) {

 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 , ... ) ) )
}
##########################################################################
## Example 1 of use: ##

HDIofICDF( qf , df1 = 10 , df2 = 90 ) ## This is a F distribution working OK

THIS IS HOW I TRIED TO APPLY THE HDIofICDF function TO MY OWN DISTRIBUTION:

HDIofICDF( invcdf.posterior, t = 2.81, N1 = 10 , N2 = 10, rscale = 1 ) ## Not working

The error I get is:

Error in eval(expr, envir, enclos) : argument "t" is missing, with no default Called from: eval(expr, envir, enclos)

解决方案

Notice that your HDIofICDF function has an argument tol.

Because of partial argument matching, when you call HDIofICDF(invcdf.posterior, t = 2.81, N1 = 10 , N2 = 10, rscale = 1), the t that you specified is assumed to mean tol. (You can verify this by modifying your HDIofICDF function to print tol.) Because of this, when your invcdf.posterior is subsequently called, R complains that the t argument is missing.

Here's a toy example illustrating what is going on:

fun1 <- function(a, b) a + b
fun2 <- function(aaa, ...) fun1(...)
fun2(a = 1, b = 2)
# Error in fun1(...) : argument "a" is missing, with no default
fun2(aaa = 99, a = 1, b = 2)
# [1] 3

As suggested by the example above, to get around your problem, explicitly specify tol in your call to HDIofICDF, e.g.

HDIofICDF( invcdf.posterior, t = 2.81, N1 = 10 , N2 = 10, rscale = 1, tol = 1e-8)

这篇关于应用函数来查找分布的高密度区域(编码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆