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))[1] 0.9999401> Probability(100, 0.001, 2, 1, 20, 20, (10/60))[1] 0.9998807> Probability(100, 0.001, 3, 1, 20, 20, (10/60))[1] 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. Any help, please? -- View this message in context: http://r.789695.n4.nabble.com/An-infinite-recursion-error-please-explain-tp3688260p3688260.html Sent from the R help mailing list archive at Nabble.com.
Not sure, but I played a little with the progression of w and m, and it appears that it doesn't take long for their values to converge (i.e. they don't change any more). Once that happens, D is not going to change. Further, unless I'm misreading the if {...} indents, you appear to be checking for D<=t at a point where it can't happen, i.e. after your comment "starts outside node." Please correct me if I'm wrong about that. Carl <quote> From: mousy0815 <mousy0815_at_gmail.com> Date: Fri, 22 Jul 2011 19:29:46 -0700 (PDT) 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)) [1] 0.9999401 > Probability(100, 0.001, 2, 1, 20, 20, (10/60)) [1] 0.9998807 > Probability(100, 0.001, 3, 1, 20, 20, (10/60)) [1] 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. Any help, please? -- ----- Sent from my Cray XK6
On 23.07.2011 04:29, mousy0815 wrote:> 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)) > [1] 0.9999401 >> Probability(100, 0.001, 2, 1, 20, 20, (10/60)) > [1] 0.9998807 >> Probability(100, 0.001, 3, 1, 20, 20, (10/60)) > [1] 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.Maybe that certain point is nested rather deeply in your recursion (if it actually exists, which I have not checked). If you change options(expressions) to allow for a deeper recursion you end in a protection stack overflow. Therefore it is obviously advisable to use an iterative approach here. Best, Uwe Ligges> Any help, please? > > -- > View this message in context: http://r.789695.n4.nabble.com/An-infinite-recursion-error-please-explain-tp3688260p3688260.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.