{"id":164,"date":"2012-07-18T02:07:44","date_gmt":"2012-07-17T23:07:44","guid":{"rendered":"http:\/\/ekqvist.goeuropeinfo.com\/rbloggerqvist\/?p=164"},"modified":"2012-07-18T02:07:44","modified_gmt":"2012-07-17T23:07:44","slug":"presenting-mortality-data-with-r-and-latex","status":"publish","type":"post","link":"https:\/\/science.ekqvist.fi\/blogi\/data-animation-with-r\/presenting-mortality-data-with-r-and-latex\/","title":{"rendered":"Presenting Mortality data with R and LateX"},"content":{"rendered":"<h1>Mortality, fertility and migration<\/h1>\n<p>What is the most informative way to present statistical dataset or actually time series data? Most convient way is to animate this data but how to do that with fastest and easiest way? Now I want to present one way to do that. <strong>Rob J Hyndman<\/strong> presented this data and animation technology in his <a href=\"http:\/\/robjhyndman.com\/researchtips\/animations\/\" target=\"_blank\">blog<\/a> on October 2010. Professor Hyndman presented French mortality data and how to utilize demography R-library.<\/p>\n<p><a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/FI_female_mortality_2009.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-167\" title=\"FI_female_mortality_2009\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/FI_female_mortality_2009-300x185.jpg\" alt=\"\" width=\"300\" height=\"185\" \/><\/a><\/p>\n<p>In this example I will introduce how to present mortality, fertility and migration data from Finland. As data source I will use data from here: <a href=\"http:\/\/www.mortality.org\/\" target=\"_blank\">http:\/\/www.mortality.org\/<\/a><\/p>\n<p>To get data you have to register as a user in The Human Mortality Database (HMD).<\/p>\n<blockquote><p><em>Data source: <\/em><br \/>\n<em>Human Mortality Database.\u00a0 University of California, Berkeley (USA), <\/em><em>and Max Planck Institute for Demographic Research (Germany). <\/em><em>Available at www.mortality.org or www.humanmortality.de <\/em><em>(data downloaded on 17.07.2012). <\/em><\/p><\/blockquote>\n<pre>Finnish death rate (mortality 1x1) data table name is Mx_1x1.txt\nand address is http:\/\/www.mortality.org\/hmd\/FIN\/STATS\/Mx_1x1.txt<\/pre>\n<p>Of course you could also use hmd function to get data from HMD (included in\u00a0 demographic library) ie.<\/p>\n<pre>norway &lt;- hmd.mx(\"NOR\", \"user\", \"pass\", \"Norway\")\nSee more informaton about this from <a href=\"http:\/\/cran.r-project.org\/web\/packages\/demography\/demography.pdf\" target=\"_blank\">there<\/a>.<\/pre>\n<p>Any way I downloaded Finnish data (23 Mb) as bulk transfer after registering above mentioned MHD service. In this case I do the following things:<\/p>\n<p><strong>STEP 1:<\/strong><\/p>\n<pre>fin_mortality &lt;- read.demogdata(\"data\/HMD\/FIN\/STATS\/Mx_1x1.txt\", \n\"\/data\/HMD\/FIN\/STATS\/Exposures_1x1.txt\", type=\"mortality\", \nlabel=\"Finland\")<\/pre>\n<p><strong>STEP 2:<\/strong><\/p>\n<p>#NEXT NOW WE WILL DO SEVERAL PDF FILES&#8230;&#8230; \ud83d\ude42<\/p>\n<pre>#MALE<\/pre>\n<pre>nyears &lt;- length(fin_mortality$year) for(i in 1:nyears) \n{ \u00a0\u00a0\u00a0 pdf(paste(\"fimale\",i,\".pdf\",sep=\"\"),height=4,width=6.5) \n\u00a0\u00a0\u00a0 x &lt;- fin_mortality \u00a0\u00a0\u00a0 \nif(i&lt;nyears) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x$\nrate$male[,(i+1):nyears] &lt;- NA \u00a0\u00a0\u00a0 \nplot(x,series=\"male\",ylim=c(-9.5,1.5), \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \nmain=paste(\"Finland: male mortality (\",fin_mortality$year[1]-1+i,\")\",\nsep=\"\")) \u00a0\u00a0\u00a0 \nif(i&gt;1) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \nx$rate$male[,1:(i-1)] &lt;- NA \u00a0\u00a0\u00a0 \nlines(x,series='male',lwd=2,col=1) \u00a0\u00a0\u00a0 dev.off() }<\/pre>\n<p>&nbsp;<\/p>\n<p>#NEXT NOW WE WILL DO SEVERAL PDF FILES&#8230;.AGAIN. \ud83d\ude42<\/p>\n<p>#FEMALE<\/p>\n<pre>nyears &lt;- length(fin_mortality$year) for(i in 1:nyears) \n{ \u00a0\u00a0\u00a0 pdf(paste(\"fifemale\",i,\".pdf\",sep=\"\"),height=4,width=6.5) \u00a0\u00a0 \n\u00a0x &lt;- fin_mortality \u00a0\u00a0\u00a0 if(i&lt;nyears) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \n\u00a0x$rate$female[,(i+1):nyears] &lt;- NA \u00a0\u00a0\u00a0 \nplot(x,series=\"female\",ylim=c(-9.5,1.5), \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \nmain=paste(\"Finland: female mortality (\",fin_mortality$year[1]-1+i,\")\",\nsep=\"\")) \u00a0\u00a0\u00a0 if(i&gt;1) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x$rate$female[,1:(i-1)] &lt;- NA \u00a0\u00a0\u00a0 \nlines(x,series='female',lwd=2,col=1) \u00a0\u00a0\u00a0 dev.off() }<\/pre>\n<p>#OK NOW LETS DO SOW TRICKS WITH LATEX<\/p>\n<p><strong>STEP 3<\/strong><br \/>\nNote! run this latex file at the same working directory as previous R step.<br \/>\nIn this example I use TexMakerX -program&#8217;s quick build &#8211;&gt; and after that you will find animated pdf file from your working director.<\/p>\n<pre>%Save this in the same working dir Note: replace fifemale text with fimale to \nproduce male mortality animation. \n\\documentclass{article}\n\\usepackage{animate}\n\\usepackage{graphicx}\n\\begin{document}\n\\begin{center}\n\\centerline{\\animategraphics[controls,buttonsize=0.3cm,width=12.5cm]\n\u00a0\u00a0\u00a0 {6}{\"fifemale\"}{1}{132}}\n\\end{center}\n\\end{document}<\/pre>\n<p><strong>STEP 4<\/strong><br \/>\nNow You will find mortality <a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/r-and-pdf-file-animation-my-own-example-fin-male-death-rate.pdf\">pdf file animation male death rate<\/a> and <a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/r-and-pdf-file-animation-my-own-example-fin-female-death-rate.pdf\">female death rate<\/a>. Pdf file include &#8220;play&#8221; button, just press it and You will see mortality animation between year 1878 &#8211; 2009.<\/p>\n<p>How to make sense with line colours? Please read this article from Hyndman and Shang. http:\/\/robjhyndman.com\/papers\/Rainbow5.pdf &#8220;&#8230; the colors of the curves follow the order of a rainbow, with the oldest data in red and the most recent data in violet&#8230;.&#8221;<\/p>\n<p><strong>STEP 5<\/strong><br \/>\nFor Web publishing it is better to do gif animation: R and ImageMagick Note: install also these package if you encounter problems Microsoft Visual C++ 2010 Redistributable Package (x86) and Microsoft Visual C++ 2010 Redistributable Package (x64) package But before do that try to do following: \u00a0 Note for some reason (I do not know why) I have to set my path variables like this: C:\\Program Files\\ImageMagick; So, I create ImageMagick -folder and copy convert.exe from folder C:\\Program Files (x86)\\ImageMagick-6.7.8-Q16 &#8230;and everything go fine now.<\/p>\n<p><code>#In working director create a new folder and set the working directory to the new folder. <\/code><\/p>\n<p>dir.create(&#8220;examples&#8221;)<br \/>\nsetwd(&#8220;examples&#8221;)<br \/>\nlibrary(animation)<br \/>\nlibrary(demography)<br \/>\n#let&#8217;s check how many years we have nyears length(fin_mortality$year)<br \/>\n#now we will do as many frames as years we have<\/p>\n<p><code>#MALE<br \/>\nmakeplot &lt;- function(){ for(i in 1:nyears) { \u00a0\u00a0\u00a0 x &lt;- fin_mortality \u00a0\u00a0\u00a0 if(i&lt;nyears) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x$rate$male[,(i+1):nyears] &lt;- NA \u00a0\u00a0\u00a0 plot(x,series=\"male\",ylim=c(-9.5,1.5), \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 main=paste(\"Finland: male mortality (\",fin_mortality$year[1]-1+i,\")\",sep=\"\")) \u00a0\u00a0\u00a0 if(i&gt;1) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x$rate$male[,1:(i-1)] &lt;- NA<br \/>\nlines(x,series='male',lwd=2,col=1) } }<br \/>\noopt = ani.options(interval = 0, nmax = nyears) saveMovie(makeplot(),interval = 0.1, width = 580, height = 400) ani.options(oopt) <\/code><\/p>\n<p><code>#FEMALE makeplot &lt;- function(){ for(i in 1:nyears) { \u00a0\u00a0\u00a0 x &lt;- fin_mortality \u00a0\u00a0\u00a0 if(i&lt;nyears) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x$rate$female[,(i+1):nyears] &lt;- NA \u00a0\u00a0\u00a0 plot(x,series=\"female\",ylim=c(-9.5,1.5), \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 main=paste(\"Finland: female mortality (\",fin_mortality$year[1]-1+i,\")\",sep=\"\")) \u00a0\u00a0\u00a0 if(i&gt;1) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x$rate$female[,1:(i-1)] &lt;- NA \u00a0\u00a0\u00a0 lines(x,series='female',lwd=2,col=1) } }<br \/>\noopt = ani.options(interval = 0, nmax = nyears) saveMovie(makeplot(),interval = 0.1, width = 580, height = 400) ani.options(oopt)<\/code><\/p>\n<p>Results:<br \/>\n<a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/fifemale1.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-174\" title=\"fifemale1\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/fifemale1.gif\" alt=\"\" width=\"480\" height=\"480\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/fimale1.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-176\" title=\"fimale1\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/07\/fimale1.gif\" alt=\"\" width=\"480\" height=\"480\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mortality, fertility and migration What is the most informative way to present statistical dataset or actually time series data? Most convient way is to animate this data but how to do that with fastest and easiest way? Now I want to present one way to do that. Rob J Hyndman presented this data and animation <a class=\"read-more-excerpt\" href=\"https:\/\/science.ekqvist.fi\/blogi\/data-animation-with-r\/presenting-mortality-data-with-r-and-latex\/\">[&#8230;] Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"gallery","meta":[],"categories":[15,21],"tags":[25,44,48],"_links":{"self":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts\/164"}],"collection":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":0,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"wp:attachment":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}