[R] Need help

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Thu Aug 4 18:08:12 CEST 2022

```Another exceedingly polite questioner. Cultural differences!

I think we can skip discussing if we are doing well, and get to the point.

To start with, I got thrown by these two lines:

a=rnorm(1000, 110, 5)
s = length(a)

This does not relate to the difficulty, but is a sort of sloppy use as a was
created with a request to be 1,000 random numbers. No need to calculate the
length. You could just say

s <- 1000

We have no idea what the purpose of your function ff is meant to be used for
and what it is normally given as an argument as you seem to just pass the
function as an argument to a stats package function called uniroot. As such,
the function should return values for a given argument that can be evaluated
between lower and upper bounds. OK, it took some time to look things up that
would have been easier had you told us a bit more.

But it gets worse as your function references "s" in this line within:

inner = vector("numeric", length = s)

You are not passing s to the function but using it from outside to make an
empty variable of the same length? Similarly, you are putting other
variables like r, lam, thr  in a global environment and using it within the
function. Is this a good design?

As other have pointed out, you should evaluate the function ff to see what
it returns on it's own, not wait for uniroot to fail in calling it.

Your function ff seems to make a vector of 1,000 complicated values based on
the global values including the values in "a" and 'b" that are random then
take the mean of the results with an adjustment.  I have no idea if your
code is modeled after anything in particular or is correct. But I exercised
it in the lower range and it produces reasonable numbers:

sapply(0:10,ff)
[1] -0.1715207 -0.1693173 -0.1671317 -0.1649635 -0.1628125 -0.1606784
-0.1585609 -0.1564598
[9] -0.1543749 -0.1523059 -0.1502526

But in the upper range your function returns a NaN or NOT A NUMBER

sapply(9995:10000,ff)
[1] NaN NaN NaN NaN NaN NaN

That would explain the error message.

So you need to look at ff and see how the NaN is introduced perhaps just for
larger numbers. You may have an overflow problem for example where you are
making number too large to fit. Who knows?

What I know is when I make your 10,001 calculations only the first 140 or so
are not Nan. ff(142) and beyond all fail.

> sapply(0:10000,ff) -> temp
> sum(is.nan(temp))
[1] 9859

So it may be obvious to you why zz-thr has such a threshold, but not to me.

I would try to make sure you implemented the algorithm required correctly in
the code and that the range of random numbers you select fits the needs. If
you can get ff() to return only valid numbers, it may be a step. And note
that when I change the mean() function you call to add na.rm=TRUE, it
removes the NaN from the calculation (albeit that should not mean it is now
correct) and a new error shows up:

> 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, na.rm=TRUE)- (1- (1/r))
+ }

> out=uniroot(ff, lower = 0, upper = 10000 )\$root out
Error: unexpected symbol in "out=uniroot(ff, lower = 0, upper = 10000 )\$root
out"

You left a dangling " out" at the end. You may mean:

out=uniroot(ff, lower = 0, upper = 10000 )\$root

My current result is 111.6597 but as noted is meaningless as I threw out
lots of NaN.

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Md. Moyazzem
Hossain
Sent: Thursday, August 4, 2022 9:31 AM
To: r-help mailing list <r-help using r-project.org>
Subject: [R] Need help

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