0

I have calculated the required Safety Stock using the excel goal seek function. Below is the image. Goal Seek Using Excel But now I want to do the same using R.

The below function gives me the same excel results when I enter the SafetyStock & SD. Now I need to do the reverse calculation(Whenever I provide x & SD I need the SS). Could someone help me with the same?

I tried Optix and other similar R packages but couldn't succeed.

opt<-function(SS,SD){

  x=-SS*(1-pnorm(SS/SD)) + SD * dnorm(SS/SD,mean=0,sd =1,0)
  print(x)

}

Excel Goal seek

June7
  • 19,874
  • 8
  • 24
  • 34
Melvin Roy
  • 11
  • 5

1 Answers1

0

Solving f(x)=c for x is the same as solving f(x)-c=0. You can use uniroot to find the root:

f <- function(SS, SD, ESC) {
  -SS*(1-pnorm(SS/SD)) + SD * dnorm(SS/SD,mean=0,sd =1,0) - ESC
}

zero <- uniroot(f,c(0,1000),SD=600,ESC=39.3)
zero$root

The second argument is the interval to search: between 0 and 1000. This returns

674.0586 

The zero structure has more interesting information:

$root
[1] 674.0586

$f.root
[1] 1.933248e-08

$iter
[1] 8

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39