- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用rpois()
创建泊松模拟。我有小数点后两位利率的分布,我想知道这些利率是否具有泊松分布而不是正态分布。
rpois()
函数返回正整数。我希望它返回小数点后两位正数。我尝试过以下方法
set.seed(123)
trialA <- rpois(1000, 13.67) # generate 1000 numbers
mean(trialA)
13.22 # Great! Close enough to 13.67
var(trialA)
13.24 # terrific! mean and variance should be the same
head(trialA, 4)
6 7 8 14 # Oh no!! I want numbers with two decimals places...??????
# Here is my solution...but it has a problem
# 1) Scale the initial distribution by multiplying lambda by 100
trialB <- rpois(1000, 13.67 * 100)
# 2) Then, divide the result by 100 so I get a fractional component
trialB <- trialB / 100
head(trialB, 4) # check results
13.56 13.62 13.26 13.44 # terrific !
# check summary results
mean(trialB)
13.67059 # as expected..great!
var(trialB)
0.153057 # oh no!! I want it to be close to: mean(trialB) = 13.67059
如何使用 rpois()
生成具有泊松分布的正两位小数数字。
我知道泊松分布用于计数,并且计数是正整数,但我也相信泊松分布可用于建模速率。这些比率可能只是正整数除以标量。
最佳答案
如果您缩放泊松分布以更改其均值,结果将不再是泊松分布,并且均值和方差不再相等 - 如果您将均值缩放一个因子 s
,则方差会改变 s^2
因子。
您可能想使用 Gamma 分布。 Gamma 的均值是 shape*scale
,方差是 shape*scale^2
,因此您必须使用 scale=1
来获得均值和方差相等的实数、正数:
set.seed(1001)
r <- rgamma(1e5,shape=13.67,scale=1)
mean(r) ## 13.67375
var(r) ## 13.6694
您可以四舍五入到小数点后两位,而不会极大地改变均值和方差:
r2 <- round(r,2)
mean(r2) ## 13.67376
var(r2) ## 13.66938
与泊松分布比较:
par(las=1,bty="l")
curve(dgamma(x,shape=13.67,scale=1),from=0,to=30,
ylab="Probability or prob. density")
points(0:30,dpois(0:30,13.67),type="h",lwd=2)
关于r - 从 rpois() 生成正实数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672403/
我正在尝试使用rpois()创建泊松模拟。我有小数点后两位利率的分布,我想知道这些利率是否具有泊松分布而不是正态分布。 rpois() 函数返回正整数。我希望它返回小数点后两位正数。我尝试过以下方法
我正在调试一组更大的嵌套模型,这些模型仅在优化过程中遇到问题。在我认为导致错误的归零过程中,我在 rpois() 中遇到了异常行为。功能。 似乎平均值非常大,rpois()返回 NA而不是一个值。此问
在 R 中,rpois可以传递一个描述多个泊松分布的 lambda 向量,例如 rpois(5, (1:5)*1000) # [1] 1043 1974 3002 3930 4992 上图中,输出向量
我正在尝试在 tibble 中生成随机数,其中不同类别的项目分布不同。我运气不好。这是代码及其产生的错误: library(dplyr) letters % rowwise() %>% mut
我必须为大小为 N 的向量的两组生成随机数。 一组的概率为 p,另一组的概率为 q = 1-p。(例如,对于 p=0.5 的 1000 人,我必须从一个分布生成 500 个随机数,从另一个分布生成 5
我必须为大小为 N 的向量的两组生成随机数。 一组的概率为 p,另一组的概率为 q = 1-p。(例如,对于 p=0.5 的 1000 人,我必须从一个分布生成 500 个随机数,从另一个分布生成 5
我是一名优秀的程序员,十分优秀!