R中的Excel目标搜索 [英] Excel Goal seek in R
问题描述
我已经使用excel目标搜索功能计算了所需的安全库存.下面是图像. 但是现在我想使用R来做同样的事情.
I have calculated the required Safety Stock using the excel goal seek function. Below is the image. But now I want to do the same using R.
当我输入SafetyStock&时,以下功能会给我相同的excel结果.标清现在,我需要进行反向计算(每当我提供x& SD时,我就需要SS).有人可以帮我吗?
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?
我尝试了Optix和其他类似的R包,但没有成功.
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)
}
推荐答案
为x求解f(x)=c
与解决f(x)-c=0
相同.您可以使用uniroot
来找到根:
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
第二个参数是搜索间隔:0到1000.返回
The second argument is the interval to search: between 0 and 1000. This returns
674.0586
zero
结构具有更多有趣的信息:
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
这篇关于R中的Excel目标搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!