I'm looking to set up a population dynamics model where each parameter value corresponds to the temperature of that day. e.g.
Simple model
library(deSolve)
set.seed(1)
pars <- c(alpha = 1, beta = 0.2, delta = 0.5, gamma = 0.2)
lv_model <- function(pars, times = seq(0, 50, by = 1)) {
# initial state
state <- c(x = 1, y = 2)
# derivative
deriv <- function(t, state, pars) {
with(as.list(c(state, pars)), {
d_x <- alpha * x - beta * x * y
d_y <- delta * beta * x * y - gamma * y
return(list(c(x = d_x, y = d_y)))
})
}
# solve
ode(y = state, times = times, func = deriv, parms = pars)
}
lv_results <- lv_model(pars = pars, times = seq(0, 50, by = 1))
I now want to use a sequence of daily temperatures
DailyTemperature<-floor(runif(50,0,40))
and make the parameter values functions of temperatures
TraitTemperature<-seq(1,40,1)
#trait responses to temperature
alpha<- abs(rnorm(40,mean = 0.5,sd=1))
beta<- abs(rnorm(40,mean = 0.2,sd=0.5))
delta<-abs(rnorm(40,mean=1,sd=2))
gamma<- seq(0.025,1,0.025)
parameters<-as.data.frame(cbind(TraitTemperature,alpha,beta,delta,gamma))
So that for each time step iterated over, it looks at the daily temperature and then finds the corresponding temperature values in the parameter data frame.
Looking back through the archives i've seen if/else
statements used when wanting to alter single parameters at particular time steps and the use of forcing functions but I don't think they apply here.
I hope this makes sense, I'm interesting in ideas on how to make it work. So far i've also attempted using a for loop
to iterate through the daily temperature list and then the match
function to identify values but this didn't tap into the daily time steps.