楼主: janyiyi
1207 2

Animation, from R to LaTeX [推广有奖]

  • 3关注
  • 17粉丝

讲师

27%

还不是VIP/贵宾

-

威望
0
论坛币
3206 个
通用积分
5056.6800
学术水平
539 点
热心指数
537 点
信用等级
538 点
经验
10157 点
帖子
300
精华
2
在线时间
90 小时
注册时间
2010-10-3
最后登录
2024-4-6

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
By arthur charpentier


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 using

  1. > n=200
  2. > k=1000
  3. > set.seed(1)
  4. > X=matrix(sample(0:1,size=n*k,replace=TRUE),n,k)
复制代码

Each row will be a trajectory of heads and tails. For each trajectory, define the mean , which will denote the mean of the first values. Such a matrix can be computed using

  1. > cummean=function(M){
  2. + U=matrix(M[,1],nrow(M),1)
  3. +         for(i in 2:ncol(M)){
  4. +         U=cbind(U,(U[,i-1]*(i-1)+M[,i])/i)}
  5. + return(U)
  6. + }
复制代码

Define then

  1. > Xbar=cummean(X)
复制代码

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

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

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

  1. > library(animation)
  2. > ani.options(interval=.15)
  3. > 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.

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

We can then import them in LaTeX,

  1. \documentclass[a4]{article}
  2. \usepackage{graphicx}
  3. \usepackage{animate}
  4. \begin{document}
  5. \begin{center}
  6. \animategraphics[height=3.1in,palindrome]{1}{/Users/UQAM/LLN-}{1}{21}
  7. \end{center}
  8. \end{document}
复制代码

This will generate the following pdf file. This animate package is described in several forums, e.g. http://www.geogebra.org/…


Arthur CharpentierArthur Charpentier, professor in Montréal, in Actuarial Science. Former professor-assistant at ENSAE Paristech, associate professor at Ecole Polytechnique and assistant professor in Economics at Université de Rennes 1.  Graduated from ENSAE, Master in Mathematical Economics (Paris Dauphine), PhD in Mathematics (KU Leuven),





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:animation LaTeX ATION atex Late generate replace average number matrix

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
oliyiyi + 60 + 3 + 3 + 3 精彩帖子

总评分: 论坛币 + 60  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

沙发
oliyiyi 发表于 2019-4-30 16:34:09 |只看作者 |坛友微信交流群
谢谢分享

使用道具

藤椅
piiroja 发表于 2020-4-16 00:24:46 |只看作者 |坛友微信交流群
thx for sharing~

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 01:55