-
Notifications
You must be signed in to change notification settings - Fork 13
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
Comments
Hi Bernhard,
In principle FLSRsim was designed to work wit the SR models defined in FLCore or models defined in similar way. Last week talking with Mark Taylor I realise of the limitation of this approach when working with SR models coming from other modelling approaches. I’ve looked at the issue in FLCore but have not time to go into the details, so not sure if you will be able to implement it within FLBEIA.
I’ll answer your questions and hopefully they will help you to solve the problem,
* How to make my own defined function visible for the call in line 65 in FLSRsim?
FLSRsim is only used to store data, it does not call any new function. In this class what you need to do is to add the name of the model to the slot @model
myFLSRsimobj@model <- ‘foo’
for example.
* Where can I find the part of the code, where the SR-function is called?
The function is called within the SRsim function line 56:
res <- c(eval(model, datam)) # valid for 1 year, 1 season and 'N' iterations
* 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?
The way to do it, is the one you are following, but I don’t know how you can pass the gam.fit object to the predict method. I suppose that this is why you are trying to use get(, pos = 2) to access the object from the global environment or something like this? If this would be possible I suppose it can be implemented. When talking with Mark last week, the problem I saw was how to pass a fitted object from a random.forest algorithm to SRsim, as current implementation of FLBEIA does not include this option.
Apart, there is a problem with new SR models when create.SRs.data, function is used, but I assume you are not using it, are you?
We are happy to help, so tell as if we can assist you on something else,
Dorleta
De: BernhardKuehn <[email protected]>
Enviado el: miércoles, 11 de marzo de 2020 10:26
Para: flr/FLBEIA <[email protected]>
CC: Subscribed <[email protected]>
Asunto: [flr/FLBEIA] How to include other SR-relationships in FLBEIA besides the defined ones (e.g. 'bevholt') from FLCore (#33)
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<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fiagomosqueira&data=02%7C01%7C%7Cbee5712824fc4a2470cc08d7c59e46b8%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195155897128861&sdata=GjqKUuVVtinE4gyvJ4NvguxMLYN6mdLKHb5ut0MxZcg%3D&reserved=0>. See the issue: flr/FLCore#60<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fflr%2FFLCore%2Fissues%2F60&data=02%7C01%7C%7Cbee5712824fc4a2470cc08d7c59e46b8%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195155897128861&sdata=nUhFtj2oG1OPwRPhQBnwp2vANXD8yhpw0kF72suQ5zI%3D&reserved=0>
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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fflr%2FFLBEIA%2Fissues%2F33%3Femail_source%3Dnotifications%26email_token%3DABGP2MWPQOMDI6EH2K46UWDRG5KMFA5CNFSM4LFRIY3KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IUEVNMQ&data=02%7C01%7C%7Cbee5712824fc4a2470cc08d7c59e46b8%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195155897138850&sdata=HoeCLzIzsJ44GXwKzeuweW4%2Fh9aRM67XyfemqTGTJi0%3D&reserved=0>, or unsubscribe<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABGP2MWQFB3AB57VWXGT6JDRG5KMFANCNFSM4LFRIY3A&data=02%7C01%7C%7Cbee5712824fc4a2470cc08d7c59e46b8%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195155897138850&sdata=5f7kJIQJgTlQDD7PURLip%2BUfKCX4le0emawwj75NPt0%3D&reserved=0>.
|
Hi Dorleta, here is what I did for anyone else, who wants to do so as well (not a fully reproducible example though):
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.
Now |
Hi Bernhard,
Great to know that you have manage to put it working, but…
how do you pass the gam.fit element? Is it enough to have it in the working space?
Dorleta
De: BernhardKuehn <[email protected]>
Enviado el: miércoles, 11 de marzo de 2020 14:01
Para: flr/FLBEIA <[email protected]>
CC: Dorleta Garcia <[email protected]>; Comment <[email protected]>
Asunto: Re: [flr/FLBEIA] How to include other SR-relationships in FLBEIA besides the defined ones (e.g. 'bevholt') from FLCore (#33)
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 flr/FLCore#60<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fflr%2FFLCore%2Fissues%2F60&data=02%7C01%7C%7C4f4d3ed5819c421272a008d7c5bc2f45%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195284353506601&sdata=k1KxOhXooGR21XNpxqMs4gM0G83%2FFwlNzOxCkKSlViE%3D&reserved=0>.
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fflr%2FFLBEIA%2Fissues%2F33%23issuecomment-597617575&data=02%7C01%7C%7C4f4d3ed5819c421272a008d7c5bc2f45%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195284353506601&sdata=6%2FwzbckZD7cmwNVdnBt8lWkexCJdKpGkkTZkTU41u0E%3D&reserved=0>, or unsubscribe<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABGP2MWGJO2QY4CYKV7CM3TRG6DO7ANCNFSM4LFRIY3A&data=02%7C01%7C%7C4f4d3ed5819c421272a008d7c5bc2f45%7C6219f1193e794e7facdea5750808cd9b%7C0%7C0%7C637195284353516593&sdata=Zaedi6%2F0W5XKvVhZ7QwAkWnjFH95uJCZZtqvk%2FCV2Ns%3D&reserved=0>.
|
Hi Dorleta, |
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
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.
The text was updated successfully, but these errors were encountered: