Reputation: 15
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?
Upvotes: 1
Views: 3374
Reputation: 18323
Wrap your function in another function that takes one argument:
uniroot(function(x) threshold(mean1,sigma1,mean2,sigma2, priors1, priors2,x),c(0,5))
Upvotes: 5