简体   繁体   English

如何在 R 中定义概率质量 function

[英]How to define probability mass function in R

I want to define (implement) the probability mass function into the R environment when dealing with another calculation.我想在处理另一个计算时将概率质量 function 定义(实现)到 R 环境中。 Since not having much R experience, I do not know how to translate the equation with these parameters into code (already tried to search everywhere).由于没有太多的 R 经验,我不知道如何将带有这些参数的方程式翻译成代码(已经尝试到处搜索)。 Thanks for help.感谢帮助。 [![probability mass function given by this equation][1]][1] [![此方程给出的概率质量 function][1]][1]

This could be expressed via Beta distribution这可以通过Beta 分布表示

Your expression = BetaPDF(r, x) t (1-t) / x你的表达式 = BetaPDF(r, x) t (1-t) / x

P <- function(x, theta) {
    shape1 <- 2
    shape2 <- x
    dbeta(theta, shape1, shape2)*theta*(1.0-theta)/x
}

Please check the math, see also here .请检查数学,另请参见此处

UPDATE更新

Or express it as Beta PDF of higher powers或者表示为更高次幂的Beta PDF

Your expression = BetaPDF(r+1, x+1) r /((r+x+1)*(r+x))你的表达式 = BetaPDF(r+1, x+1) r /((r+x+1)*(r+x))

It's already a built in pmf in R - its there when you install R.它已经是 R 中的内置 pmf - 当您安装 R 时它就在那里。 If you have r and theta already defined, just call dnbinom如果您已经定义了rtheta ,只需调用dnbinom

dnbinom(x, size=r, prob=theta)

See the help ?dnbinom查看帮助?dnbinom

Don't waste your time debugging your own code when there's code that's already debugged and which is tested daily by large numbers of highly knowledgeable users (many research statisticians use it for example) - who should certainly have spotted anything but very rare problems by now.当有代码已经调试过并且每天由大量知识渊博的用户(例如许多研究统计学家使用它)进行测试时,不要浪费您的时间调试自己的代码——他们当然应该发现除了非常罕见的问题之外的任何东西. It will also likely be considerably more numerically stable and more efficiently implemented than you will write.它也可能比你写的在数值上更稳定,更有效地实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM