Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / Schoens_del.R
Last active August 28, 2015 12:08
calculate Schoen's del for the entire HMD
# Tim Riffe
# HT Andrew Noymer
# Link to original Schoen article (Demography, 1970):
# http://link.springer.com/article/10.2307/2060150
#library(devtools)
#install_github("timriffe/TR1/TR1/HMDHFDplus")
library(HMDHFDplus)
library(data.table)
@timriffe
timriffe / GSShappiness.R
Last active August 29, 2015 13:56
Make US male and female happiness surfaces using GSS data
# fun with GSS data, using ajdamico's code to read in and weight the data
# below see code for US male and female subjective wellbeing Lexis surfaces!
# sorry you'll need to change all file paths below to work with the data. I
# forgot to setwd() while playing
# ----------------------------------
# analyze survey data for free (http://asdfree.com) with the r language
# general social survey
# 1972-2012 cross-sectional cumulative data (release 1, march 2013)
@timriffe
timriffe / BendordHMD.R
Last active August 29, 2015 13:56
Testing HMD death counts for conormity with Benford's law
# 1) install DemogBerkeley package from github
library(devtools)
install_github("DemogBerkeley", subdir = "DemogBerkeley", username = "UCBdemography")
library(DemogBerkeley)
library(parallel)
# handy-dandy function to get a vector of the country codes:
CNTRIES <- getHMDcountries()
# ----------------------------------------
@timriffe
timriffe / aggN.R
Created March 20, 2014 17:55
aggN() aggregate vector of single age (year) data to N-year groups
#'
#' @title aggN aggregate vector of single age (year) data to N-year groups
#'
#' @description This only makes sense if N is 5 or 10 (or 2 I guess), since it uses modulo to find groups
#' Ages are assumed to start at 0 and count up in single ages.
#'
#' @param x the vector of single-age-classified data
#' @param N desired width of interval, e.g., 5 or 10
#'
#' @export
@timriffe
timriffe / FormalDemogADhoc.R
Last active August 29, 2015 13:57
a code chunk demonstrated in the 2014 Stanford formal demography workshop
# author: tim riffe
# March 24, 2014
# do with this as you will. If you sell it, I'l hunt you down.
# Thanks to Vladimir Canudas Romo for the idea!
# --------------------------------------------
# start
# This package is under development! please test!
# installation is via github. See the README for instructions
@timriffe
timriffe / JMD.R
Created March 31, 2014 06:29
Load and use JMD data in R, including plotting on a GADM basemap
# Author: Tim Riffe
# 30 March, 2014
###############################################################################
library(parallel) # used for mclapply()
library(sp) # basic spatial utils
library(reshape2) # used for acast()
library(compiler) # to make some function run faster
## if you don't have the DemogBerkeley package:
# library(devtools)
# install_github("DemogBerkeley", subdir = "DemogBerkeley", username = "UCBdemography")
@timriffe
timriffe / OutliveEx.R
Created April 28, 2014 16:52
The age-specific proportion of people that will outlive their remaining life expectancy
#library(devtools)
#install_github("DemogBerkeley", subdir = "DemogBerkeley", username = "UCBdemography")
library(DemogBerkeley)
# this will only work if you have an HMD username and password...
# but see the rest inc ase you want to try it on some other lifetable
username <- userInput() # do these one at a time, enter text into console
# this assigns the text to a variable without you
# having to save it in the script.
@timriffe
timriffe / thirtythree.R
Last active August 29, 2015 14:01
chances of living from 33 to 99
# see install instructions here:
# https://github.com/UCBdemography/DemogBerkeley
library(DemogBerkeley)
us <- userInput()
pass <- userInput()
mltper <- readHMDweb("USA","mltper_1x1",username = us, password = pass)
lx <- mltper$lx[mltper$Year == 2010]
lx <- lx / 1e5
@timriffe
timriffe / CroatiaFert.R
Created July 19, 2014 21:27
Deriving chronological and thanatological fertility rates from Croatian linked historical demographic data
# download linked data from here:
# data from http://www.demog.berkeley.edu/croatia/datadir/Linkfiles/linkfiles.htm
# add path to where data is, or set working directory
CRO <- read.table("croatdata4.txt.gz",
stringsAsFactors = FALSE, sep = "\t", col.names = c('bid','bdate','sex','motherid','fatherid','mid1','mid2','mid3','mid4','mid5',
'sid1','sid2','sid3','sid4','sid5','mdate1','mdate2','mdate3','mdate4','mdate5','did',
'ddate','idk1','idk2','idk3','idk4','idk5','idk6','idk7','idk8','idk9','idk10','idk11',
'idk12','idk13','idk14','dobk1','dobk2','dobk3','dobk4','dobk5','dobk6','dobk7','dobk8','dobk9',
'dobk10','dobk11','dobk12','dobk13','dobk14','sidk1','sidk2','sidk3','sidk4','sidk5','sidk6','sidk7',
@timriffe
timriffe / EigenSonification.R
Created July 24, 2014 12:49
Sonification of Leslie eigenvectors
# written 24 July, 2014 for fun, hope I didn't leave anything out.
# If you can get stuff installed, this should run on its own
# first part copied from Fischer.R, sonifaction code after
# change this as needed:
setwd("/home/triffe/git/TimeProposal/TimeLab")
# -----------------------------------------------------
# Utility functions.