I have what I think is a fairly simple ask but I don't seem to be finding a solution for it.
Here's a reproducible example
Cost <- c(100,200,300,400,500)
Savings <- c(10,20,30,40,50)
Fees <- c(20,20,20,20,20)
Data <- data.frame(Cost,Savings,Fees)
ROI <- function(x) {
sum(subset(Data,Cost>=x)$Savings)/sum(subset(Data,Cost>=x)$Fees)
}
Basically I want to find the optimum value of the Cost (x) which will make the ROI = 2
In Excel what I do is
Assuming you have Cost, Savings, and Fees in columns A, B, and C respectively.
In cell E2, I wrote the function below
SUMIF($A$2:$A$6,H2,$B$2:$B$6)/SUMIF($A$2:$A$6,H2,$C$2:$C$6)
Where H2 has the following function: CONCATENATE(">",G2)
G2 is the parameter of interest here.
I then go to Data What-If analysis > Goal Seek
Set Cell E2 to Value 2 by changing G2
Is there a way to replicate Excel's steps in R? Thank you!
PS in R, If I did
ROI (201) or ROI (299)
I get 2
What I want to find is the 201.
You could try the uniroot()
function to find the value x where your ROI function - 2 ==0.
Cost <- c(100,200,300,400,500)
Savings <- c(10,20,30,40,50)
Fees <- c(20,20,20,20,20)
Data <- data.frame(Cost,Savings,Fees)
ROI <- function(x, targetROI) {
y<-sum(subset(Data,Cost>=x)$Savings)/sum(subset(Data,Cost>=x)$Fees)
y-targetROI
}
uniroot(ROI, c(0,500), targetROI=2)
Note Since there is a step change in your function, there are multiple roots this function from x=201 to x=300
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.