1

I have 16068 datapoints with values that range between 150 and 54850 (mean = 3034.22). What would the R code be to generate a set of random numbers that grow in frequency exponentially between 54850 and 150?

I've tried using the rexp() function in R, but can't figure out how to set the range to between 150 and 54850. In my actual data population, the lambda value is 25.

set.seed(123)
myrange <- c(54850, 150)
rexp(16068, 1/25, myrange)

The call produces an error.

Error in rexp(16068, 1/25, myrange) : unused argument (myrange)

The hypothesized population should increase exponentially the closer the data values are to 150. I have 25 data points with a value of 150 and only one with a value of 54850. The simulated population should fall in this range.

jay.sf
  • 60,139
  • 8
  • 53
  • 110

1 Answers1

0

This is really more of a question for math.stackexchange, but out of curiosity I provide this solution. Maybe it is sufficient for your needs.

First, ?rexp tells us that it has only two arguments, so we generate a random exponential distribution with the desired length.

set.seed(42)         # for sake of reproducibility
n <- 16068
mr <- c(54850, 150)  # your 'myrange' with less typing

y0 <- rexp(n, 1/25)  # simulate exp. dist.
y <- y0[order(-y0)]  # sort

Now we need a mathematical approach to rescale the distribution.

# f(x) = (b-a)(x - min(x))/(max(x)-min(x)) + a
y.scaled <- (mr[1] - mr[2]) * (y - min(y)) / (max(y) - min(y)) + mr[2]

Proof:

> range(y.scaled)
[1]   150.312 54850.312

That's not too bad.

Plot:

plot(y.scaled, type="l")

enter image description here

Note: There might be some mathematical issues, see therefore e.g. this answer.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thanks so much, @jay.sf. There is a small bit of trouble with the scaled code: `y.scaled <- (mr[1] - mr[2]) * (y - min(y)) / (max(y) - min(y)) + mr[2]` Once I put it in that way, it worked perfectly. – David Wright Jan 29 '19 at 11:21
  • Very welcome. Just a typo thanks, I've edited it. Please [mark the question as answered](https://meta.stackexchange.com/a/5235/371738), this will stop people spending time on answering a question that has already been answered, thanks. – jay.sf Jan 29 '19 at 11:26