Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to include other SR-relationships in FLBEIA besides the defined ones (e.g. 'bevholt') from FLCore #33

Open
BernhardKuehn opened this issue Mar 11, 2020 · 5 comments

Comments

@BernhardKuehn
Copy link

So far it seems only possible to use the predefined SRModels{FLCore} in FLBEIA, but I want to incorporate other stock-recruitment models as well.

I already managed to implement a toy-example, which uses a GAM with ssb and 2 environmental covariates in the FLSR-framework from FLCore, thanks to @iagomosqueira. See the issue: flr/FLCore#60

Digging a bit in the code of "FLSRsim" and "create.SRs.data" in FLBEIA, I have realised that FLBEIA does not directly use the FLSR-class provided by FLCore. Correct me if I am wrong, but it seems that only the parameters from the formula related to a specific model from FLCore (e.g. 'bevholt') are getting extracted:

line 65-66 in FLSRsim

 nmparams <- all.vars(get(x[["model"]], pos = 2)()[[2]])
 nmparams <- nmparams[!(nmparams %in% c("rec", "ssb"))]

I haven't found the part in the code where the actual SR-relationship is called, so I just guess these parameters are getting plugged into the formula provided with the SR-model (e.g. 'bevholt').

My idea would be to define my own function (e.g. 'mySR.relationship') and make it visible for the FLSRsim function to call. But here I encountered a problem with the environment-structure in R, since get(...,pos = 2) calls an environment, where my function is not defined. My questions would be:

  • How to make my own defined function visible for the call in line 65 in FLSRsim?

  • Where can I find the part of the code, where the SR-function is called? and

  • Is this the way to do it or do you see some problems in the way I want to include my own SR-relationship into FLBEIA?

I would be glad if you can help me with this issue.

@dorleta
Copy link
Contributor

dorleta commented Mar 11, 2020 via email

@BernhardKuehn
Copy link
Author

Hi Dorleta,
thanks for your quick response and your suggestions. With a bit of thought, I now managed to include my own SR-relationship in a quite "hacky" kind of way working both with create.SRs.data and FLSRsim.

here is what I did for anyone else, who wants to do so as well (not a fully reproducible example though):

  1. I created a function that takes ssb as input and returns a prediction based on a fitted GAM, as I already did in Option to include fitted models (GLM, GAM) into a FLSR-object  FLCore#60.
mySR.relationship <- function(ssb,a){
  # convert to vector
  new.data <- data.frame(ssb = as.vector(ssb))
  new.rec <- predict(gam.fit,newdata = new.data,type = "response")
  # convert back to FLQuant-obj
  new.rec <- FLQuant(as.vector(new.rec))
  return(new.rec)
}

FLBEIA requires explicit definition of the parameters related to the SR-model (e.g. "a" & "b" for Beverton-Holt), otherwise it throws an error. Since my GAM-fit does not really have any explicit parameters, I added some parameter "a" in "mySR.relationship", that does not have any effect on the prediction, just to make sure FLBEIA is satisfied. Quite a "dirty" programming way, but it seems to get me where I want.

  1. Then I wrote a function that is structured like the 'bevholt'-function in FLCore, with the second argument specifying a formula calling mySR.relationship(ssb,a).
gam.function <- function(){
  logl <- NA
  initial <- NA
  model <- as.formula(rec~mySR.relationship(ssb,a))
  return(list(logl = logl, model = model, initial = initial))
}
  1. To ensure that the function is found by the get(...,pos = 2) call in the FLSRsim call: nmparams <- all.vars(get(x[["model"]], pos = 2)()[[2]]) I copied the function to the corresponding environment with assign.
assign("gam.function",value = gam.function,pos = 2)

Now create.SRs.data runs without throwing an error and simulating with FLBEIA seems to work as well giving me the recruitment prediction based on 'mySR.relationship'. Next thing would be to check how to include a covariate, as this example was just based on ssb.

@dorleta
Copy link
Contributor

dorleta commented Mar 11, 2020 via email

@BernhardKuehn
Copy link
Author

Hi Dorleta,
yes, having the prediction model (in this case "gam.fit") loaded in the workspace seems to be enough for FLBEIA to access it.
Bernhard

@dorleta
Copy link
Contributor

dorleta commented Mar 11, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants