user6767735 user6767735 - 1 month ago 5x
R Question

Error message with objects in 'lsoda' in R

Very new user here. I am trying to use lsoda to solve differential equations stratified into two layers (as denoted by the

for(s in 1:2)

When running this full code, I keep getting the error message

object 'N' not found

no matter where or how I try to define

Can anyone help spot the error or advise on what I'm doing wrong? Thanks in advance.

R code:


Dyn <- function(t, var,par) {

with(as.list(c(par, var)), {

for(s in 1:2){

dX[s] <- mu*N[s] - sigma*X[s] - (c[s]*beta*(InD[s] +ID[s]+ IdT[s])/N[s])*X[s] - mu*X[s]

dXint[s] <- sigma*X[s] - (1-omega)*(c[s]*beta*(InD[s] +ID[s]+ IdT[s])/N[s])*Xint[s] - mu*Xprep[s]

dInD[s] <- (c[s]*beta*(InD[s] +ID[s]+ IdT[s])/N[s])*X[s] - psi*InD[s]- mu*InD[s]

dID[s] <- (1-omega)*(c[s]*beta*(InD[s] +ID[s]+ IdT[s]) /N[s])*Xint[s]+ psi*InD[s]- mu*ID[s]

N[s] <- X[s]+Xint[s]+InD[s]+ID[s]

diffs <- c(dX[s], dXint[s], dInD[s], dID[s], N[s])}



#Defining parameter and initial values

par <- c(mu=0.033, sigma=0.29, beta=0.40, c=c(2, 30), Ctot=1773600, N=c(332550, 36950), psi=0.022, omega=0.44)

init <- c(X=c(332550,36950), Xint=c(0,0), InD=c(1,1), ID=c(0,0))

t <- seq(0, 30, by=0.1)

#Numerical solution#

Hom.sol <- lsoda(init, t, Dyn,par)


I think you are mixing up parameters and variables. N seems to be defined as a parameter par with dimension 2. However, in your model definition you are updating N with dimension 1.