[英]How to generate a negative exponential distribution in R
I was manually creating a negative exponent distribution today and was trying to figure out a faster/easier solution.我今天手动创建了一个负指数分布,并试图找出一个更快/更简单的解决方案。 First, I just manually crafted a geometric sequence such as this one, multiplying constantly by .60 til I neared zero:首先,我只是手动制作了一个几何序列,比如这个,不断地乘以 0.60 直到我接近于零:
x <- 400
x*.60
Doing this about 20 times, I got this vector of solutions and plotted the distribution, as seen below:这样做大约 20 次,我得到了这个解向量并绘制了分布,如下所示:
y <- c(400,240,144,86.4, 51.84, 31.104, 18.6624, 11.19744, 6.718464, 4.031078,
2.418647, 1.451188, .8707129, .5224278, .3134567, .188074, .1128444,
.06770664, .04062398, .02437439)
plot(y)
However, I was trying to figure out what must be an easier way of doing this with seq
, but I only know how to do this with arithmetic sequences.但是,我试图找出使用seq
执行此操作的更简单方法,但我只知道如何使用算术序列执行此操作。 I tried reproducing what I did below:我尝试重现我在下面所做的事情:
plot(seq(from=400,
to=1,
by=-.60))
Which obviously doesn't produce the same effect, causing a very linear decline when plotted:这显然不会产生相同的效果,在绘制时会导致非常线性的下降:
Is there an easier solution?有更简单的解决方案吗? I have to imagine that this is a rather basic function within R.我不得不想象这是 R 中一个相当基本的功能。
You may use dexp
.您可以使用dexp
。
(x <- dexp(1:20, rate=.5)*1000)
# [1] 303.26532986 183.93972059 111.56508007 67.66764162 41.04249931 24.89353418 15.09869171 9.15781944 5.55449827
# [10] 3.36897350 2.04338572 1.23937609 0.75171960 0.45594098 0.27654219 0.16773131 0.10173418 0.06170490
# [19] 0.03742591 0.02269996
plot(x)
To make it start exactly at 400, we can minimize (400 - dexp(1, rate=.5)*x)^2
using optimize
.为了让它准确地从 400 开始,我们可以使用优化(400 - dexp(1, rate=.5)*x)^2
最小optimize
。
f <- function(x, a) (a - dexp(1, rate=.5)*x)^2
xmin <- optimize(f, c(0, 4000), a=400)
(x <- dexp(seq_len(20), rate=.5)*xmin$minimum)
# [1] 400.00000000 242.61226389 147.15177647 89.25206406 54.13411329 32.83399945 19.91482735 12.07895337 7.32625556
# [10] 4.44359862 2.69517880 1.63470858 0.99150087 0.60137568 0.36475279 0.22123375 0.13418505 0.08138735
# [19] 0.04936392 0.02994073
Note that if you want any different rate=
you should to use it both in optimize
and when creating the values.请注意,如果您想要任何不同的rate=
,您应该在optimize
和创建值时使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.