dimanche 13 décembre 2020

My if function gives me NA values if I'm trying to calculate the cost of a function

This are the three options for the amount of vaccines ordered by a firm

option1 <- 15000 option2 <- 18000 option3 <- 11000 options <- c(option1, option2, option3) n_simulations <- 13520

The demand for the vaccines differs from 10000 to 18000 with different probabilities of said demand to be happening Demand<-sample(c(10000, 12000, 13000, 14000, 15000,16000,17000,18000), n_simulations, replace = TRUE, prob = c(0.05,0.1,0.15,0.17,0.18,0.15,0.13,0.07))

Our vaccines are sold for 130 revenueD <- 130 Throwing away unused vaccines will cost $5 per dose DispC <- 5 Not having enough vaccines to meet the demand is penalised by paying $100 dollars for each demand higher than the existing stock Penalty <- 100 Variable cost for the vaccine is $40 variablecost <- 40 We calculate the revenues for each option revenue1 <- revenueD * option1 revenue2 <- revenueD * option2 revenue3 <- revenueD * option3

The cost is as follows: We calculate: if demand<stock, than the costs are the variable costs times the stock, disposable costs times the (stock-demand). If demand > stock, then we clculate variable cost times stock, disposable costs are non existing but we have to pay a penalty that is (demand-stock)*penalty

My function gives me this error message: >In mean.default(cost1) : argument is not numeric or logical: returning NA>

cost1 <- function(option1, Demand){ if(Demand>option1){ option1*variablecost + abs(Demand-option1)*Penalty} else{Demand*variablecost + abs(Demand-option1)*DispC}}

Aucun commentaire:

Enregistrer un commentaire