1

I am trying to call a function with multiple arguments inside uniroot, to solve and find the value of x. Code below,

mean1 = 0
mean2 = 1

sigma1 = 0.5
sigma2 = 0.5

priors1 = 0.6
priors2 = 0.1

threshold = function(mu1, sigma1, mu2, sigma2, prior1, prior2, x) {
    (dnorm(x,mu1,sigma1) * prior1 - dnorm(x, mu2, sigma2) * prior2) 
}

uniroot(threshold(mean1,sigma1,mean2,sigma2, priors1, priors2), c(0,5))

But the call to uniroot fails, since the function expects x to be passed as well. How do i solve this?

Prav
  • 15
  • 1
  • 4
  • 1
    `uniroot` takes a "additional arguments" argument for such functions. Here, it should be `uniroot(threshold, c(0, 5), mu1 = mean1, sigma1 = sigma1, mu2 = mean2, sigma2 = sigma2, prior1 = priors1, prior2 = priors2)` – alexis_laz Feb 18 '14 at 11:10

1 Answers1

5

Wrap your function in another function that takes one argument:

uniroot(function(x) threshold(mean1,sigma1,mean2,sigma2, priors1, priors2,x),c(0,5))
nograpes
  • 18,623
  • 1
  • 44
  • 67