Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / MT_COlatz.R
Created March 31, 2012 23:55
Model Thinking- Coordination and Culture- Colatz Plot
# This code does the Colatz problem- not that difficult to implement.
# 'number' is the given number, i is the number of steps. Beware, doing
# this for all numbers from 1 to a million can take a long time! If you reduce
# 1e6 to 1e4 you still get a pretty plot!
Colatz <- function(number){
i <- 0
while(number != 1){
i <- i + 1
number <- ifelse(number %% 2 == 0, {number / 2}, {3 * number + 1})
@timriffe
timriffe / AxelrodCulture.R
Created April 6, 2012 03:41
Model Thinking- Coordination and Culture- Axelrod emergence of culture
# Axelrod culture formation
# Author: triffe
###############################################################################
# here are 2 functions to do the Axelrod culture emergence simulation
# they work like this:
# you have a matrix of person IDs, IDmat
# each person has a certain number of traits and a certain number of possible values for those traits
# in each iteration, each person looks up, down, left, right. calculate the proportion similar for each
# neighbor, and choose a neighbor randomly, but with probabilities given by the proportion similar. I.e.
# if you're different on everything, you don't pick that person- if you're same then you probably do: this is
@timriffe
timriffe / MT_Polya.R
Created April 7, 2012 16:21
Model Thinking- Path Dependence- Polya urn model
# Author: triffe
###############################################################################
# the system will keep progressing until the change in the distribution from one period to another is < tol
# or until max iterations is met
# x must be logical or character
Polya <- compiler:::cmpfun(function(x, tol = .0001, maxit = 1e5){
if (!(is.logical(x) | is.character(x))){
stop("specify x as a logical or character vector")
@timriffe
timriffe / MT_TentMap.R
Created April 9, 2012 02:26
Model Thinking- Path Dependence- Tent Map
# Author: triffe
###############################################################################
TentMap <- compiler:::cmpfun(function(x, steps = 1e3+1){
for (i in 1:steps){
(x <- ifelse(x <= .5,{2 * x},{2 - 2 * x}))
}
x
})
@timriffe
timriffe / MatrixProportionPlots.R
Created June 3, 2012 20:09
Matrix Proportion Plots
# plots inspired by Cox, 1970, Sex Differences in Age at marriage
# this conveys absolute numbers or proportions (converted to proportions internally)
MatrixProportionPlot <- function(Mat, ...){
inner.sides <- as.vector(sqrt(Mat / sum(Mat)))
x <- as.vector(col(Mat))
y <- as.vector(row(Mat))
plot(NULL, type = "n", xlim = c(0, ncol(Mat)),ylim = c(0, nrow(Mat)), axes = FALSE, asp = TRUE,
xlab = "", ylab = "")
@timriffe
timriffe / StarPlot.R
Created June 3, 2012 20:17
Star plots
# strange ternary plots for proportion cohabiting by educational group.
# triplot(), a function to 2 a 3-axis version of that funky graphic in the Economist:
# arguments:
# center: a pairlist or named vector with elements "x" and "y", i.e. c(x = 0, y = 0)
# cex: expansion factor. By default, spokes are 1 unit long. You can either arbitrarily resize the plot area or
# resize these data icons to fit.
# levs: a vector of length 3: the 3 proportions (between 0 and 1- if in %, divide by 100 first) cohabiting (or whatever).
# These are plotted counter-clockwise over the spokes: top, lower left, lower right. Arrange data accordingly:
# spoke.col: the color of the axis spokes and ticks.
@timriffe
timriffe / PolarImagePlot.R
Created June 5, 2012 19:41
PolarImagePlot()
# by Tim Riffe, June 5th, 2012
# arguments:
# Mat, a matrix of z values as follows:
# leftmost edge of first column = 0 degrees, rightmost edge of last column = 360 degrees
# columns are distributed in cells equally over the range 0 to 360 degrees, like a grid prior to transform
# first row is innermost circle, last row is outermost circle
# outer.radius, By default everything scaled to unit circle
# cols: color vector. default = rev(heat.colors(length(breaks)-1))
@timriffe
timriffe / PolarInterpolatePlot.R
Created June 8, 2012 05:33
PolarInterpolatePlot
PolarImageInterpolate <- function(x, y, z, outer.radius = 1,
breaks, col, nlevels = 20, contours = TRUE, legend = TRUE,
axes = TRUE, circle.rads = pretty(c(0,outer.radius))){
minitics <- seq(-outer.radius, outer.radius, length.out = 1000)
# interpolate the data
Interp <- akima:::interp(x = x, y = y, z = z,
extrap = TRUE,
@timriffe
timriffe / StableMarriage.R
Created June 11, 2012 07:20
Stable Marriage Problem with Gale-Shapley algorithm
# Author: triffe
###############################################################################
# Stable marriage problem using the Gale-Shapely algorithm:
# example data below, and the motivation to check perturbations for instability from:
# http://rosettacode.org/wiki/Stable_marriage_problem
MakeMatches <- function(males, females, m.prefs, f.prefs, maxit = 1e3){
MatchMat <- matrix(0, ncol = length(males), nrow = length(females), dimnames = list(females, males))
@timriffe
timriffe / TuftePyramid.R
Last active December 10, 2015 23:19
Tufte-like Population Pyramid for R. Still figuring out how to sort out ellipsis args between functions. Example at bottom.
TuftePyramid <- function(males, females, age, widths, gap = .05,
fill.args = list(), border.args = list(), grid.args = list(),
age.label.args = list(), x.label.args = list(),
grid = TRUE, labels = TRUE, add = FALSE){
Total <- sum(males, females)
males <- males / Total
females <- females / Total
max.x <- max(abs(pretty(c(males, females), n = 25)))