<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/visualising-uncertainty-in-time-series-using-animations/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.seascapemodels.org/rstats/2017/05/14/timeseries-uncertainty-gif.html"> Bluecology blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Visualising uncertainty in time-series using animations</h2>
<p>I was reading an example of visualising election outcome uncertainty<br>
using a <a target="_blank" rel="nofollow" href="https://medium.com/@jofu/openvis-2017-a-recap-3e945bb9f267">wobbly<br>
needle</a> and<br>
wondered, could animating uncertainty help communicate for the kind of<br>
work I do on population modelling?</p>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/time_series_uncertainty_figs/images_pause.gif?w=456" data-recalc-dims="1"></p>
<p>One challenge when plotting uncertainty intervals for population trends<br>
is that confidence intervals don’t capture the wandering nature of<br>
population processes. The figure here shows the median of a poisson<br>
random walk, with 90% quantiles.</p>
<p>In fact, each trend line will wander around, like the next image. So,<br>
the median and intervals give a false impression that an individual<br>
trend will experience relative stability.</p>
<div>
<img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/time_series_uncertainty_figs/tsu_ciplot.png?w=456" data-recalc-dims="1"><br>
<img alt="" src="https://i0.wp.com/www.seascapemodels.org/Images/time_series_uncertainty_figs/tspng_30.png?w=456" data-recalc-dims="1">
<p> A graph of the median and quantiles doesn’t represent the uncertainty in any single trend line very well. </p>
</div>
<p>I decided to try and animate the uncertainty, using R.</p>
<p>The first step was to try and install the R package <code>animate</code>.<br>
Unfortunately I was in a world of Unix coding pain trying to get<br>
animation to work. In particular to get R to talk to the<br>
<a target="_blank" rel="nofollow" href="https://www.imagemagick.org/">ImageMagick</a> program. After cutting and<br>
pasting numerous commands from StackOverflow to my terminal I gave up.<br>
It seems I need to update my OS before I can get any further (from<br>
Mavericks).</p>
<p>In the end I found I could use ImageMagick directly from the command<br>
line. So now what we need is a folder full of .png files that we wish to<br>
turn into the animation. That Unix code to do that is at the end of this<br>
blog.</p>
<p>First we should set up a random walk process. I decided to go for a<br>
Poisson random walk, because it will have integer numbers, like a real<br>
population. I wrote a little function to achieve this</p>
<div>
<pre><code>walkmod &lt;- function(t, yinit, thetasd, rho){
  y &lt;- numeric(t)
  y[1] &lt;- yinit
  theta &lt;- rnorm(t, sd = thetasd)
  for(i in 2:t){
    lambda &lt;- (y[i-1]*rho + yinit*(1-rho))* exp(theta[i])
    y[i] &lt;- rpois(1, lambda)
  }
  return(y)
}
</code></pre>
</div>
<p>The core equation is in the <code>for</code> loop. It just says the mean abundance<br>
at a given time is dependent on abundance at the last time-step with<br>
correlation <code>rho</code> plus some extra variation <code>theta</code>. We then sample the<br>
observed abundance from a poisson distribution.</p>
<p>Now let’s use our function. We will load in the <code>purrr</code> package to help<br>
plotting replicate runs of the random walk and the <code>leaflet</code> package<br>
because it has some nice functions for making colour scales.</p>
<div>
<pre><code>library(purrr)
library(leaflet)

t &lt;- 50
yinit &lt;- 10
thetasd &lt;- 0.01
rho &lt;- 0.9
n &lt;- 30

y &lt;- matrix(NA, nrow = t, ncol = n)
tmat &lt;- matrix(rep(1:t), nrow = t, ncol = n)

for (i in 1:n){
    set.seed(i)
    y[,i] &lt;- walkmod(t, yinit, thetasd, rho)
}
ymax &lt;- max(y)
medy &lt;- apply(y, 1 , median)
</code></pre>
</div>
<p>We just looped over the <code>walkmod</code> function <code>n</code> times so we could<br>
generate <code>n</code> random walks.</p>
<p>Now let’s build a colour scale. I would like to colour the lines by the<br>
abundance quantile they end on. So we apply <code>colorQuantile</code> to all<br>
abundances in the last time-step to get a red to blue (<code>RdBu</code>) colour<br>
palette for the lines:</p>
<div>
<pre><code>quants &lt;- c(0, 0.25, 0.5, 0.75, 1)
yquant &lt;- quantile(y[t,], probs = quants)
mycols &lt;- colorQuantile(&quot;RdBu&quot;, domain = y[t,], probs = quants)
</code></pre>
</div>
<p>The trick to making a .gif animation is just to make a bunch of figures<br>
and save them as .png files in the order you want them to animate (or if<br>
you can get <code>animate</code> package to work within R you can export the .gif<br>
directly without having to save the .pngs).</p>
<p>So we just loop over <code>walkmod</code> plotting our lines each time. Note I use<br>
<code>walk</code> from the <code>purrr</code> package to redraw the background lines in grey<br>
each time.</p>
<div>
<pre><code>thispath &lt;- &quot;/insert/your/path/here/&quot;

for (i in 1:n){
    fname &lt;- paste(thispath, i, &quot;.png&quot;, sep = &quot;&quot;)
    png(filename =fname, width = 480, height = 480)
    plot(0, 0, xlim = c(0, t), ylim = c(0, ymax),
         bty = 'n', type = &quot;n&quot;, las = 1,
         ylab = &quot;Abundance&quot;, xlab = &quot;Time&quot;)
    lines(1:t, medy, col = &quot;grey90&quot;, lwd = 2)
    walk(1:i, ~lines(tmat[,.x], y[,.x], lwd = 0.7, col = grey(0.5, 0.2)))
  lines(1:t, y[,i], lwd = 2, col = mycols(y[t, i]))
    legend(&quot;topleft&quot;, legend = quants,
        lwd = 2, col = mycols(yquant),
    title = &quot;Quantile t=100&quot;, cex = 0.8)
  dev.off()
}
</code></pre>
</div>
<p>If you want to check this from R, without saving the files, then turn of<br>
the <code>png()</code> function that saves the files and insert <code>Sys.sleep(0.1)</code> at<br>
the end of the loop, so it will print each figure in turn but pause for<br>
1/10 <sup>th</sup> of second each time.</p>
<p>We have one final figure to plot, that is the median line. We can do<br>
this outside the <code>for</code> loop:</p>
<div>
<pre><code>fname &lt;- paste(thispath, i+1, &quot;.png&quot;,  sep = &quot;&quot;)
png(filename =fname, width = 480, height = 480)

plot(0, 0, xlim = c(0, t), ylim = c(0, ymax),
     bty = 'n', type = &quot;n&quot;, las = 1,
     ylab = &quot;Abundance&quot;, xlab = &quot;Time&quot;, main = &quot;Median&quot;)
walk(1:i, ~lines(tmat[,.x], y[,.x], lwd = 0.5, col = grey(0.5, 0.2)))
lines(1:t, medy, lwd = 3, col = &quot;purple&quot;)
dev.off()

cd &quot;your/path/here&quot;
magick *.png images.gif
convert images.gif -set delay 30 \( +clone -set delay 100 \) +swap +delete images_pause.gif
</code></pre>
</div>
<p>The first line creates the gif and the second line adds a pause of 30<br>
1/100ths of a second after each image and a pause of 1 second at the end<br>
of the loop.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.seascapemodels.org/rstats/2017/05/14/timeseries-uncertainty-gif.html"> Bluecology blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1nSmGuR-Kr8"></a></div>