mhunter1 / dynr

Dynamic Modeling in R
5 stars 6 forks source link

How to set options in dynr.model #278

Closed chriscmr closed 3 months ago

chriscmr commented 3 months ago

I'm trying to set the options for the optimization algorithm as explained in the documentation like this:

opts <- list(
  xtol_rel=1e-7, stopval=-9999, ftol_rel=1e-10, 
        ftol_abs=-1, maxeval=as.integer(10000), maxtime=-1
)
model_shk <- dynr.model(
  dynamics = dynm_shk,
  measurement = meas_shk,
  noise = nois_shk,
  initial = init_shk,
  data = data_shk,
  outfile = paste0("tempk.c"),
    options = opts # Incorporate options here
)

But I'm getting the following error

Error in FUN(X[[i]], ...): cannot get a slot ("paramnames") from an object of type "list"
Traceback:

1. dynr.model(dynamics = dynm_shk, measurement = meas_shk, noise = nois_shk, 
 .     initial = init_shk, data = data_shk, outfile = paste0("tempk.c"), 
 .     options = opts)
2. unlist(sapply(inputs, slot, name = "paramnames"))
3. sapply(inputs, slot, name = "paramnames")
4. lapply(X = X, FUN = FUN, ...)
5. FUN(X[[i]], ...)

Could you please help me to solve this?

chriscmr commented 3 months ago

Putting the solution here in case someone encounters this issue again:

Set options directly with the @ setters for S4 objects, and see them with the $ getters for S4 objects. For example:

# Create model
model <- dynr.model(dynamics=dynamics, measurement=meas, noise=ecov, initial=initial, data=data)
# Set things
opt <- model$options
opt$maxeval <- 1000
model@options <- opt # Now the model has maxeval option set to 1000 instead of 500