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

bug in SAM2FLR #8

Open
hgerritsen opened this issue Feb 5, 2020 · 2 comments
Open

bug in SAM2FLR #8

hgerritsen opened this issue Feb 5, 2020 · 2 comments

Comments

@hgerritsen
Copy link

hgerritsen commented Feb 5, 2020

I have a stock that has no catch numbers at age 0 but i do have a tuning index at age 0. I cannot have a stock object that has fewer ages than the index, so need to include age 0 in the stock object and set the catch.n to NA.
You can then fit the SAM model but when SAM2FLR tries to populate the harvest slot it breaks.
Below is some example code to illustrate the problem:

library(FLSAM)
data(NSH)
data(NSH.sam)

what if you have no catch at age 0 but you do have a tuning index:

you dont want to estimate F for age 0

NSH.ctrl@states[1,] <- c(-1,0,1,2,3,4,5,6,6)

the model fits but when it tries to convert it into an FLSAM object it fails

sam <- FLSAM(NSH,NSH.tun,NSH.ctrl)

this bit does work

sam <- FLSAM(NSH,NSH.tun,NSH.ctrl,return.fit=T)

this doesn't

SAM2FLR(sam,NSH.ctrl)

the problem is here in the SAM2FLR function where it tries to pupulate the harvest slot
it uses the ctrl@states to get the f at each age, but if your state is -1 for age 0
and age0 does exist in your FLstock object, it doesnt put NA's in for that first age

    res@harvest <- FLQuant(
        exp(fit$pl$logF[ctrl@states[grep("catch", rownames(ctrl@states)), ] + 1, ]),
        dimnames = list(age = ctrl@range["min"]:ctrl@range["max"], 
        year = ctrl@range["minyear"]:ctrl@range["maxyear"], 
        unit = "unique", season = "all", area = "unique", 
        iter = 1))

the stockassessment package has some handy functions the do the right thing, e.g fatable()
could be used like below

    res@harvest <- FLQuant(t(stockassessment::faytable(fit)), 
        dimnames = list(age = ctrl@range["min"]:ctrl@range["max"], 
        year = ctrl@range["minyear"]:ctrl@range["maxyear"], 
        unit = "unique", season = "all", area = "unique", 
        iter = 1))

Similarly, the stock.n slot could be populated using ntable()

Finally, SAM2FLR does not update the catch.n and catch with the fitted values, is this by design? I think it would be nice to get those out as well

@nielshintzen
Copy link
Contributor

Good suggestion to use the newly available faytable and ntable functions.
However, conceptually I'm less sure about the hack you are suggesting (of course, technically it can be solved, but if we should is a different story).
I don't have a solution for it, so I pushed the technical solution.

Wrt catch and catch.n, it is by design and modelled catch can be obtained from catch(sam). However, catch.n was not a function yet and this has been added so you can call catch.n(sam)

@hgerritsen
Copy link
Author

Thanks, Niels.

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