Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve a function in R similar to Goal Seeker in Excel

Tags:

r

solver

I saw already a similar question, but I can´t figure out, how to do. Maybe you can help.

Back solving a function or goal seek in R

ES <- function(y, z){-y * (1-pnorm(y/z)) + z * dnorm(y/z)} # = x
ES(906.19, 707.1) #33.47587

What I want is to solve for y, so z and x (33.4) is known. I have seen the solve and optim function, but I am not able to get the expected result.

Thanks!

like image 584
Sven Avatar asked Jul 18 '18 13:07

Sven


Video Answer


1 Answers

We can use uniroot to find the root of ES(y, z) - x for y given values z = 707.1 and x = 33.4.

ES <- function(y, z) -y * (1 - pnorm(y / z)) + z * dnorm(y / z)
res <- uniroot(function(x, y, z) ES(y, z) - x, c(0, 1000), z = 707.1, x = 33.4)

The solution for y is then

res$root
#[1] 906.9494

We confirm that E(y, z) = x

ES(res$root, 707.1)
#[1] 33.4
like image 91
Maurits Evers Avatar answered Sep 24 '22 12:09

Maurits Evers