[英]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
I choose division 0 = d0 < d1 < .我选择除法 0 = d0 < d1 < 。 .
. .
. < dm =tau, and then, for t ∈ (dj−1, dj ] the intensity function λ(t) is equal to λj.
< dm =tau,然后,对于 t ∈ (dj−1, dj ],强度函数 λ(t) 等于 λj。
My data set我的数据集
parlocation<-125.0949
parscale<-34.98857
tau<-365
trendlinier$coefficients[1]<-78.37888
trendlinier$coefficients[2]<--5.809776
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)
deretbln2<-c(0,31,62,90,121,151,182,212,243,274,304,335,365)
and then, I make this function for λ(t) in R然后,我为 R 中的 λ(t) 创建这个函数
lambda2 <- function(t){for (i in 1:12) {while (t <=deretbln2[i]) {return(lambdaj[i-1])}}}
I also make this function in R我也在 R 中创建了这个函数
FU0 <- function(t){pcauchy((tau-t)*trendlinier$coefficients[1]/((trendlinier$coefficients[2]*t)+trendlinier$coefficients[1]),location=parlocation,scale=parscale)}
intensityIBNR <- function(t){lambda2(t)*(1-FU0(t))}
when I run this code当我运行此代码时
c <- 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 (t <= deretbln2[i]) { ... :
the condition has length > 1 and only the first element will be used
Please, help me to solve this problem..请帮我解决这个问题..
a few remarks on the side:边上的几点说明:
1) the object trendlinier
is non-existant in your MWE; 1) 对象
trendlinier
在您的 MWE 中不存在; hence trendlinier$coefficients[1]
should terminate with an error.因此,
trendlinier$coefficients[1]
应该以错误结束。 We need to instantiat that object first as我们需要先将该对象实例化为
trendlinier <- list(coefficients = NULL)
2) I would recommend not using t
or c
as object names in R , since both are used for function names; 2)我建议不要在R 中使用
t
或c
作为对象名称,因为两者都用于函数名称; rather use tt
or cc
(or better, variable names that have more meaning);而是使用
tt
或cc
(或者更好,具有更多含义的变量名称); Let's stay with tt
and cc
(and leave the lambda2
- function on the side), we'll obtain让我们继续使用
tt
和cc
(并将lambda2
- 函数放在一边),我们将获得
parlocation<-125.0949
parscale<-34.98857
tau<-365
trendlinier$coefficients[1]<-78.37888
trendlinier$coefficients[2]<--5.809776
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)
deretbln2<-c(0,31,62,90,121,151,182,212,243,274,304,335,365)
FU0 <- function(tt){pcauchy((tau-tt)*trendlinier$coefficients[1]/((trendlinier$coefficients[2]*tt)+trendlinier$coefficients[1]),location=parlocation,scale=parscale)}
intensityIBNR <- function(tt){lambda2(tt)*(1-FU0(tt))}
cc <- integrate(f=intensityIBNR, lower=0, upper=tau, subdivisions=200)
Now, note that R 's indices start at 1;现在,请注意R的索引从 1 开始; that is regarding the
for
-loop in the lambda2
-function, for i=1
, there is no lambdaj[i-1]
.即关于
for
-loop在lambda2
-function,对于i=1
,没有lambdaj[i-1]
You need to consider and implement different routines for i=1
and i>1
(or add a default value to lambdaj
; eg, lambdaj <- c(0, lambdaj)
if 0
is a reasonble value in this case).您需要为
i=1
和i>1
考虑并实现不同的例程(或向lambdaj
添加默认值;例如,如果0
在这种情况下是lambdaj <- c(0, lambdaj)
值,则lambdaj <- c(0, lambdaj)
)。 (Another remark on the side: the statement while (expression) { return }
is an unusual implementation of the if
-clause). (旁边的另一个评论:语句
while (expression) { return }
是if
子句的一个不寻常的实现)。 If you prefer not altering lambdaj
you could use如果你不想改变
lambdaj
你可以使用
lambda2 <- function(tt){for (i in 1:12) {if (i == 1) return(0) else if (tt <= deretbln2[i]) return(lambdaj[i-1])}}
PS: that is not the best way to typeset R -Code (cf., Hadley's style guide ) PS:这不是排版R代码的最佳方式(参见Hadley 的风格指南)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.