【问题标题】:Re-Writing user-defined function with While Loop用 While 循环重写用户定义的函数
【发布时间】: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] &lt; 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 循环替换sumwhich,因为我的数组是有序的时间并且可以变得很长。有什么建议吗?

按照建议,让我发布产生错误的数据:

event1&lt;-c(3580.794 3583.079 3583.714 3583.998 3584.116 3585.042 3586.264) seed.times1&lt;-seq(3580, 3590, by=0.001)

hintensity1&lt;-Intensity(c(0.1,5,17), seed.times1, event1)

Error in while (t[j] &lt; s[i]) { : missing value where TRUE/FALSE needed

【问题讨论】:

  • 发布一些抛出错误的示例数据。
  • 如果我在同一数据上使用whichsum 运行函数,我不会收到错误消息...

标签: r function while-loop


【解决方案1】:

如果您在引发错误时检查 t[j] 和 s[j] 的值,我怀疑您会发现两者之一是 NA。更具体地说,我认为是 t[j]。那是因为您允许索引 j 无限增长,因此在某些时候您将超过 t 向量中的元素数量。尝试在 where() 控件中包含一个附加条件。当 t 的所有元素都详尽无遗时,我不知道你想要发生什么,但这样的事情可能会起作用:

event1<-c(3580.794, 3583.079, 3583.714, 3583.998, 3584.116, 3585.042, 3586.264) 
seed.times1<-seq(3580, 3590, by=0.001)

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 (!is.na(t[j]) && t[j] < s[i])
     {
         values[i] <- values[i] + par2*exp(-par3*(s[i]-t[j]))
         j <- j+1
      }
    }   

 return(values)
}

hintensity1<-Intensity(c(0.1,5,17), seed.times1, event1)

【讨论】:

  • 很好,这似乎可以解决问题!您还建议我“在引发错误的地方检查 t[j] 和 s[j] 的值”,这是个好主意,但我该怎么做呢?
  • 一种方法是使用 print() 函数,可能在您调用 while... 循环之后,或者在您更新 j 索引之后的循环结束之前。像 print(t[j]);打印(s[i]);这样至少你会在出错之前看到正确的值(如果有错误)。我建议仅将其用于调试目的,因为如果您的循环运行更多次,它可能会填满您的屏幕。
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 2019-04-21
  • 1970-01-01
  • 2021-05-10
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多