Debug location is approximate because source is not available

Infected <- c(1,3,4,7,7,7,7,9,31,45,66,73,84,89,99,117,190,217,319,340,368,399,439,466,498,590,649,694,767,824,886,966,1156)

SIR <- function(time, state, parameters) {
par <- as.list(c(state, parameters))
with(par, {
dS <- -beta/N * I * S
dI <- beta/N * I * S - gamma * I
dR <- gamma * I
list(c(dS, dI, dR))
})
}

library(deSolve)
init <- c(S = N-Infected[1], I = Infected[1], R = 0)
RSS <- function(parameters) {
names(parameters) <- c("beta", "gamma")
out <- ode(y = init, times = Day, func = SIR, parms = parameters)
fit <- out[ , 3]
sum((Infected - fit)^2)
}

Opt <- optim(c(0.5, 0.5), RSS, method = "L-BFGS-B", lower = c(0, 0), upper = c(1, 1))

Opt_par <- setNames(Opt$par, c("beta", "gamma"))
Opt_par

t <- 1:190 # time in days

fit <- data.frame(ode(y = init, times = t, func = SIR, parms = Opt_par))

  1. When I run the code the following Warning message appears " debug location is approximate because source is not available"

  2. "Error in setNames(Opt$par, c("beta", "gamma")) : object 'Opt' not found" also appears.

Does anyone know how to solve this problem?

this is an incomplete code that can't be run.
object N was not declared

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Even after I declare N the same error messages appear.

ok, but the point is that we can't run it and track the error 'on your behalf'.
so when you submit each parseable phrase one at a time, which one throws the error message?

It is challenging my understanding because it seems that the optim command is warning you only that it cant tell you where its erroring but does not give you an explicit error message or warning beyond that its not debuggable ?
does Opt contain any value after that line ?

(there generally isnt a point running code beyond a point of error in your code, so I wouldnt try setNames on an object that wasnt constructed properly. errors should be addressed in strict order)

you can call
debug(optim)
in the console before you run the optim() line, that will let you step through the function in a browser. All though I expect it will take some patience and expertise to interpret.

When I run this line: "Opt <- optim(c(0.5, 0.5), RSS, method = "L-BFGS-B", lower = c(0, 0), upper = c(1, 1)) "

the following warning message appears "debug location is approximate because source is not available".

And then when i run this line "Opt_par <- setNames(Opt$par, c("beta", "gamma"))"
the following "Error in setNames(Opt$par, c("beta", "gamma")) : object 'Opt' not found" also appears.