Big Data/Analytics Zone is brought to you in partnership with:

Arthur Charpentier, ENSAE, PhD in Mathematics (KU Leuven), Fellow of the French Institute of Actuaries, professor at UQàM in Actuarial Science. Former professor-assistant at ENSAE Paritech, associate professor at Ecole Polytechnique and professor assistant in economics at Université de Rennes 1. Arthur is a DZone MVB and is not an employee of DZone and has posted 154 posts at DZone. You can read more from them at their website. View Full User Profile

Animation from R to LaTeX

05.06.2013
| 2314 views |
  • submit to reddit

Just a short post, to share some codes used to generate animated graphs, with R. Assume that we would like to illustrate the law of large number, and the convergence of the average value from binomial sample. We can generate samples http://latex.codecogs.com/gif.latex?X_{i,j}\sim\mathcal{B}(1/2) using

> n=200
> k=1000
> set.seed(1)
> X=matrix(sample(0:1,size=n*k,replace=TRUE),n,k)

Each row http://latex.codecogs.com/gif.latex?\boldsymbol{X}_{i}=(X_{i,1},\cdots,X_{i,n},\cdots) will be a trajectory of heads and tails. For each trajectory, define the mean http://latex.codecogs.com/gif.latex?\bar{X}_{i,n}=n^{-1}(X_{i,1}+\cdots+X_{i,n}), which will denote the mean of the first http://latex.codecogs.com/gif.latex?nvalues. Such a matrix can be computed using

> cummean=function(M){
+ U=matrix(M[,1],nrow(M),1)
+ 	for(i in 2:ncol(M)){
+ 	U=cbind(U,(U[,i-1]*(i-1)+M[,i])/i)}
+ return(U)
+ }

Define then

> Xbar=cummean(X)

Now, to generate an animated gif, the way I usually do it is to generate graphs (png graphs) using a loop,

> S=trunc(10^seq(1,3,by=.05))
> for(j in 1:length(S)){
+ 	s=S[j]
+ 	Xhist=hist(Xbar[,s],breaks=seq(0,1,by=.05),plot=FALSE)
+ 	nfile=paste("LLN-",100+j,".png",sep="")
+ 	png(nfile,600,350)
+ 	layout(matrix(c(3,0,1,2),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
+ 	plot(1:s,Xbar[1,1:s],type="l",col="light blue",ylim=0:1,xlab="",ylab="",axes=FALSE,
+ 		 xlim=c(10,k),log="x")
+ 	axis(1)
+ 	axis(2)
+ 	for(i in 2:(n-1)) lines(1:s,Xbar[i,1:s],col="light blue")
+ 	lines(1:s,Xbar[n,1:s],col="red",lwd=2)
+ 	abline(v=s)
+ 	barplot(Xhist$counts, axes=TRUE,horiz=TRUE,col="light green",xlim=c(0,n/2*1.05))
+ 	dev.off()
+ }

I start at 100 because afterwards, when merging files, it is better to have (really) consecutive numbers, since sometimes, the lexical order is used, i.e. after 1 is 10, then 100, etc. Then I use Terminal commands

Here, the delay is in /100 seconds, and I use an infinite loop. The graph is here

It is possible to use

> library(animation)
> ani.options(interval=.15)
> saveGIF({      })

But the loop can be used also to generate several graphs, and to produce an animated graph in a pdf document (slides or lecture notes). The idea is to use the same code, but the output is here a pdf graph.

> S=trunc(10^seq(1,3,by=.1))
> for(j in 1:length(S)){
+ s=S[j]
+ Xhist=hist(Xbar[,s],breaks=seq(0,1,by=.05),plot=FALSE)
+ 	nfile=paste("LLN-",j,".pdf",sep="")
+ 	pdf(nfile,10,6)
+ layout(matrix(c(3,0,1,2),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
+ plot(1:s,Xbar[1,1:s],type="l",col="light blue",ylim=0:1,xlab="",ylab="",axes=FALSE,
+ xlim=c(10,k),log="x")
+ axis(1)
+ axis(2)
+ for(i in 2:(n-1)) lines(1:s,Xbar[i,1:s],col="light blue")
+ lines(1:s,Xbar[n,1:s],col="red",lwd=2)
+ abline(v=s)
+ barplot(Xhist$counts, axes=TRUE,horiz=TRUE,col="light green",xlim=c(0,n/2*1.05))
+ 	dev.off()
+ }

We can then import them in LaTeX,

\documentclass{article}
\usepackage{graphicx}
\usepackage{animate}
\begin{document}
\begin{center}
\animategraphics[height=3.1in,palindrome]{1}{/Users/UQAM/CLT-}{1}{21}
\end{center}
\end{document}

This will generate the following [a href="http://f.hypotheses.org/wp-content/blogs.dir/253/files/2013/05/slides-anim.pdf" style="text-decoration: none; outline-style: none; color: rgb(0, 130, 137);"]pdf file. This animate package is described in several forums, e.g.http://www.geogebra.org/…

Published at DZone with permission of Arthur Charpentier, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)