0
votes

ntimes = 50
H =  numeric(ntimes) # Host
P = numeric(ntimes) # Parasitoid
H = .4; P = 2.0 # initial values for populations H and P
for (i in 2:ntimes)  {
  H[i+1] = (8.7*H[i]*exp^(-0.4*P[i]))/(1+(4.7*H[i]))
  P[i+1] = (5.5*H[i])*(1-exp^(-0.4*P[i]))
  }

Error in exp^(-0.4 * P[i]) : non-numeric argument to binary operator The model equation I am tryinng to create

Cannot figure out why I am getting this Error. This is a model adaptation from the Nicholson Bailey model. This is the initial code I used, Nicholson Bailey model.

ntimes = 25
H = numeric(ntimes) # Host
P = numeric(ntimes) # Parasitoid
H = 350; P = 150 # initial values for populations H and P
a = .004; c = 1; r = 1.75 # the parameter values


for (i in 2:ntimes) {
  H[i] = r * H[i-1] * exp(-a * P[i-1])
  P[i] = c * H[i-1] * (1 - exp(-a * P[i-1])) 
 }

par(mfrow = c(1,2)) # 1 x 2 graphics window
plot(1:ntimes, H, type = "n", las = 1,
     xlim = c(0,25), ylim = c(0,1100),
     ylab = "Number of Individuals",
      xlab = "Time",cex.lab = 1.5)
leg.txt = c("Host","Parasitoid")
legend("topright",leg.txt,lwd=2,lty = c(1,2))
lines(1:ntimes,H,lwd=2,lty=1)
lines(1:ntimes,P,lwd=2, lty=2)

plot(H, P, las = 2,
      xlim = c(0,800), ylim = c(0,1100),
      ylab = "Number of Parasitoids", xlab = "Number of Hosts",
      cex.lab = 1.5,type = "l",lwd = 2)
1
change exp^(-0.4*P[i])) to exp(-0.4*P[i])) exp is a function in r, not a variable. you have it right in the initial codejeremycg
You also probably want the for (i in 2:ntimes) to be for (i in 1:ntimes)jeremycg
Also don't do H =.4; P = 2.0 but H[1]=.4;P[1]=2.0 ; you are initializing the first elements (I presume).Bhas
Thank you. I have that part of the code functioning, but now I am trying to plot the data. I have the graph that it should create- which I am trying to recreate using my code. It creates a type of loop. Any suggestions?Michelle Presler

1 Answers

0
votes

This seems to work:

Correct minor typos in code:

ntimes = 50
H =  numeric(ntimes) # Host
P = numeric(ntimes) # Parasitoid
H[1] = .4; P[1] = 2.0 # initial values for populations H and P
for (i in 1:(ntimes-1))  {
  H[i+1] = (8.7*H[i]*exp(-0.4*P[i]))/(1+(4.7*H[i]))
  P[i+1] = (5.5*H[i])*(1-exp(-0.4*P[i]))
  }

Plot, removing old xlim/ylim values and switching to type="b", pch=16:

plot(H, P, las = 1,
     xlim=c(0,1), ylim=c(1,3),
     ylab = "Number of Parasitoids", xlab = "Number of Hosts",
      cex.lab = 1.5,type = "b", pch=16, lwd = 2)