how to solve this problem in R: In while (t <= cc[i]) { … : the condition has length > 1 and only the first element will be used

允我心安 提交于 2020-01-16 05:36:11

问题


big thank you and warm regards for the answer Mr. Tom, for the detail problem may you can help me the following step :

I have a problem with R Program,

first, I make a trendlinier (At+B) from this data set

timedelay<-c(76.77,65.88,60.96,51.61,48.72,39.86,35.43,33.36,30.32,23.96,16.95,3.564)
timedelayts<-ts(timedelay,start=c(2011,1),freq=12)
plot(timedelayts,ylab="Time Delay",col="blue")
time=ts(1:length(timedelayts),start=c(2011,1),freq=12)
trendlinier<-lm(timedelayts~time)
trendlinier
Call:
Coefficients:
(Intercept)         time
78.38              -5.81
Avalue<-(-5.81)
Bvalue<-78.38

From fitdist I get a cauchy distribution and result like this

fitcauchy<-fitdist(filterdelay, "cauchy")
summary(fitcauchy)
Fitting of the distribution ' cauchy ' by maximum likelihood 
Parameters : 
          estimate Std. Error
location 125.08885   2.215711
scale     34.97553   2.143992
Loglikelihood:  -3436.131   AIC:  6876.262   BIC:  6884.852 
Correlation matrix:
          location     scale
location 1.0000000 0.2219629
scale    0.2219629 1.0000000

and then I put parameter location and scale in

parlocation<-fitcauchy$estimate[1]
parscale<-fitcauchy$estimate[2]

After that, I make a function

FU0 <- function(tt){pcauchy((tau-tt)*Bvalue/((Avalue*tt)+Bvalue),location=parlocation,scale=parscale)}

then, I have lambdaj=λj and lambda2(tt)=λ(t) and data set for lambdaj like this

lambdaj<-c(8.665351,178.934646,161.189187,57.680814,43.540869,102.405160,108.003538,157.690273,138.600075,269.314099,353.897407,197.891330)

with λ1=8.665351,λ2=178.934646,...,λ2=197.891330. Then, I have data set sequenceday like this

sequenceday<-c(0,31,62,90,121,151,182,212,243,274,304,335,365)

and then, I make this function for λ(tt) in R for implementation this formula,

division 0 = d0< d1=31 <d2=62 . . . < d12 =365,

for tt ∈ (dj−1, dj ] the intensity function λ(tt) is equal to λj.

lambda2 <- function(tt){
  for (i in 1:(length(sequenceday))) {
    while (tt <=sequenceday[i]) {
      return(lambdaj[i-1])
    }
  }
}

I want to result that function following :

example:

"tt in a day, if I input 63 days to function lambda2(63) where 63 ∈ (d2=62, d3=90 ], then lambda2(63)=λ3=161.189187"

when I run function lambda2(63) in R, the result is:

> lambda2(63)
[1] 161.2404

until this step is run correctly,

so next step, I want to implementation lambda2 into integrate() in R, with the function IntensityIBNR Like this

intensityIBNR <- function(tt){lambda2(tt)*(1-FU0(tt))}

when I run this code

cc <- integrate(f=intensityIBNR, lower=0, upper=tau,
           subdivisions=200)

its result like this

There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In while (tt<=sequenceday[i]) { ... :
the condition has length > 1 and only the first element will be used

Please, help me to solve this problem..

来源:https://stackoverflow.com/questions/59185747/how-to-solve-this-problem-in-r-in-while-t-cci-the-condition-has-le

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!