简体   繁体   English

如何在 R 中解决这个问题:在 while (t <= cc[i]) { ... : 条件长度 > 1 并且只使用第一个元素

[英]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 中使用tc作为对象名称,因为两者都用于函数名称; rather use tt or cc (or better, variable names that have more meaning);而是使用ttcc (或者更好,具有更多含义的变量名称); Let's stay with tt and cc (and leave the lambda2 - function on the side), we'll obtain让我们继续使用ttcc (并将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=1i>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.

相关问题 解决方法:条件的长度&gt; 1,并且仅使用第一个元素 - How to solve: the condition has length > 1 and only the first element will be used 错误:条件的长度&gt; 1,并且在r中仅使用第一个元素 - Error: condition has length > 1 and only the first element will be used in r R if 语句错误:条件长度 &gt; 1 且仅使用第一个元素 - R if statement error: the condition has length > 1 and only the first element will be used R If 语句返回“条件长度 &gt; 1 且仅使用第一个元素” - R If statement returns “the condition has length > 1 and only the first element will be used” 条件长度 &gt; 1 并且仅使用第一个元素 - 在 R - the condition has length > 1 and only the first element will be used - in R 如果条件的长度&gt; 1并且仅使用第一个元素,为什么我会在R中收到此警告 - Why do I get this warning in R if, the condition has length > 1 and only the first element will be used 条件的长度&gt; 1,并且仅使用第一个元素 - The condition has length > 1 and only the first element will be used 条件的长度 &gt; 1 并且只有第一个元素 r - condition has length > 1 and only the first element r 如何修复“条件长度 &gt; 1 且仅使用第一个元素”的 VECM 警告消息? - How can I fix the VECM warning message of "the condition has length > 1 and only the first element will be used"? 如何解决此错误“条件的长度&gt; 1,并且仅使用第一个元素”? - How can I fix this error “the condition has length > 1 and only the first element will be used”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM