Playing with data

There is a very interesting dataset available on Mammalian Trophic Levels and Body Sizes that is available on figshare (click the link to go to the data). I would also link to the paper the data is from, Tucker and Rodgers (no date), but I cannot seem to find it (the title and authors are given on the figshare site). Given that I just wrote about a paper that discussed body size, ratios, and trophic levels, I thought it would be a good time to mess around with these data.

troph <- read.csv(paste(filepath, "TrophicLevel_Appendix1.csv", sep = ""))

The dataset is 211 rows by 12 columns (dim(troph)). The first column gives us the name of the taxa, in what I think is a fairly simply formatted and consistent style (Genus_species). For each taxon we are given the trophic level, mass, prey mass, and predator-prey mass ratio. We are also given information about their diet choices (carnivore/omnivore), what kind of prey they prefer (e.g., fish, zooplankton, benthic inverts. mammals, etc.), and their habitat. The last three rows specifies source information for the diet and mass information.

For my purposes I did need to clean the data up a bit as in some parts a “-” was used as the null character instead of the preferable “NA”. This means that when you use read.csv R will think that all of the data are factors.

I simply changed it to numeric and pulled out the variables I wanted to use into a new dataframe:

trophlev <- as.numeric(troph$Trophic.level)
predmass <- as.numeric(troph$
preymass <- as.numeric(as.character(troph$
ppratio <- as.numeric(as.character(troph$Predator.Prey.ratio..log10.))

troph2 <- data.frame(Diet = troph$Diet, Hab = troph$Environment, TL = trophlev,
Pred = predmass, Prey = preymass, Ratio = ppratio)

Now, if you recall from the last paper review, Riede et al had three major findings:

  1. Predator mass and prey mass were positively correlated

  2. Predator mass increases with increasing trophic level

  3. Predator-prey mass ratio declines with increasing trophic level

I want to use this publicly available and easily accessible dataset to test their findings. Note that this dataset is much narrower in scope than theirs, which spanned several taxa type including invertebrates and ectotherms, while this dataset is focused solely on mammals. To analyze the data I am going to use two packages, lmodel2 for standard major axis regression, and ggplot2 for visualization.


So, given the data, how are predator and prey mass related?

g1 <- ggplot(troph2, aes(x = Pred, y = Prey)) + geom_point() + geom_smooth(method = "lm")
g1 + xlab("Log10 Predator Mass") + ylab("log10 Prey Mass")


There seems to be little to no relationship between the two.

There seems to be little to no relationship between the two.

With the linear model based trendline in there, it seems as if prey and predator mass are negatively related if anything, rather than the positive relationship found by Riede et al, and definitely not the same as the greater than unity relationship. In fact we can do some quick stats to double check.

In their paper, Riede et al. used model II regression, which for those who have long been away from (or yet to get to stats) means that we expect some error in both the x and y variables. Alternatively you can think of it as neither variable being set by the researcher (no independent variable). Ordinary least squares (OLS, or model I) regression (what is often meant when one says “regression”) assumes no error in the x variable. Anyway, we can use the package lmodel2 to do model II regression:

lmodel2(Prey~Pred, data = troph2)

Giving the output:


I haven’t gotten into too much detail about the output, but in this case what we are interested in is the Regression Results and Confidence Intervals for method 3, and mostly the slope parameter. Here we see that there is a slope of -1.28, with a 95%CI between -1.55 and -1.06. Far from the slope of 1 we were expecting.

Although, in their paper they split the data up by habitat type. We can do the same with the Tucker and Rodgers data.

g2 <- ggplot(troph2, aes(x = Pred, y = Prey)) + geom_point() + geom_smooth(method = "lm")
g2 + facet_grid(.~Hab) + xlab("Log10 Predator Mass") + ylab("log10 Prey Mass")


There is an apparent positive relationship across terrestrial pairs, but not in marine.

So when the data is split, we do get the expected positive slope from the terrestrial side. And by using lmodel2 and subsetting by troph$Hab == "Terrestrial,

lmodel2(Prey~Pred, data = troph2[troph2$Hab == "Terrestrial",])

you can see that the slope is 2.18, right in the range of what we expect based on Riede et al.’s results. On the other hand, there still does not seem to be a relationship with marine mammals.

What about the second point? Does predator mass increase with increasing trophic level?


g3 <- ggplot(troph2, aes(x = TL, y = Pred)) + geom_point() + geom_smooth(method = "lm")
g3 + xlab("Trophic Level") + ylab("log10 Prey Mass")

There certainly does seem to be a positive relationship.

There certainly does seem to be a positive relationship.

It is fairly clear that mass is increasing with trophic level, but does the pattern hold when the two habitat types are split?



When we split the data by habitat, the trend of increasing body size with trophic level disappears, something that holds true whether or not we include herbivores (herbivores are excluded in the graph on the right). It seems that in this case what was driving the pattern was the combination of habitats, and that on average marine mammals are both larger and on a higher trophic level than terrestrial mammals.

Now lets test the predictions of body mass ratios

In the first case we saw that while there was no relationship in the aggregate data, some appeared in the split data. In the last case the opposite was true. Will body mass ratio decline with trophic level, or will the pattern depend on how we look at the data? Note that in this case the herbivores will be excluded due to a lack of “prey size” data (no ratios).

g6 <- ggplot(troph2, aes(x = TL, y = Ratio)) + geom_point() + geom_smooth(method = "lm")
g6 + xlab("Trophic Level") + ylab("log10 Predator-Prey Mass Ratio")

Increasing body mass ratio with increasing trophic level.

Increasing body mass ratio with increasing trophic level.

When the data is aggregated we get the opposite of what we expected to see. From Riede’s paper, the Eltonian viewpoint should be that the body mass ratio should be independent of trophic level. The new view proposed by Riede et al. is that the ratio should decline with trophic level. The aggregated data from Tucker and Rodgers shows that there is an increasing ratio with increasing trophic level. This means that at higher trophic levels organsims are consuming comparably smaller organisms. Splitting the data by habitat type:

Again the opposite trend emerges, decreasing ratio with increasing trophic level

Again the opposite trend emerges, decreasing ratio with increasing trophic level

Based on the split data, it seems as if the positive trend in the previous plot was being driven by the fact that on average terrestrial mammals occupy a lower trophic level, and have smaller mass ratios. Likely the larger mass ratios are being driven by the inclusion of whales in the Tucker and Rodgers dataset. Terrestrial mammals have relatively few species of comparable size, coupled with the fact that whales also tend to eat small organisms (e.g., krill), which is a potential explanation for the discrepancy.

All in all, it seems that the data provided by Tucker and Rodgers may support the findings of Riede et al., if viewed in the proper context. I do wonder if there is any overlap in the datasets, although I am doubtful. The paper by Riede et al. based their analyses on data derived from a food web context, and I do not think that Tucker and Rodgers used similar methods. Without reading the original paper I can only speculate, but my guess would be that for this dataset, trophic levels came from stable isotope data or derived from the literature. Hopefully their paper (Predictors of predator-prey body size, trophic level and body mass in mammals) will come out soon as I am sure it will be interesting to see what they’ve done with it.

Posted in Coding, Fun | Leave a comment

Elton’s footsteps – Thursday Review

In this post, and others to come every Thursday I will choose a random paper from my stash of 1500 or so, read it and review it.

Today’s paper is: Stepping in Elton’s footprints: a general scaling model for body masses and trophic levels across ecosystems

eltonsFOOTPRINTSThe Eltonian footprint we are stepping into with this paper is the concept of predator/prey body size ratios. The authors describe the prevailing paradigm as

1. predator and mean prey mass are positively correlated

2. predator masses increase with trophic level

3. predator-prey body mass ratios do not vary consistently across trophic levels

They immediately follow with a list of the issues surrounding this paradigm:

1. the scatter in the relationships among prey mass, predator mass, and trophic level can be substantial

2. a few studies have reported different relationships

3. [the third concept (above)] has not [to the author’s knowledge] been explicitly analysed, neither within nor across ecosystems

In order to reconcile the current Eltonian paradigm with these apparent issues the authors use a dataset of 35 food webs;  from marine (5), stream (12), lake (10), and terrestrial (8) habitats. At this point I should note that I think it is a plus that while they do not include their data alongside their publication they do provide a very nice table that serves as a lovely treasure map telling the reader exactly where to find all the information.

So what they do is split their data into marine, stream, lake, and terrestrial habitats, and analyze the relationship between prey mass vs predator mass, predator mass vs trophic level, and predator-prey mass ratio vs trophic level. For each habitat type the consumers were split up into invertebrates, ectotherm vertebrates, and endotherm vertebrates. The authors note three general results that they have shown:

1. the log10 of average prey mass increases with the log10 of predator mass with a slope greater than unity

2. the log10 of predator mass increases with predator trophic level

3. the log10  of the predator-prey body-mass ratio […] decreases with predator trophic level

It is interesting to find that larger predators consume prey that are on average, closer to their own size. I suppose from an evolutionary standpoint it makes sense, you get more bang for your buck with a larger prey and should therefore be able to spend more of your time and energy growing and reproducing, rather than finding more small things to eat. I do wonder, however, whether social or pack hunting could be taken into account. Of those large carnivores that consume even larger prey, I would think a decent portion utilize pack hunting, e.g., lions or wolves. If that were the case is the appropriate body mass ratio the predator/prey or the pack/prey. Another concern I have with body mass ratios is that very rarely do predators consume their entire kill, especially when that kill is large (e.g., an elephant). Some portion of the prey goes towards feeding scavengers and insects. Should that be taken into account when discussing body mass ratios, or is what we are interested in merely the ability to kill a prey individual? And what about parasites? In this study they focused solely on predator-prey interactions, ignoring as well herbivores and detritivores. I wonder if parasites would follow the opposite trend as regular predator prey, or if they would be closer to whatever the equivalent plant-herbivore ratio scaling would be.

I really liked the last paragraph of their discussion, where they relate their findings of body mass ratios to studies of interaction strength and stability.  According to their findings we should expect that interactions should be strongest amongst the intermediate trophic levels, and comparably weaker interactions at higher trophic levels. Compare this with what was found by Ulanowicz, Holt, and Barfield (2014), that in trophic chains that are unusually long (6-9 trophic levels) the interactions at the top of the chain are relatively weak. Precisely what is predicted by Riede et al.

Posted in Thursday Review | Tagged , , , , | 1 Comment

A shiny new Rmarkdown for the win!

After a few weeks without using Rmarkdown, I recently went to use it for a new project I am working on where I am accumulating information on the numerous data sources out there for food webs and other ecological networks. I decided to use Rmarkdown because I see it as a great way to integrate text, code, and images in a single document. The last time I used markdown, I really liked the ease of creating plain markdown and html files, along with the option of using pandoc within R to convert to pdf.

Cut to the past few days and I have learned that Rmarkdown has gotten a great upgrade. Everything that it was good for previously, it is now even better at, and now there are so many awesome new options!

Popup menu for starting a new Rmarkdown project

Popup menu for starting a new Rmarkdown project

When you start a new Rmarkdown project you have the option of generating a plain document in either html, pdf, or word (word? really, that’s a little weird but still COOL!), or you can make html or pdf slides, set up a an interactive document or slides that can integrate shiny components, there is even an option to use custom templates. I have to say that the possibilities for this are very intriguing. There is no doubt in my mind that I am going to have some fun playing around with all the new things you can do with it.

Posted in Fun, Writing | Tagged , , , | Leave a comment

Linneaus’ wormy fish – Thursday Review

In this post, and others to come every Thursday I will choose a random paper from my stash of 1500 or so, read it and review it.

Today’s paper is: Food web topology and parasites in the pelagic zone of a subarctic lake

Added bonus, it is open access (click the image to go get the paper)


This paper starts out with an amusing anecdote about Carl von Linne on an expedition in 1732 to subarctic Lapland. On his visit, apparently he found the local Arctic charr to be so infested with parasitic worms that he would not eat them. Thus we have the impetus for this paper, what role do these parasitic organisms play in structuring the food web.

The main point of this paper is to describe the food web of a freshwater lake and to determine how including parasitic interactions will impact the structure of the web. It seems as if describing food webs with parasites has become quite popular in the last ten years or so, since Lafferty published several papers on the potential impacts and ubiquity of parasites in natural communities. There are data for several such webs stored in the Ecological Archives (and I have talked about them before), and this is just adding to pile, which in my opinion is always a good thing. And for now, I am going to ignore the recent paper by Dunne et al. demonstrating that there is nothing inherently special about adding parasites to food webs that changes the network structure, rather it is the same as simply increasing the size of the web.

On to the web!

Amundsen and colleagues are describing the food web of lake Takvatn (is that redundant? as the authors note “vatn” means “lake”, oh well), a subarctic, oligotrophic, and dimictic lake in Norway.  Annual studies since 1979 have yielded information about the population dynamics, feeding ecology, and parasite ecology of the fish community. These studies allowed the authors to create a source based food web (although I would consider a source web to have only a single source, where here they have several species of phytoplankton at the base of the web). Their web is spatially and temporally restricted to the open water limnetic zone of the lake, and only during the ice-free season.

The zones of a lake if (like me) you have forgotten a lot from your general ecology class… (from, click the picture to go to their site)

Each node was (to the best of their ability) classified to the species level, which I think was a good decision. While there have been studies that show that lumping species into categories by trophic/taxonomic/functional groups doesn’t impact overall structural properties that much (provided it is applied evenly), I think that if what we are ultimately interested in is the dynamics of the community, it is a waste to throw away useful information, and potentially important complexity. But then again, the most popular food web models (e.g., the niche, cascade, nested hierarchy) are only able to produce trophic species webs (which is what I think drives their popularity). Of course, that is neither here nor there. I also liked that they did not split the parasites up into different life stages. At the end they wound up with a food web with 50 nodes (37 without parasites) and 432 links (198 without parasites).

Of course, while they include species lists in their supplemental material, they do not include the adjacency matrix for their food web, so I am unable to reproduce their results, or incorporate their food web, which seems great, into bigger questions…

I found it interesting that they described their food web as “wasp-waisted” with high diversity in the upper and lower levels, while intermediate levels were of relatively low diversity. I have not heard this term used before, but it does make me want to look through my collection of food web datasets to see if any of them are like that.

The food web of Takvatn. Figure is from the paper “Food web topology and parasites in the pelagic zone of a subarctic lake.” I would have preferred to create my own figure of the web, but without the data this is the best I can do.

Their main results were that including the parasites increased connectance, omnivory, link density, and nestedness. It was interesting to find out that before and after the inclusion of parasites, the most connected species remained the most connected species. I also liked that they found a relationship between the number of predator-prey links a given host species had, and the number of trophically transmitted parasites links it had. Although, perhaps we may say that is relatively unsurprising. I do wonder if that is merely an artifact of probability, if you eat more things you are more likely to get parasitized. One could imagine a simple test using two null models, one that preserves degree distribution and one that did not. If, compared to randomized webs with the same degree distribution you find the same pattern, then it may be simple probability, you would not expect to see the pattern in a randomization that does not preserve the degree distribution.

While it may be (as I said above) that parasites do not alter the structure significantly differently than if they were any other functional group (more predators perhaps), that does not necessarily mean that there is no use in including them in food web studies. There is, to me, no reason to say well, they are not all that structurally important so let’s just pretend there are fewer species in the system (although I don’t think anyone is making quite that argument).

I think that what will become important in the future will be the implications of parasitic links towards the dynamics of the food web. We already know from Allesina and Tang (2008) that increasing the number of antagonistic links in a network has a stabilizing effect, so the fact that including parasites increases connectance somewhat significantly could help to explain how such complex systems are stable. I am definitely looking forward towards being a part of the development of these kinds of ideas as I progress through my dissertation.

One thing I cannot avoid mentioning is that in their discussion the authors cite Begon, Townsend, and Harper (2006).  For those who don’t know this is one of the most widely used General Ecology textbooks. Seeing this as a citation in a scientific paper really grated on me. There is no original research in this book (unless I seriously misread it, and correct me if I am mistaken). If you want to cite something from a textbook, stop and just don’t. It is not that challenging to find the source from which the information came and cite the original research article in your paper. Citing textbooks is something you do in high school when you don’t know any better. Sure, using a textbook as a starting point is a good place to find a collection of sources on your topic, but you should always go to the original source. It is paramount to citing Wikipedia, albeit a bit more credible. But that is my opinion.

The “definitive ecology textbook”

Overall, I enjoyed reading a paper about how researchers put together a food web. I think I will definitely have to read more of these in the future.

Posted in Thursday Review | Tagged , , , , , | Leave a comment

writeLaTex for collaboration

I recently began a project that will have a large number of contributing authors, the result of a review of the proceedings of a workshop I recently attended (more on that later). Most of the other participants (think a lot of early career theoreticians) exhorted their dislike of programs like MS Word for writing, with one person going so far as to say they will not read any papers sent to them in Word format. I have been toying with the idea of writing my manuscripts in a more version control friendly format for a little while now. It just seems more logical to me to write up a paper in Rmarkdown (since most of my coding is done in R) or LaTeX. Not only is it a nicer output, but it is far more straightforward in my opinion, with the added benefit of being compatible with git.

This presented me with a perfect opportunity to try out some new tools that others have found very useful for collaborative writing. Because most of the workshop’s participants are at this point spread out across the country again, I thought that writeLaTeX would be very useful to us. WriteLaTeX is a great website that allows you to create, read, and edit LaTeX documents in your browser. It’s layout reminded me of using something like Mou for markdown (when I still had my Mac). One side you have the uninterpreted LaTeX document, and on the other side you have the interpreted output (pdf) that updates in real time so you can see what kind of changes you are making. I think that this is a great way to learn how to write using LaTeX.

Moreover, they made the whole process very easy. I went to the site and created a free account. From there I started out with a new article. You can choose to create a new paper or presentation and they will start you out with a default template that shows you some of the basics, a lot like when you start a new Rmarkdown script. From there I just made some necessary changes, using the very useful help page to learn how to add additional authors and affiliations. The site also allows you and your contributors to archive different versions of the document.

The new paper template.

The new paper template.

Collaboration is made very easy with this site. There is a “share” button you can click and it offers you two options; a link for reading and editing, or a link for just reading. So you can invite people to contribute to your project, or you can just have them take a look at it without worrying that they will mess it up. Each author can edit the document in real time, with changes being made by one person immediately visible by the others. Another feature I thought was pretty neat is that for those who are a bit intimidated by the different LaTeX commands, there is a rich text option, which lets you edit the document in a manner that is very similar to Word.

Apparently this site can be used with Dropbox, so you can more easily upload figures and such I suppose. You can also save/download the pdf with only a few clicks. There is also the option to “Publish” that offers links to various preprint sites, such as PeerJ, arXiv, bioRXive,  F1000, as well as writeLaTeX’s own gallery.

I am definitely looking forward to getting more familiar with LaTeX and writeLaTeX, and I suggest that if you are interested in learning LaTeX, or collaborating on a paper that you take a look at the site.


Posted in Other, Writing | Tagged , , , | Leave a comment

On the horizon

I have not been posting to the blog as much as I would like. Most of my time has been consumed with getting a paper that is currently in review, two that are in different stages of “being written,” and trying to design a project or two to finish up my dissertation (the one I had in mind no longer seems feasible).

But that is not much of an excuse, and I would very much like to post regularly if only to keep honing my writing skills. So to that effect I plan on starting a weekly post where I review a random paper from my stash of papers. I have downloaded about 1500 papers over the last 3 years, and I have read or at least looked through around 900 of those. In the last year the number of papers I read per month has dramatically decreased. As I am sure many of you can commiserate, there is simply not enough time to keep up with all of the papers and do your own work. So this weekly post will serve multiple purposes for me; increase my weekly reading, increase my weekly writing, and forcing me to keep up with a blog. At the very least this kind of thing is something I can do in advance and keep a queue so that there will be some regularity to my posts.

I still plan on writing more R tutorials, and possibly start moving towards more about Python and Julia, since those are two other languages I would like to learn. So you can look forward to my sharing the pain of learning a new language that is almost painfully similar to the one I have become familiar with.

Also, keep an eye out for a few posts I am planning about a trip to Switzerland (my current location) where I attended a workshop about “Non-adaptive selection: explaining macroscopic laws in ecology and evolution,” as well as a few thoughts I have been having on going to scientific meetings and the peer review process.

Posted in Other | Leave a comment

My first try at plotly

I have been hearing a lot about this new tool for interactive plotting, called plotly, and I have been wanting to try it out for a while. So this is my first attempt at using plotly with R and RStudio.

The packages that are needed are just devtools (for installing the plotly library using install_github), and ggplot2 makes great plots that are compatible with plotly.

This first bit of code is just for loading packages and installing plotly. (note: I am also loading RCurl here to facilitate grabbing data from GitHub)


 ## Loading required package: devtools


 ## Loading required package: ggplot2


 ## Loading required package: RCurl
 ## Loading required package: bitops

 if (!require(plotly)) {
 install_github("plotly", "ropensci")
 ## Loading required package: plotly
 ## Loading required package: RJSONIO



Now that I have installed the plotly package, I need to create a plotly object in order to sign into my Plotly API account. This was made very easy, I basically just copied the code from their online GUI.

py <- plotly(username = "jjborrelli", key = "mykey")


I also signed up for a plotly API using their online GUI. Once I signed up my username and key were given to me through the GUI.

Now I can start plotting:

First I load in some data to make the plot. This particular data is the trophic position and omnivory index for the species in 50 different food webs that I have been collecting.

url <- getURL("")
tinfo <- read.csv(text = url, row.names = 1)

Then create a plot with ggplot2, here I am plotting the omnivory index (OI) as a function of trophic position (TL).

g <- ggplot(tinfo, aes(x = TL, y = OI)) + geom_point(aes(col = L1))

The syntax to make the interactive plot is quite simply py$ggplotly()

r <- py$ggplotly(g)

This will generate a url that you can follow to view the plot.

And you can view the plot on here

So it turns out this was incredibly easy, and makes for amazing interactive graphs. I will definitely be using this tool frequently in the future.

I would love to be able to insert the interactive version of the graph here in the post, but WordPress will not let me, so in the future I may be migrating this blog from WP to a more code friendly hosting platform such as GitHub pages (powered by Jekyll).

Posted in Coding | Tagged , , | 2 Comments