나는 seed.times에서 intensity 함수의 값을 계산하기로되어있는 다음 함수를 작성하고있다. 나는 R에서 실행할 때While 루프로 사용자 정의 함수 다시 쓰기
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)
}
그러나, 나는 다음과 같은 오류가 발생합니다 : Error in while (t[j] < s[i]) { : missing value where TRUE/FALSE needed
은. 무슨 뜻이에요? 위의 기능은 실제로있는 내 배열은 시간을 정렬 얻을 수 있기 때문에 while
루프로 sum
및 which
을 대체 할
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)
로 작성 내 원래의 기능} 개선하는 나의 시도 꽤 오래. 어떤 제안? 당신이 t [J]와 S의 값을 검사하는 경우
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'을 사용하여 함수를 실행하면 오류가 발생하지 않습니다 ... –