R Code

As of now 8 small packages have been posted to this site: 
    -LexisUtils (see github readme)
    -LexisSurface <- in limbo, have found better ways to do this
    -HMDget (deprecated- See DemogBerkeley instead)
    -DecompHoriuchi
    -Lotka 
    -Pyramid

    -LifeTable
    -RateSketch
    -EZLex
     
These can be downloaded manually from the downloads page and loaded from local .zip files from the R console, or you can visit the github page and get the package guts there directly. These are rough-and-ready solutions to different graphical and basic demography-related things that I repeatedly need access to. They are by no means extensively tested, and version numbers should be considered pre-release. There are also other functions in existence that do some of the things that my functions do. I have no immediate plans of uploading things to CRAN, mostly because I'm still too novice- rather than clutter CRAN with a bunch of dinky packages, I'm sort of waiting to accumulate many more functions and just upload one clean package to CRAN... some day, maybe. Until then, in good faith, most of what I do eventually makes it up to this site either in the packages or in individual blog posts. I totally encourage folks to write me reporting bugs and giving suggestions/requests. Awesome bug-reporters will be honored in the package documentation. By extension, feel free to contact me here or in github if you'd like to make suggestions, collaborate, fork off or improve on any of this work.

The html help files for all the packages are also available on this website, although I might fall behind updating them when packages undergo revisions. Images produced by function examples are also embedded on this site, but not necessarily console output.

Package summaries (links lead to index page):
LexisUtils NEW built for Shiro Horiuchi to use in class, but also available for everyone. The main function here is LexisMap(), which plots Lexis surfaces for data in AP squares (or AC or PC, with the appropriate orientation). That function uses lattice under the hood and its only advantage are some decent defaults for easy plotting of demographic surfaces. Won't do negative values though! There are also several Lexis matrix functions, AC2PC() etc etc, for convenience.

LexisSurface [NO LONGER MAINTAINED. I posted how to do this in Lattice and in ggplot2]. To make one easily in base graphics, follow the data prep steps outlined in the lattice post] is a package containing a function to plot demographic surfaces consisting in Lexis triangles. There are also 4 different functions available for splitting mortality Mx data from Lexis squares into Lexis triangles for purposes of plotting. These methods are untested; some will be improved, others dropped in the future. Will plot using any color ramp. Flexible legend sizing and positioning. The function plots both logged and not-logged data well. Includes example fertility and mortality data and several plotting examples. [still working on getting good legend labels- they are accurate but may overlap at times]. Also see the LexisMap() function in the LexisUtils package for square data.

HMDget is a function for reading Human Mortality Database data into R in different formats. You can merge countries and years, and output is available in a few different predefined formats. Database access either local or via the web (based on Carlo Boe's HMD2R function). Super awesome thanks to Felix Rößger and Adrien Remund for reporting bugs! Alas, the function got too out of hand to maintain, and I'll no longer be maintaining it. Look instead to the HMD grabber and reader functions found in the DemogBerkeley package (separate project I work on).

DecompHoriuchi offers the function DecompContinuous(), a generic decomposition program for a wide variety of functions, and no limits to the number of covariates they may have. Also includes an exact R copy of the Matlab function proposed by the authors, called here DecompContinuousOrig(), which takes arguments as a single long vector and is therefore a bit ore flexible.

Lotka, a small set of formal demography functions, including estimates of r (3 strategies), calculations of R0, T (mean generation time), age-survival-fertility decompositions of differences in r and R0, and a Kitagawa-ish decomposition of differences in R0. Examples are included, which use a dataset from Spain 1975 and 1998. See examples in help files. (added 7 March, 2011)  Several changes coming eventually.

RateSketch() is for hand-sketching demographic (or any) age-specific rate patterns. Just define the x and y limits and click the function from left to right. The function returns a list with the points you clicked, plus values interpolated to your desired x-values (argument = xnew) using loess, spline and linear methods. See examples. I've used this tons for generating fake data to practice other demographic functions on. Documentation to be improved- might need to do a video tutorial since it's hands-on...

LexisDiagram is a package with two functions for drawing Lexis diagrams (think presentations and teaching materials). Includes Lexis() and Thighlight(). Try the examples, they're easy.

LifeTable Contains a main function LT(), which does the whole basic lifetable spiel, taking either Nx and Dx or else Mx as it's basic arguments. This package also contains example data for Ukraine males, 1965, coming from the HMD, as well as 4 different a(x) estimation methods ("keyfitz", "schoen", "preston" and "midpoint"), all of which were modified by me in some minor way (apologies to the namesakes). See the documentation and examples for further details. It accepts data up to any age, in single-ages or five-year abridged data. Also optional smoothing (using Giancarlo's MortalitySmooth package). Returns all sorts of demographic age-functions and a few different measures. I'm thinking of including summary and plot methods in the next release version (no hurry though). This package is now hosted entirely on github, here: 
http://timriffe.github.io/LifeTable/ . There are installation instructions there, online help files, and that's a good place to report bugs, or make suggestions too.

Pyramid provides a simple wrapper with several defaults to quickly plot a population pyramid, and with simple detection and plotting of multistate pyramids. The function also gives optional absolute or percent scales, with flexible age-group widths, and optional generation labels on the right axis. This package is now hosted entirely on github, here: http://timriffe.github.io/Pyramid/ . There are installation instructions there, online help files, and that's a good place to report bugs, or make suggestions too. Recently added functions include a pyramid outline function, and a function for aggregating age-groups.

Random scripts:

Lotka1922 is not a package- it's the code used to replicate my blog entry from Jan 24th, 2011. This code refers to Lotka (1922) The Stability of the Normal Age Distribution PNAS (8)


I also try to be helpful with R questions on Stack Exchange:
profile for tim riffe at Stack Overflow, Q&A for professional and enthusiast programmers

And have a moderately sized but rapidly growing number of R code snippets (called Gists) on github.com:

packages and files attached below here may or may not be the latest versions, from now on I'll always leave up the old ones just in case, but in general get the most recent versions from the downloads page instead!
ċ
Tim Riffe,
Mar 3, 2011, 7:26 AM
ċ
DecompHoriuchi_1.1.tar
(50k)
Tim Riffe,
Aug 11, 2011, 4:46 PM
ċ
DecompHoriuchi_1.1.zip
(34k)
Tim Riffe,
Jul 22, 2011, 7:30 AM
ċ
EZLex_1.0.tar
(20k)
Tim Riffe,
Aug 11, 2011, 4:46 PM
ċ
EZLex_1.0.zip
(14k)
Tim Riffe,
Dec 17, 2010, 10:19 AM
ċ
GOL_Dens1000Dead.Rdata
(366k)
Tim Riffe,
Mar 1, 2012, 6:47 AM
ċ
GOL_Dens1000Wrap.Rdata
(370k)
Tim Riffe,
Mar 1, 2012, 10:03 AM
ċ
GOL_Parallel_Densities5000.Rdata
(890k)
Tim Riffe,
Feb 28, 2012, 6:22 AM
ċ
HMDget_1.0.zip
(18k)
Tim Riffe,
Jun 18, 2011, 10:32 AM
ċ
HMDget_1.21.zip
(21k)
Tim Riffe,
Jul 12, 2011, 11:08 AM
ċ
HMDget_1.22.zip
(21k)
Tim Riffe,
Jul 14, 2011, 1:00 AM
ċ
HMDget_1.24.tar
(50k)
Tim Riffe,
Aug 11, 2011, 4:47 PM
ċ
HMDget_1.24.zip
(21k)
Tim Riffe,
Aug 11, 2011, 5:47 AM
ċ
LexisSurface_1.0.zip
(60k)
Tim Riffe,
Jul 25, 2011, 8:06 PM
ċ
LexisSurface_1.1.tar
(320k)
Tim Riffe,
Aug 11, 2011, 4:47 PM
ċ
LexisSurface_1.1.zip
(275k)
Tim Riffe,
Jul 28, 2011, 12:05 PM
ċ
LifeTable_1.0.1.tar
(80k)
Tim Riffe,
Aug 12, 2011, 6:29 AM
ċ
LifeTable_1.0.1.zip
(58k)
Tim Riffe,
Aug 12, 2011, 6:29 AM
ċ
LifeTable_1.0.zip
(56k)
Tim Riffe,
Dec 27, 2010, 7:20 AM
ċ
Tim Riffe,
Jan 24, 2011, 1:55 AM
ċ
Lotka_1.0.zip
(46k)
Tim Riffe,
Feb 2, 2011, 11:00 AM
ċ
Lotka_1.1.zip
(58k)
Tim Riffe,
Mar 8, 2011, 9:07 AM
ċ
Pyramid_1.0.zip
(22k)
Tim Riffe,
Dec 31, 2010, 10:26 AM
ċ
Pyramid_1.1.tar
(40k)
Tim Riffe,
Aug 11, 2011, 4:48 PM
ċ
Pyramid_1.1.zip
(25k)
Tim Riffe,
Jul 12, 2011, 1:41 AM
ċ
RateSketch_1.0.zip
(22k)
Tim Riffe,
Dec 14, 2010, 4:06 AM
ċ
RateSketch_1.1.tar
(30k)
Tim Riffe,
Aug 11, 2011, 4:48 PM
ċ
RateSketch_1.1.zip
(17k)
Tim Riffe,
Jul 21, 2011, 7:01 AM
Comments