简体   繁体   中英

Create a Bound Exponential Column in R

How can I create an exponential percentage column in a data frame between the years 2022 and 2040 which is bound by the range 1% and x% where I can set the "peak" of the exponential equation?

In the expected outcome table below, the percentage starts to increase by 1%, then 2%... then "peaks" at a growth rate of 5% between 25%-35%, then decreases back to 2-3% growth rate.

Here's an example of what I would like to accomplish:

Example Outcome

end_growth_rate <- 48

2022    1%
2023    2%
2024    3%
2025    4%
2026    6%
2027    8%
2028    10%
2029    12%
2030    15%
2031    18%
2032    21%
2033    25%
2034    30%
2035    35%
2036    39%
2037    42%
2038    44%
2039    46%
2040    48%

Thanks a lot for any help! Much appreciated! Happy to explain more if any clarification is required.

The fact that you're putting something into a data.table or data.frame seems immaterial. What you're asking for is simply a function of start, end, and curve-type. This might be a sigmoid function (aka "logistic function").

Try this:

mysigmoid <- function(n, min, max, maxe = 4, mine = -maxe) {
  y <- 1 / (1 + exp(-seq(mine, maxe, len = max(0, n))))
  min + (max - min) * (y - y[1]) / (y[n] - y[1])
}

You'll need to play around with the two exponential endpoints ( maxe and mine , they correspond to the left/right x values used in the exponential equation in the sigmoid link above) to get the perfect spread. Using "-3 to 2", I'm able to get "starting with 1%, ending with 1.9%", which seems close to your communicated intent.

round(mysigmoid(19, 1, 49, 2, -3), 1)
#  [1]  1.0  1.8  2.9  4.2  5.8  7.9 10.3 13.1 16.4 20.0 23.9 27.9 31.8 35.6 39.1
# [16] 42.2 44.9 47.1 49.0
round(diff(mysigmoid(19, 1, 49, 2, -3)), 1)
#  [1] 0.8 1.0 1.3 1.6 2.0 2.4 2.9 3.3 3.6 3.9 4.0 4.0 3.8 3.5 3.1 2.7 2.3 1.9

(And then just assign them to the frame or table column of your choice.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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