Skip to content

Need Help To Solve An Equation In R

3 messages · Neetu Shah, Bert Gunter, David Winsemius

#
Dear Sir/Ma'am,

I am trying to make a function to solve an equation that is given below:
findH <- function(p_star, k){
fun <- function(y){
(pnorm(y+h))^(k-1)*dnorm(y)
}
lhs <- integrate(fun, -Inf, Inf)$value
return(uniroot(lhs, lower = -10, upper = 10,
tol = p_star)$root)
}
In lhs, I integrated a function with respect to y and there would be an
unknown h that I need to find.
I need to equate this lhs to p_star value in order to find h. Which
function should I use to solve this equation?
Please guide me regarding this.
Thank you.
#
Homework? We generally don't do homework here.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Sat, May 27, 2017 at 9:16 AM, Neetu Shah <nshah.0452 at gmail.com> wrote:
#
uniroot needs a function as its first argument. `integrate(fun, -Inf, Inf)$value` does not return a function. it will return a numeric value (if it succeeds). 

I have no idea whether this makes any mathematical sense because you have not described the background for this effort and have not shown how you expect to call `findH`. Is `h` supposed to be some sort of non-centrality parameter with `k` representing degrees of freedom?

I tried this code that does run, although it fails gracefully at runtime:

findH <- function(p_star= 0.004, k=2){
   fun <- function(y,h){
    (pnorm(y+h))^(k-1)*dnorm(y)
       }
    lhs <- function(x) { integrate(fun, lower=-Inf, upper=Inf, h=x)$value }
    return(uniroot(lhs, lower = -10, upper = 10, tol = p_star)$root)
}
#-------
Error in uniroot(lhs, lower = -10, upper = 10, tol = p_star) : 
  f() values at end points not of opposite sign
David Winsemius
Alameda, CA, USA