delta_t <- 0.02 # months per step
time <- seq(0, 24, by = delta_t)
rabbit_birth_rate <- 0.8
fox_effect_on_rabbits <- 0.02
rabbit_effect_on_foxes <- 0.005
fox_death_rate <- 0.6
# Create and initialize the rabbit and fox vectors.
# TODO: 90 rabbits and 12 foxes
for (i in 2:length(time)) {
# Read both populations from position i - 1.
# Calculate rabbit births, rabbit losses, fox gains, and fox deaths.
# Update both populations using the old values.
# TODO
}
# Put time and both populations in a data frame.
# TODOQuestion and assumptions
What question will the two scenarios answer? Briefly explain the four flows in your model and state two simplifying assumptions.
Base-case simulation
Complete this code using the Module 4.2 pattern. Remove #| eval: false from this chunk when your code is ready to run.
First-step check
Show the first rabbit and fox update by hand. Then display the first two rows from your code. Do they agree?
# TODO: display the first two rows.Base-case graph and result
Graph both populations against time. Describe their behavior and report the largest rabbit population in this 24-month simulation and the time it occurs.
# TODO: plot rabbits, add the fox line, and add a legend.
# TODO: use which.max() to locate the largest rabbit value.Experiment: start with 18 foxes
Prediction: Will the largest rabbit population in the same 24-month window be higher or lower? Explain your reasoning before running the new simulation.
Copy the original loop, change only the initial fox population, and store the second set of results with different variable names. Remove #| eval: false when the code is ready.
# TODO: create and initialize two new population vectors.
# TODO: duplicate and adapt the loop for 18 initial foxes.
# TODO: graph both rabbit trajectories on one set of axes.
# TODO: report the largest rabbit population in each scenario.What changed in the two trajectories? Did your prediction hold? Do the curves tell a more detailed story than their maxima alone?
Conclusion
Answer the manager’s question for the two-year simulation. Explain one limitation that matters for a real wildlife decision.