[R-sig-eco] Error in optim function
Alexandre F. Souza
alexsouza.cb.ufrn.br at gmail.com
Wed Jan 20 01:35:04 CET 2016
Dear friends,
I am getting a persistent error message when trying to estimate
lotka-volterra competition coefficients from data on two populations of
paramecium using the optim function. Do anyone knows how could I solve
this? The error message is
Error in eval(substitute(expr), data, enclos = parent.frame()) :
numeric 'envir' arg not of length one
Debug
function (data, expr, ...)
eval(substitute(expr), data, enclos = parent.frame())
and the code is
library(deSolve)
LotVolt = function(t,n,parms) {
with(parms,{
dn = r*n*(1-LV%*%n)
return(list(dn))
})
}
LVmse = function(parms) {
out = as.data.frame(lsoda(n0, times, LotVolt, parms)) # run ode
mse = mean((out[,2:3]-nTrue)^2) # calculate mean squared error between
simulated and original data
return(mse) # return mean squared error
}
dados =
cbind(c(2,4,7,25,14,43,81,140,180,224,240,204,375,370),c(2,4,6,13,41,74,195,164,160,240,230,215,230,215))
nTrue = as.matrix(dados)
s = 2 #number of species
r = c(0.7816, 0.6283) #vector of species intrinsic growths
K = c(559.686, 202.4931) #vector of species carrying capacities
n0 = c(n=nTrue[1,])
alpha0 = diag(1,s,s) #alpha is matrix of species competition coefficients,
ones on diagonal
a12guess = 0.5
a21guess = 0.5
alpha0[2,1] = a12guess
alpha0[1,2] = a21guess
tf = dim(nTrue)[1] # time
times = 1:tf # vector of times to run over
LV0 = diag(1/K)%*%alpha0 #modified lotka-volterra matrix is species
competitions divided by carrying capacities
optimOut = optim(LV0, LVmse) # give optim initial guess and function
parms = optimOut$par # extract parameters
nSim = as.data.frame(lsoda(n0,times,logInt,parms)) # run ode to get
simulated population
Any assistence would help.
Many thanks in advance,
Alexandre
--
Dr. Alexandre F. Souza
Professor Adjunto III
Universidade Federal do Rio Grande do Norte
CB, Departamento de Ecologia
Campus Universitário - Lagoa Nova
59072-970 - Natal, RN - Brasil
lattes: lattes.cnpq.br/7844758818522706
http://www.docente.ufrn.br/alexsouza
[[alternative HTML version deleted]]
More information about the R-sig-ecology
mailing list