2014-10-03 2 views
0

나는 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 루프로 sumwhich을 대체 할

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

+0

오류를 던지는 몇 가지 예제 데이터를 게시하십시오. –

+0

동일한 데이터에서'which'와'sum'을 사용하여 함수를 실행하면 오류가 발생하지 않습니다 ... –

답변

1

:

제안으로

, 내가 오류를 생성하는 데이터를 게시 할 [j] 오류가 발생하는 지점에서 두 가지 중 하나가 NA가 아닌 것을 알게 될 것입니다. 더 구체적으로, 나는 그것이 t [j]라고 생각한다. 그리고 그것은 인덱스 j가 경계없이 커지도록 허용하기 때문입니다. 그래서 어떤 시점에서 여러분은 t 벡터의 요소 수를 초과 할 것입니다. where() 컨트롤에 추가 조건을 포함 시키십시오. 나는 모든 요소가 철저 할 때 어떤 일이 일어나길 원하는지 모르지만, 이와 같은 것이 효과가있을 수 있습니다 :

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) 
+0

트릭을 수행하는 것 같습니다! 또한 "오류가 발생하는 지점에서 t [j] 및 s [j]의 값을 검사"하는 것이 좋습니다. 이것이 좋은 아이디어이지만 어떻게해야합니까? –

+1

한 가지 방법은 print ... 함수를 사용하는 것입니다. 예를 들어 while ... 루프를 호출 한 직후 또는 j 인덱스를 업데이트 한 후 루프의 끝 직전에 사용하는 것이 좋습니다. print (t [j])와 같은 것; print (s [i]); 그런 식으로 적어도 오류가 발생하기 전에 값이 무엇인지 알 수 있습니다 (오류가있는 경우). 루프를 더 많이 실행하면 화면이 채워질 수 있기 때문에 디버깅 용으로 만 사용하는 것이 좋습니다. –

관련 문제