QFASA Workflow Example

Shelley Lang

2024-08-27

Load Package

library(QFASA)
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize

Modeling Inputs

Prior to starting make sure that:

Distance Measure

Choose from one of three distance measures:

  1. KL (Kullback-Leibler)
  2. AIT (Aitchison)
  3. CSD (Chi-Squared)
dist.meas=1

Fatty Acid Set

data(FAset)
fa.set = as.vector(unlist(FAset))

Matrix of Predator FA signatures

data(predatorFAs)
tombstone.info = predatorFAs[,1:4]
predator.matrix = predatorFAs[,5:(ncol(predatorFAs))]

# number of predator FA signatures this is used to create the matrix of CC values (see section 6 below)
npredators = nrow(predator.matrix)

Matrix of Prey FA signatures

#full file
data(preyFAs)

#extract prey FA only from data frame and subset them for the FA set designated above
prey.sub=(preyFAs[,4:(ncol(preyFAs))])[fa.set]

#renormalize over 1
prey.sub=prey.sub/apply(prey.sub,1,sum) 

#extract the modelling group names from the full file
group=as.vector(preyFAs$Species)

#add modelling group names to the subsetted and renormalized FAs
prey.matrix=cbind(group,prey.sub)

#create an average value for the FA signature for each designated modelling group
prey.matrix=MEANmeth(prey.matrix) 

Prey Lipid Content

#numbers are the column which identifies the modelling group, and the column which contains the lipid contents
FC = preyFAs[,c(2,3)] 
FC = as.vector(tapply(FC$lipid,FC$Species,mean,na.rm=TRUE))

Calibration Coefficients

data(CC)
cal.vec = CC[,2]
cal.mat = replicate(npredators, cal.vec)

Run QFASA

Q = p.QFASA(predator.matrix, prey.matrix, cal.mat, dist.meas, gamma=1, FC, start.val=rep(1,nrow(prey.matrix)), fa.set)

p.QFASA Output

The QFASA output is a list with 2 components:

Diet Estimates

This is a matrix of the diet estimate for each predator (by rows, in the same order as the input file) by the modelling groups (by column, in the same order as the prey.matrix file). The estimates are expressed as a proportion (they will sum to 1). In the code below the Diet Estimate matrix is extracted from the QFASA output and the modelling group identities and predator tombstone data (created above) are added to the matrix:

DietEst = Q$'Diet Estimates'

#estimates changed from proportions to percentages
DietEst = round(DietEst*100,digits=2)
DietEst = cbind(tombstone.info,DietEst)

Additional Measures

This is a list of lists where each list (one per predator) is itself a list of four outputs:

  • ModFAS: the value of the modelled FA. These are expressed as proportions (they will sum to 1).

  • DistCont: the contribution of each FA to the final minimized distance.

  • PropDistCont: the contribution of each FA to the final minimized distance as a proportion of the total.

  • MinDist: the final minimized distance in the code below the ‘ldply’ function from the plyr package is used to compile the lists within ‘Additional Measures’ into a data frame with one row per predator (in the same order as the input predator matrix) and the values for each of the 4 lists arranged into columns. The ‘ldply’ function automatically names the columns of the data frame with a concatenation of the originating list name and the FA name so that the 4 sets of outputs can be easily identified within the data frame.

Add.meas = ldply(Q$'Additional Measures', data.frame)

Note that the function “conf.meth” will return approximate simultaneous confidence intervals for diet.