mfarr
mfarr

Reputation: 55

R purrr function: Sensitivity with multiple parameters

I have been working on this problem and can't seem to figure it out.

I have a dataset show below in which I want to run a cashflow sensitivity to price. The dataset will have appended rows, but the problem I am running into is using the different prices with the unique labor cost and matching them to their unique stations.

a <- data.frame(Name = "station1", units_sold = seq(2000, 3000, by = 2))
b <- data.frame(Name = "station2", units_sold = seq(1000, 2000, by = 2))

monthly_sales <- rbind(a, b)

price_1 <- c(2,3,4,5,6) #price for station_1
price_2 <- c(3,4,5,6,7) #price for station_2

labor1 <- c(1000) #labor for station_1
labor2 <- c(2000) #labor for station_2

I am using the code below and I am not able to apply price_1 and labor_1 to station_1, and then price_2 and labor_2 to station_2 for my sensitivity.

How do I setup my cf_model function or the map function to match the unique labor and price parameters?

Any help would be greatly appreciated!

cf_model <- function(x)
{
  data.frame(Scenario = (monthly_sales$units_sold * price_1[x]))
}
sensitivity_model <- data.frame(map(1:5, cf_model)) 

Upvotes: 1

Views: 127

Answers (2)

chappers
chappers

Reputation: 2415

I believe what you are trying to do is to use map with 2 parameters, which would be map2.

In this case your function cf_model would be a function which takes in two parameters, being the parameter for price_1 and price_2; you should never really need to index it.

In this example it would be:

cf_model <- function(price_1, price_2){
  data.frame(Scenario = (a$units_sold*price_1 + b$units_sold*price_2))
}
sensitivity_model <- data.frame(map2(price_1, price_2, cf_model))

head(sensitivity_model)

Notice I did not use your table monthly_sales. If I was to use it it would be:

monthly_sales <- data.frame(station1=a$units_sold, 
                            station2=b$units_sold)
sensitivity_model <- data.frame(map2(price_1, price_2, 
                       ~ list(Sensitivity=.x*monthly_sales$station1 + .y*monthly_sales$station2)))

In both cases it will have the same output:

> head(sensitivity_model)
  Scenario Scenario.1 Scenario.2 Scenario.3 Scenario.4
1     7000      10000      13000      16000      19000
2     7010      10014      13018      16022      19026
3     7020      10028      13036      16044      19052
4     7030      10042      13054      16066      19078
5     7040      10056      13072      16088      19104
6     7050      10070      13090      16110      19130

Upvotes: 1

JCR
JCR

Reputation: 71

It is hard to figure out what you want to do exactly. Do you want to find out what net revenue you will have if you have different assumptions about items sold, price per item and (total) labor costs?

If this is so I think the quickest way would look like e.g.

    scenario_station_1 <- data.frame(a$units_sold %*% t(price_1) - labor1)
    names(scenario_station_1)<-as.character(price_1)

Upvotes: 2

Related Questions