gadget-framework/mfdb

stomach data import

Closed this issue · 10 comments

Hi all,

I want to import few prey species from stomach data with mfdb_stomach_presenceratio. How to properly specify prey length groups in that case? I tried:

prey_species = c('SPR','HER'),
prey_length = c(lls,llh))

where lls and llh are separate length aggregation for sprat and herring:

ls <- mfdb_interval("spr", c(10,110, 175))
names(lls) <- paste("spr", c(7,15), sep="")

It created a file with numerical values of length, which I manually edited to f.ex. spr7. When I tried to create a likelihood file with gadget_likelihood_component("stomachcontent"... , I got an error:

Data should group by prey_length to build prey aggregation file

Thank you in advance!

Nataliia

Quick answer: The error is probably because when editing the data.frame you got from mfdb_stomach_presenceratio, the attributes on the data.frame haven't been preserved. The attributes store information on the grouping, so the various aggregation files can also be generated---try running str() on the data.frame either side to see what's happening with them.

Better answer: The real challenge here is having per-species length aggregations. prey_length = c(lls,llh) isn't doing what you're hoping for, unfortunately. The concatenation is causing it to lose it's "interval-ness", and is now just looking for instances of those numbers. The following would work if the length ranges are distinct:-

> len_both <- mfdb_interval("all", c(5, 7, 10, 110, 175))
> names(len_both) <- c("her5", "her7", "spr10", "spr110", "spr175")
> len_both
  her5   her7  spr10 spr110 spr175 
     5      7     10    110    175 
attr(,"open_ended")
[1] FALSE
attr(,"class")
[1] "mfdb_interval"  "mfdb_aggregate"

...however, I suspect life isn't that easy and the length ranges for the 2 species will overlap. The easiest way around this is to do 2 mfdb_stomach_presenceratio calls, once for herring and again for sprat. You then need to combine the data.frames, something like this off the top of my head:-

stomach_sprat <- mfdb_stomach_presenceratio(...)[[1]]
stomach_herring <- mfdb_stomach_presenceratio(...)[[1]]
stomach_all <- rbind(stomach_sprat, stomach_herring)
attr(stomach_all, 'prey_length') <- c(attr(stomach_sprat, 'prey_length'), attr(stomach_herring, 'prey_length'))

If the above doesn't work how you expected, please let me know and I'll have a play around. Or feel free to add a code snippet for me to look at if it'd help.

For the last example, you probably need a similar line for prey_species too---in reality you need the intersection of all the attributes of same name as the data.frame columns, but I wasn't brave enough to work out the code for that off the top of my head.

Thank you a lot, Jamie! Merging with rbind and then copying attributes from separate files, did the trick. And then I just needed to write prey species manually in prey length aggregation file.

Are you setting prey_labels when using gadget_likelihood_component? You should be able to do that automatically. Although it's quite possible there's a case I haven't thought of.

I tried, but then in each length group were names of both prey species.

Yeah, that's the mistake I thought I made. Can you attach/copy-paste the file post-modifications, and I'll see if I can fix it.

I sent it to you by e-mail.

@nataliia-kulatska Okay, I've tidied up a bunch of loose ends now, so you should be able to make the stomach process a lot neater. Can you install the latest MFDB 4.x?

When combining data.frames, you need to also do:-

attr(aggdata_all, 'prey_species')
attr(aggdata_all, 'prey_species') <- c(attr(aggdata, 'prey_species'), attr(aggdata2, 'prey_species'))

And in place of prey_labels = c("sprat","herring") when making the likelihood component, you can do:

prey_labels = list("spr" = "sprat", "herr" = "herring")

Also the predator length aggregation file should be generated now.

Great, thank you a lot, Jamie!

There's now also a mfdb_concatenate_results function that saves you from having to mess about with rbind and fiddling with the attributes too.