Hi,
I'm trying to convert a spatial loop in a disease model to a nimblefunction as the compilation takes an increasing length of time as the number of sites in the model grows.
The original loop:
for (k in 1:J){
IN.tmp_Pf[i,j,k]<-q*(RC_Pf[i,k]*exp(-gamma*D[j,k]))
w[i,j,k]<-exp(-gamma*D[j,k])
}
#cant have empty indices, need to make explicit
IN_Pf[i,j]<-sum(IN.tmp_Pf[i,j,1:J])/sum(w[i,j,1:J])
The function I made:
betacalc2 <- nimbleFunction(
run = function(w = double(1),adj=integer(1),weights=double(1),num=integer(1),wstart=integer(0),wend=integer(0),wi=integer(0), gamma = double(0),RC=double(1),q=double(0)) {
returnType(double(0))
result=0.0
for(ji in wstart:wend){
j=adj[ji]
result=result+(q*(RC[j]*exp(-gamma*weights[ji])))
}
result=result/w[wi] #sum(result)/ sum(w[j,neighbors])
result=log(result) # beta0_Pf/Pv
return(result)
})
I then call it as:
beta0_Pf[i,j]<- betacalc2(w[1:J],adj[1:L],weights[1:L],num[1:J],wstart[j],wend[j],j,gamma,RC_Pf[i,1:J],q)
However, this fails at compilation.
I've attached the error log. If anyone has suggestions, I'd greatly appreciate it. I can add the full model code if that would help.