knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width=7, fig.height=8
)

The population diet (estimated in Example 03) is organised as an object of the class STOMdiet, which is basically a list of predator and prey related information. The attributes of a STOMdiet (the control object) include information on the compilation of data.

library(FishStomachs)
suppressMessages(library(dplyr))
suppressMessages(library(readr))

load(file=file.path(system.file( package = "FishStomachs"),"extdata","ex03d.Rdata"))
d

d[['PRED']]  # predator related information
d[['PREY']]  # prey related information

# show parts of the control attributes
print(attr(d,'control'),show=c('calc_diet','stratification'))

STOMdiet objects can be sub-setted and combined by the methods subset and c.

# make a subset of data
d1<-subset(d,pred_size=='0400-0499' & stratum_time=="1991-Q1")
d1

# another subset
d2<-subset(d, stratum_time=="1991-Q2")
d2

#combine subsets
c(d1,d2)

rm(d1,d2)

It is also possible to transform STOMdiet object into a plain data.frame, modify data and convert back again.

# convert into a data frame
df<-as.data.frame(d)
df

# add a variable 
df$cannibal<-as.character(df$pred_name)==as.character(df$prey_name)

#convert it back to a STOMdiet object with a new PREY variable
d2<-as_STOMdiet(df,new_prey_var = 'cannibal')

# "cannibal" s now included (in the PREY data set)
names(d2[['PREY']])

rm(d2)

There are various summary functions available for the STOMdiet object.

summary(d)
summary(d,level=2)

# make a subset of data
d1<-subset(d,pred_size=='0300-0399' & stratum_time=="1991-Q1")
d1
summary(d1,level=3)
summary(d1,level=4,drop.unused.levels=TRUE)

And plots of relative stomach contents in weight.

#default
plot(d)

# change in layout, shorter predator size on the x-axis ("from" value),
#   added number of stomachs and added title
plot(d,Ncol=1,cut_pred_size=c(1,4),addTitle = TRUE, addNstom=TRUE,refac_prey=TRUE)

It is possible to change the names of both predator and prey species.

# Change names into English names
d2<-from_to_species_diet(d,pred_from_to=c("species","short"),prey_from_to=c("species","short"),
          sp_info_file=file.path(system.file( package = "FishStomachs"),"extdata",'species_info.csv'))

plot(d2,Ncol=1,cut_pred_size=c(1,4),addTitle = TRUE, addNstom=TRUE)

Plots can also be done where the prey size class is shown. In this plot data from all quarters in 1991 are combined (weighted mean, by number of stomachs, of all quarters of 1991).

# detailed plot by predator size class at the top, 
#   prey size class on the x-axis and prey species shown at the right margin. 
#   The very small predators are excluded from the plot.

plotSize(subset(d2,!(pred_size %in% c("0100-0149","0150-0199"))), 
          cut_pred_size=c(1,3),addTitle=TRUE, byVar=c('year-quarter','year','quarter','none')[2])
# load data from example_03
 load(file=file.path(system.file( package = "FishStomachs"),"extdata","ex03half.Rdata"),verbose=TRUE)


 # a very complex way of getting a list of prey names!
 prey_names <-(read_csv(file.path(system.file( package = "FishStomachs"),"extdata","species_info.csv"),
                       col_types = cols())  %>% 
                filter(prey_sp)  %>% select(species) %>% unique())$species
 prey_names #input is just a vector of prey names


 s<-as.data.frame(s)

 s<-mutate(s,identified=prey_name %in% prey_names & 
             (substr(prey_size,1,4) != "9999")  & (substr(prey_size,6,10) != "9999")) 


 # number of records, with fully identified prey items 
 a<-xtabs(~ prey_name+identified,data=s);b<-rbind(a,all=apply(a,2,sum));cbind(b,all=apply(b,1,sum))

 # prey weight of records, with fully identified prey items 
 a<-xtabs(prey_w~ prey_name+identified,data=s);b<-rbind(a,all=apply(a,2,sum));
 round(cbind(b,all=apply(b,1,sum))/1000)

 # move not fully identified preys to "other"
 s<-mutate(s,prey_name=if_else(identified,prey_name,factor('other')),
        prey_size=if_else(identified,prey_size,factor('9999-9999')),
        prey_size_class=if_else(identified,prey_size_class,0L),
        prey_l_mean=if_else(identified,prey_l_mean,as.integer(NA)),
        prey_n=if_else(identified,prey_n,as.double(NA))
        )

 s<- as_STOMobs(s)



s<-edit_control(s,sel_preys=prey_names)


s<-edit_control(s,
        calc_sub_strata=list(
          # transform into relative weight before data are compiled
          relative_weight=FALSE,      
          # use number of stomach in the sample as weighting factor
          weighting_factor=expression(n_tot),
          # set file to NA when weighting_factor is given
          weighting_factor_file=NA
        ),
        calc_strata=list(
          relative_weight=FALSE,
          weighting_factor=expression(sqrt(mean_cpue)),
          weighting_factor_file=NA
        ),
        calc_total=list(
          relative_weight=FALSE,
          # set to NA when input is provided in a file
          weighting_factor=NA,
          # use data from a specified file (in those case, a file provided by the FishStomachs)
          weighting_factor_file=file.path(system.file( package = "FishStomachs"),"extdata","weighting_total.csv")
        )
)


d_simple<-calc_population_stom(s)
d_simple

# Change names into English names
d_simple<-from_to_species_diet(d_simple,pred_from_to=c("species","short"),prey_from_to=c("species","short"),
                sp_info_file=file.path(system.file( package = "FishStomachs"),"extdata",'species_info.csv'))

#Relative stomach contents
d_simple[['PREY']]<- d_simple[['PREY']] %>% dplyr::group_by(key) %>% 
   dplyr::mutate(prey_w=prey_w/sum(prey_w)) %>% dplyr::ungroup()
# table of differences
a<-xtabs(prey_w~prey_name+key,data=d_simple[['PREY']]);
# round(rbind(a,apply(a,2,sum)),3)

d1<-d2
#Relative stomach contents
d1[['PREY']]<- d1[['PREY']] %>% dplyr::group_by(key) %>% 
  dplyr::mutate(prey_w=prey_w/sum(prey_w)) %>% dplyr::ungroup()

b<-xtabs(prey_w~prey_name+key,data=d1[['PREY']]);
# round(rbind(b,apply(b,2,sum)),3)

# round((a-b)*1000)


plotdif(d1=d1,d2=d_simple,show_plot=TRUE,cut_pred_size=c(1,4),cut_prey_size=c(2,4),addTitle=TRUE,tAngle=90,
                  refac_prey=FALSE, relative=FALSE,maxDif=3,
                  byVar=c('year-quarter','year','quarter','none')[4])


plotdif(d1=d1,d2=d_simple,show_plot=TRUE,cut_pred_size=c(1,4),cut_prey_size=c(2,4),addTitle=TRUE,tAngle=90,
        refac_prey=FALSE, relative=TRUE,maxDif=3,
        byVar=c('year-quarter','year','quarter','none')[4])


MortenVinther/FishStomachs documentation built on Feb. 14, 2025, 7:33 a.m.