[R] Need help
John Fox
j|ox @end|ng |rom mcm@@ter@c@
Thu Aug 4 16:00:58 CEST 2022
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))
return(answer)
}
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))
>>> return(answer)
>>> }
>>> ########
>>> out=uniroot(ff, lower = 0, upper = 10000 )$root
>>> out
>>>
>>> ########### Error ########
>>> Error in uniroot(ff, lower = 0, upper = 10000) :
>>> f.upper = f(upper) is NA
>>>
>>> Please help me. Thanks in advance.
>>>
>>> Take care.
>>>
>>> Hossain
>>>
>>
>
>
--
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/
More information about the R-help
mailing list