简体   繁体   中英

Call a function with multiple arguments inside uniroot in R

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?

将您的函数包装在另一个带有一个参数的函数中:

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

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