简体   繁体   中英

Finding x and y at the maximum point of z using optim in R

I have a function in which x and y vary simultaneously. The result of plotting this function within a specified range for x and y is the picture below (see Full R code below the picture ).

Question:

I was wondering using an optimization command ( optim , nlm , etc.) in R, how can I get the value of x and y at the maximum point of z in this function?

在此处输入图片说明

Here is my R code:

Data = c( 182, 201, 221, 234, 237, 251, 261, 266, 267, 273, 
          286, 291, 292, 296, 296, 296, 326, 352, 359, 365 )

x = seq(200, 340, len = 30)

y = sqrt( seq(800, 8500, len = 30) )

f = function(x, y) Reduce(`*`, Map(dnorm, x = Data, mean = list(x), sd = list(y)))

z = outer(x, y, f)

persp(x, y, z , theta = 55, phi = 15, expand = 0.5, ticktype = "detailed", col = 'gold')

Here is a very simple and raw code for a first step toward the solution of your optimization problem:

Data = c( 182, 201, 221, 234, 237, 251, 261, 266, 267, 273, 
          286, 291, 292, 296, 296, 296, 326, 352, 359, 365 )

f = function(x) Reduce(`*`, Map(dnorm, x = Data, mean = list(x[1]), sd = list(x[2])))*(-10^50)

optim(c(200,100),f, method="BFGS", control=list(maxit=100000))

$par
[1] 277.59967  48.28371

$value
[1] -9981.604

$counts
function gradient 
    1723     1693 

$convergence
[1] 0

$message
NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM