# [R] Need help

Md. Moyazzem Hossain ho@@@|nmm @end|ng |rom jun|v@edu
Thu Aug 4 21:05:34 CEST 2022

```Dear John

Thank you very much for your feedback.

Take care.

Hossain

On Thu, Aug 4, 2022 at 3:01 PM John Fox <jfox using mcmaster.ca> wrote:

> Dear Hossain,
>
> Did you look at the values that your function returns? As John Nash
> pointed out, these values presumably depend on the random values of a and
> b.
>
> I tried the following:
>
> set.seed(123) # for reproducibility
> a=rnorm(1000, 110, 5)
> b = rnorm(1000, -0.3, 0.4)
> s = length(a)
> lam=0.15
> thr=70
> r= 10
>
> ff = function(zz){
>    inner = vector("numeric", length = s)
>    for(k in 1:s){
>      inner[k]=(1- lam*((1+b[k]*((zz-thr)/a[k]))^(-1/b[k])))
>    }
>    answer = mean(inner)- (1- (1/r))
> }
>
> res <- sapply(0:10000, ff)
> res[1:500]
> plot(0:150, res[1:151], type="l")
>
>
> It was quickly clear that ff() produces NaNs for all but the smallest
> values of zz.
>
> Beyond this fact, it's unclear to me what the purpose of the computation
> is. In the short domain where it produces numbers, the function returns
> both negative and positive values (for my values of a and b) -- that is,
> does cross 0 -- and if it did not, it would be nonsense to look for roots.
>
> I hope this helps,
>   John
>
>
> On 2022-08-04 9:49 a.m., Md. Moyazzem Hossain wrote:
> > Dear JN,
> >
> > Thanks.
> >
> > I do not check whether the function actually crosses zero or not.
> However,
> > by assumption, the value would be greater than zero.
> >
> > Hossain
> >
> > On Thu, Aug 4, 2022 at 2:40 PM J C Nash <profjcnash using gmail.com> wrote:
> >
> >> Have you checked that your function actually crosses zero?
> >>
> >> You should also set a seed if you want a reproducible result.
> >>
> >> JN
> >>
> >> On 2022-08-04 09:30, Md. Moyazzem Hossain wrote:
> >>> Dear R Experts,
> >>>
> >>> I hope that you are doing well.
> >>>
> >>> I am facing a problem to find out the value of the following function.
> I
> >>> need help in this regard.
> >>>
> >>> #####
> >>> a=rnorm(1000, 110, 5)
> >>> b = rnorm(1000, -0.3, 0.4)
> >>> s = length(a)
> >>> lam=0.15
> >>> thr=70
> >>> r= 10
> >>>
> >>> ff = function(zz){
> >>>     inner = vector("numeric", length = s)
> >>>        for(k in 1:s){
> >>>         inner[k]=(1- lam*((1+b[k]*((zz-thr)/a[k]))^(-1/b[k])))
> >>>             }
> >>>     answer = mean(inner)- (1- (1/r))
> >>>     }
> >>> ########
> >>> out=uniroot(ff, lower = 0, upper = 10000 )\$root
> >>> out
> >>>
> >>> ########### Error ########
> >>> Error in uniroot(ff, lower = 0, upper = 10000) :
> >>>     f.upper = f(upper) is NA
> >>>
> >>>
> >>> Take care.
> >>>
> >>> Hossain
> >>>
> >>
> >
> >
> --
> John Fox, Professor Emeritus
> McMaster University
> web: https://socialsciences.mcmaster.ca/jfox/
>
>

--
Best Regards,
Md. Moyazzem Hossain
Associate Professor
Department of Statistics
Jahangirnagar University