ZombiePlan37
ZombiePlan37

Reputation: 279

How to find the maximum of a multivariate function in R

I have a joint likelihood that I need to maximize which is determined by about 25 different variables. I was hoping there was a method for finding the combination of variable values that maximized this function without resorting to 25 'for' loops that cycle through every possible value.

Here is an example of a much smaller piece of this likelihood, maximized using the 'for' loop approach, where 'temp' calculates the value of the likelihood and test is a vector that records the values of the variables used:

h1=23
h2=31

test=c(NA,NA,NA,NA,0)
for (N in seq(60,150,10)) {
  for (p1 in seq(0.01,1,.01)) {
    for (p2 in seq(0.01,1,.01)) {
      for (S1 in seq(0.5,1,.005)) {
        temp=factorial(N)/(factorial(h1)*factorial(h2)*factorial(N-h1-h2))*(p1)^h1*((1-p1)*S1*p2)^h2*(1-(p1+(1-p1)*S1*p2))^(N-h1-h2)
        if (temp>test[5]) test=c(p1,p2,S1,N,temp)
      }
    }
  }
}
test

Upvotes: 0

Views: 2026

Answers (0)

Related Questions