Alex
Alex

Reputation: 11

R uniroot: Solving an unknown variable in uniroot from a function with multiple arguments

I am trying to obtain the value of a variable that is inside a log by using the uniroot function. In the function with the equation I want to solve, I intend on having a vector argument so that I can multiply values from that vector to each log.

I have tried the method shown in this thread: Call a function with multiple arguments inside uniroot in R but have not been successful. Please help. Thank you.

prob.vals <- c()
x <- 1
for (i in 0:4) {
  prob.calcs = q^(i)*(1-q)^(4-i)
  prob.vals[x] = prob.calcs
  x <- x + 1
}

# want to find the value of P
f = function(P, B = c()) {
  log(5 + 4*P - 4)*B[5] + log(5 + 4*P - 3)*B[4]
  + log(5 + 4*P - 2)*B[3]+log(5 + 4*P - 1)*B[2] +
    log(5 + 4*P)*B[1] - log(5)
}

uniroot(function(P) f(P, B = prob.vals), lower = 0, upper = 99999999)$root

Upvotes: 1

Views: 244

Answers (0)

Related Questions