Here is a simple base R implementation, see my comment for details.
I will leave it to you to work out a version which works well with {dplyr}
syntax. The data masking is different. "dplyr
-style" is close to subset()
.
If you need assistance, do not hesitate to comment.
Data
data3 = data.frame(
customer = c(1,2,3),
frequency = c(30,32,36),
recency = c(72,71,74),
TX = c(74,72,77),
monetary_value = c(35.654,47.172187,30.603611))
Implementation
of log_div_mean()
(do you have a reference for the calculation?)
log_div_mean = \(.data, # data
.x, .y, .z, # columns of interest
a = .6866195, b = 2.959643, # default values
r = .2352725, alpha = 4.289764 # which can be overwritten
) {
.u = .data[[.x]]
r1 = r + .u
r2 = log( (alpha + .data[[.y]]) / (alpha + .data[[.z]]) )
r3 = log(a / (b + max(c(.u, 1)) - 1)) # typo in your max?
rr = r1 * r2 + r3
1 / (1 + exp(rr))
}
where we use the variable naming routine present in the {tidyverse}.
Application
> log_div_mean(.data = data3, .x = "frequency", .y = "TX", .z = "recency")
[1] 0.9619502 0.9730688 0.9340070
Correct results?