Xinting WANG
Xinting WANG

Reputation: 1985

Difference equations in R

Below is a parasite growth model:

Ni(a,t) represents the expected number of parasites of age a at time t

Ni(a, t) represents the expected number parasites of age a at time t, and ki(a, t) represents the killing effect, while PMF the multiplication factor. This is a discrete model as a equals 1, 2, 3.....48. Can anybody tell me how to implement this equation in R using difference equations? Many thanks for your support.

Upvotes: 0

Views: 1366

Answers (2)

N. McA.
N. McA.

Reputation: 4946

This is the best I could do with the information you provided. Point me to the rest of it and I might be able to get it to actually work, as stands I think it'll recur infinitely.

 Ki <- function(a, t){ ## You need to actually define this properly
      return(1)
    }

    Ni <- function(a, t, PMF){
      if ((a %% 1 != 0)) stop("Only Takes Integer values of a")
      if ((t %% 1 != 0)) stop("Only Takes Integer values of t")

      if (a == 1){
        x = Ni(48, t-1, PMF)
        y = exp(-Ki(48,t-1))
        result = PMF * x * y
        return(result)
      }

      if (a > 1){
        x = Ni(a-1, t-1, PMF)
        y = exp(-Ki(a-1,t-1))
        result = x * y
        return(result)
      }
    }

Upvotes: 2

Spacedman
Spacedman

Reputation: 94182

You don't have a set of initial conditions. Once you've got some initial 48 values for N(a=1..48,t=1) you compute N(a=1,t=2) from the second equation, and then compute N(a=2..48,t=2) from the first equation. Repeat for t=3 and so on.

What you have is a recurrence relation, not a differential equation. You step through a recurrence relation as I've just explained.

It might be possible to convert this to a system of differential equations by looking at N(t)-N(t-1)/dt and solving but that's a maths job not a programming job.

Upvotes: 0

Related Questions