我正在尝试获取一种物种B与另一种物种T的相互作用强度(a)。我对建模非常陌生,并且不太了解错误,因此详细的解释确实有帮助!
我的数据由变量day,densityT和densityB组成:
day = c(0,1,2,5,7,9,12,14,16,19,21,23,26,28,30,33,35)
密度T = c(10,11048,6550,10140,1106,1184,381,241,282,51,85,237,83,53,186,248,132)
密度B = c(0,0,0,0,15,120,579,568,105,511,224,632,275,107,6,0,0)
library(deSolve)
library(bbmle)
library(plotrix)
library(stats4)
r = 0.992 # Tetra parameter extracted from other code
K = 6471 # Tetra parameter extracted from other code
# density for Tetra (N1) and Bleph (N2)
N1 = densityT
N2 = densityB
parameters <- c(a=0.1) # initial alpha parameter guess
state <- c(N1 = densityT[1]) # initial condition
t <- day
y.obs <- densityT
logistic <- function(times, state, parms) {
with(as.list(c(state,parms)),{
dNdt = r*N1*(1-(N1/K))-(a*N1*N2)
list(c(dNdt))
})
}
shootfun <- function(a){
out <- ode(y = state, times = t, func = logistic, parms = parameters, maxsteps = 100000)
y.pred <- out[,2]
sigma <- sqrt(sum((y.obs-y.pred)^2)/length(y.obs))
nloglike <- -sum(dnorm(y.obs, mean = y.pred, sd = sigma,log = TRUE))
return(nloglike)
}
shoot <- mle2(shootfun,start=list(a = parameters[1]),
method="Nelder-Mead",
control = list(maxit = 100000, reltol = 1e-8, abstol = 1e-8))
summary(shoot)
**Error in checkFunc(Func2, times, y, rho) :
The number of derivatives returned by func() (18) must equal the length of the initial conditions vector (1)**