Math, programming, ecology, and more

Interactive visualization of survival curves with Shiny

We have a growing interest in using our favorite tools (R and Mathematica) to build web interfaces to interactively explore and visualize data. Our last 5 posts have involved interactive tools, namely Mathematica’s computable document format and R’s new Shiny package.

There is a new kid on the block for interactive visualization tools in R, healthvis. I have not yet taken healthvis for a spin, but the survival example in the introductory blog post inspired me to create a Shiny app to visualize the results of a survival analysis conducted for my dissertation.

As part of my PhD research, I used Cox proportional hazards models to analyze the behavior of ladybird larvae (see photo at bottom of post). The Cox models were used to determine if experimental factors (species of aphid eaten, duration of starvation period) affected the likelihood that a ladybird larvae (Hippodamia convergens) ended a behavioral bout (e.g., stopped searching intensively, exited the patch).

The standard approach to presenting the results of a survival analysis is to plot the proportion of individuals that are still alive (or engaging in a behavior) in each test group over time. Survival curves can provide a nice visualization of the effect of categorical predictors in a Cox model, but visualizing the effect of continuous covariates is trickier and might require the use of numerous lines in one figure (creating clutter) or the use of a multi-panel figure (making it harder to see the combined effects of the continuous and categorial covariates).

An interactive visualization, however, allows the user to see the effect of changing continuous covariates on the survival curves. In the example below*, clicking play causes the slider to loop through the starvation period values and creates an animation of the effect of starvation period on the likelihood that a ladybird larva leaves the patch after eating either a pea aphid (Acyrthosiphon pisum) or black bean aphid (Aphis fabae).

The ‘Model Summary’ tab provides summary output from the Cox model. There is no significant effect of the type of aphid consumed, which is apparent from the plot at low values of starvation period. However, increasing the starvation period increases the likelihood that ladybird larvae leave the patch (significant starvation effect), but only for ladybird larvae that ate a pea aphid (significant interaction effect). The starvation and interaction effects are clear from the animation as the solid red line shifts towards the plot origin (increased patch-leaving tendency) while the solid black line moves very little.

For even this simple model, the interactive visualization is a big improvement over the static visualizations that I had created previously for talks and my dissertation and, generally, interactive visualizations should outperform their static counterparts as model complexity increases. The increasingly digital nature of scientific publishing suggests that interactive graphics are the future and tools like Mathematica’s CDF, Shiny, and healthvis are making the creation of interactive graphics more accessible to scientists.

*The data and code used to create the Shiny app are available from GitHub.