【发布时间】:2014-11-28 21:39:54
【问题描述】:
我正在编写以下函数,该函数应该在种子时间计算强度函数的值。
Intensity=function(params, eval.times, event.times) {
# This function computes the value of the intensity function.
# It takes as seed a vector of values/times at which to compute the
# the value of the function and a vector with the occurrence times
# of the events.
# Input: eval.times, event.times and values of parameters
# Output: values of intensity function
s<-sort(eval.times)
t<-sort(event.times)
par1<-params[1]
par2<-params[2]
par3<-params[3]
values <- rep(par1,length(s))
for (i in 1:length(values)) {
j<-1
while (t[j] < s[i])
{
values[i] <- values[i] + par2*exp(-par3*(s[i]-t[j]))
j <- j+1
}
}
return(values)
}
但是,当我在 R 中运行它时,出现以下错误:Error in while (t[j] < s[i]) { : missing value where TRUE/FALSE needed。这是什么意思?上面的函数其实是我对原函数的改进,写成
Intensity=function(params, eval.times, event.times) {
# This function computes the value of the intensity function.
# It takes as seed a vector of values/times at which to compute the
# the value of the function and a vector with the occurence times
# of the events.
# Input: eval.times, event.times and values of parameters
# Output: values of intensity function
s<-sort(eval.times)
t<-sort(event.times)
par1<-params[1]
par2<-params[2]
par3<-params[3]
values<-foreach(i=seq_along(s), .combine=c) %do% {par1+sum(par2*exp(-par3*(s[i]-t[which(t<s[i])])))}
return(values)
}
我想用while 循环替换sum 和which,因为我的数组是有序的时间并且可以变得很长。有什么建议吗?
按照建议,让我发布产生错误的数据:
event1<-c(3580.794 3583.079 3583.714 3583.998 3584.116 3585.042 3586.264)
seed.times1<-seq(3580, 3590, by=0.001)
hintensity1<-Intensity(c(0.1,5,17), seed.times1, event1)
Error in while (t[j] < s[i]) { : missing value where TRUE/FALSE needed
【问题讨论】:
-
发布一些抛出错误的示例数据。
-
如果我在同一数据上使用
which和sum运行函数,我不会收到错误消息...
标签: r function while-loop