# [R] Need help

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Aug 4 17:24:49 CEST 2022

```Hello,

Like others have said, the root does depend on the seed but not by much.
Here is a loop finding the roots for seeds from 1 to 10,000.

I have defined a variable n == 1000 instead of having the calls to rnorm
depend on a hard-coded constant.

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))
}

n <- 1000
s <- n
lam <- 0.15
thr <- 70
r <- 10

up_lims <- 112.5   # found by trial and error to be nice most
# of the time, see mean(ok) below
out_list <- vector("list", length = 1e4)

for(i in seq_along(out_list)) {
# give the user feedback
if(i %% 100 == 0) print(i)

set.seed(i)
a <- rnorm(n, 110, 5)
b <- rnorm(n, -0.3, 0.4)

out_list[[i]] <- tryCatch(
uniroot(ff, lower = 0, upper = up_lims),
error = function(e) e
)
}

ok <- !sapply(out_list, inherits, 'error')
mean(ok)
# 0.9978

root <- sapply(out_list[ok], '[[', 'root')
hist(root)

Hope this helps,

Às 15:37 de 04/08/2022, Ebert,Timothy Aaron escreveu:
> And one last edit.
> Rstudio Environment shows out\$root as 112. However, if I then print out\$root I get 111.5303 which is much closer to the right answer.  Entering values for ff, I can work out the answer to slightly more than 111.53030196.
>
> Regards,
> Tim
>
> -----Original Message-----
> From: Ebert,Timothy Aaron <tebert using ufl.edu>
> Sent: Thursday, August 4, 2022 10:28 AM
> To: Ebert,Timothy Aaron <tebert using ufl.edu>; Md. Moyazzem Hossain <hossainmm using juniv.edu>; J C Nash <profjcnash using gmail.com>
> Cc: r-help mailing list <r-help using r-project.org>
> Subject: RE: [R] Need help
>
> I wish I could edit my earlier reply.
> ff(144.43) returns 0.03142121, but ff(144.44) returns NaN.
> ff(12) returns -0.1468287
>
> out=uniroot(ff, lower=12, upper =144) does not have errors.
> out is a list of 5.
> \$root is 112
> \$f.root is 1.09e-08
> \$iter is 5
> \$Init.it is NA
> \$estim.prec is 6.1e-05
>
> Is this working?
>
> Regards,
> Tim
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ebert,Timothy Aaron
> Sent: Thursday, August 4, 2022 10:09 AM
> To: Md. Moyazzem Hossain <hossainmm using juniv.edu>; J C Nash <profjcnash using gmail.com>
> Cc: r-help mailing list <r-help using r-project.org>
> Subject: Re: [R] Need help
>
> [External Email]
>
> A few issues
> 1) What does this function do? If you describe the problem and goal there might be a better answer. We appreciate seeing that you have attempted a solution, but you have trapped us all in blindly following your attempt.
>
> 2) This is an error checking phase of programming. Setting the seed is a good idea. Just remember to unset the seed after finishing the debugging phase.
>
> 3) In uniroot you call the function, but the function expects an argument (zz) that is not provided, and there is no default.
>
> 4) I set.seed(42), and entered ff(12) getting an answer of -0.1468287. Is this the expected result?
>
> Regards,
> Tim
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Md. Moyazzem Hossain
> Sent: Thursday, August 4, 2022 9:50 AM
> To: J C Nash <profjcnash using gmail.com>
> Cc: r-help mailing list <r-help using r-project.org>
> Subject: Re: [R] Need help
>
> [External Email]
>
> 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
>>>
>>
>
>
> --
> Best Regards,
> Md. Moyazzem Hossain
> Associate Professor
> Department of Statistics
> Jahangirnagar University
>
>          [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see