Reputation: 179
I'm trying build this function that takes two lists and a single input.
What I have so far:
CLV_general <- function(profit, alpha, r){
CLV = 0
t = length(profit)
for (i in 1:length(profit)){
for (p in profit){m = p}
for (t in 0:t){T = t}
for(a in alpha){A = a}
clv = (m*A)/((1 +r)^T)
CLV = CLV + clv
}
return(CLV)
}
Calling the function is based off:
alpha = c(1, .96, .92, .88, .84, .80)
profits = c(100, 100, 100, 100, 100,100)
CLV_general(profits, alpha, .10)
But returns a value of 270.9475 but it should return of value of:
> (100*1/((1+.10)^0)) + (100*.96/((1+.10)^1)) + (100*.92/((1+.10)^2)) + (100*.88/((1+.10)^3)) + (100*.84/((1+.10)^4)) + (100*.80/((1+.10)^5))
[1] 436.4683
The function is based off of the formula SUM(Mt*At/((1+r)^t) where Mt is each value sequential value in profits above, A is sequential value of alpha above, rate is constant and t is time 0 to length of profits or alpha.
I've been stuck on this for like 5 hours and I'm just missing SOMETHING. Thanks!!!
Upvotes: 2
Views: 23
Reputation: 388982
This seems to be a vectorised operation. Try :
CLV_general <- function(profit, alpha, r){
sum((profit*alpha/((1 + r)^(seq_along(alpha) - 1))))
}
alpha = c(1, .96, .92, .88, .84, .80)
profits = c(100, 100, 100, 100, 100,100)
CLV_general(profits, alpha, .10)
#[1] 436.4683
Upvotes: 1