Thursday, May 5, 2011

Animation of branching random diffusion

I just created a function to animate branching random diffusion (i.e., Brownian motion with speciation) in discrete time. The functions (optionally) calls several functions from the R package {animation}, and to save the resulting video to file, requires the installation of 'FFmpeg'. My function is available on my R-phylogenetics page (direct link here).

To run it after having installed {animation} and 'FFmpeg', first load the function from source:

> source("branching.diffusion.R")

then, try with the default options (but saving the animation to file using the filename record="diffusion.mp4"):

> branching.diffusion(record="diffusion.mp4")

Be warned, all the animation functions eat up lots of memory. If you just want to view the animation in R without saving it, simply set record=NULL (the default), i.e.:

> branching.diffusion()

The user can also change the variance of the BM process, the birth-rate, and the time of the simulation. One option, smooth, tells the function whether to destroy the existing plot and create a new one each generation using plot() (which results in a slower animation, but is smooth); or add the new bits of evolution using lines() (which results in a faster but choppy animation - although the saved movie should be unaffected). You can also specify the pause between generations (default, 0.02s) and the path to ffmpeg.exe (default path="C:/Program Files/ffmpeg/bin/ffmpeg.exe"). The video below was created using the function on its default settings. Enjoy!

3 comments:

  1. Thanks to Carl Boettiger for pointing me towards 'animation' earlier.

    ReplyDelete
  2. Hi Liam -- pretty awesome! I got ffmpeg and dependencies installed on my mac os X (port install ffmpeg did the trick). And this worked:

    branching.diffusion()

    ...however, when I tried to save it, I got:

    ===============
    > branching.diffusion(record="diffusion.mp4")

    Error in mf[pmatch(names(mc), names(mf))] = mc :
    NAs are not allowed in subscripted assignments
    ===============



    Putting in the correct path to ffmpeg didn't help:
    ===============
    > branching.diffusion(record="diffusion.mp4", path="/opt/local/bin/ffmpeg")


    Error in mf[pmatch(names(mc), names(mf))] = mc :
    NAs are not allowed in subscripted assignments
    ===============

    Cheers!
    Nick

    ReplyDelete
  3. Hi Nick.
    Unfortunately, I'm stumped. I don't recognize the error. Any progress on getting this to work? - Liam

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.