# [R] An infinite recursion error please explain!

mousy0815 mousy0815 at gmail.com
Sat Jul 23 04:29:46 CEST 2011

```Probability <- function(N, f, w, b, y, t, q) {
#N is the number of lymph nodes
#f is the fraction of Dendritic cells (in the correct node) that have the
antigen
#w is time in terms of hours
#b is the starting position (somewhere in the node or somewhere in the gap
between nodes. It is a number between 1 and (x+t))
#y is the number of time steps it takes to traverse the gap (8hr/y)
#t is the number of time steps it takes to traverse a node. (24hours total
-- so 24hr/t)
#q is the length of the time step

m <- ceiling(w/q)
A <- 1/N
B <- 1-A
C <- 1-f
D <- (((m+b-1)%%(y+t))+1)

if (b<=t) {########starts inside node
if (m<=(t-b)){return(B + A*(C^m))} # start & end in first node
if (D<=t) { # we finish in a node
a <- (B + A*(C^(t-b))) #first node
b <- ((B + A*(C^t))^(floor((m+b)/(y+t))-1))  # intermediate nodes (if
any)
c <- (B + A*(C^D))  # last node
return(a*b*c)
} else {return(Probability(N, f, ((m*q)-q), b, y, t, q))} ## finish in a
gap
} else {###### starts outside node
if (m<=(y+t-b)) {return(1)} #also end in the gap
if (D<=t) { #end in a node
b <- ((B + A*(C^t))^(floor((m/(y+t)))))
c <- (B + (A*(C^D)))
return(b*c)
} else {return(Probability(N, f, ((m*q)-q), b, y, t, q))} #outside node
}
}

This works for most values of 'w' (time):

> Probability(100, 0.001, 1, 1, 20, 20, (10/60))
 0.9999401
> Probability(100, 0.001, 2, 1, 20, 20, (10/60))
 0.9998807
> Probability(100, 0.001, 3, 1, 20, 20, (10/60))
 0.9998215
> Probability(100, 0.001, 4, 1, 20, 20, (10/60))
Error: evaluation nested too deeply: infinite recursion /
options(expressions=)?

But once I get to w=4 I get an infinite recursion. I get that there is a
recursion in my function but I'm not sure why it wouldn't work. After a
certain point (D<=t) would be true.