Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / ggs_caterpillar_label.R
Last active August 29, 2015 13:57
Modified version of ggs_caterpillar function from Xavier Fernández i Marín's ggmcmc R package
#' Caterpillar plot with thick and thin CI
#'
#' Caterpillar plots are plotted combining all chains for each parameter.
#'
#' @param D Data frame whith the simulations or list of data frame with simulations. If a list of data frames with simulations is passed, the names of the models are the names of the objects in the list.
#' @param X data frame with two columns, Parameter and the value for the x location. Parameter must be a character vector with the same names that the parameters in the D object.
#' @param family Name of the family of parameters to plot, as given by a character vector or a regular expression. A family of parameters is considered to be any group of parameters with the same name but different numerical value between square brackets (as beta[1], beta[2], etc).
#' @param thick_ci Vector of length 2 with the quantiles of the thick band for the credible interval
#' @param thin_ci Vector of length 2 with the quantiles of the thin band for the credible interval
#' @param line Numerical value in
@christophergandrud
christophergandrud / ggs_summary.R
Created March 24, 2014 10:40
Extract parameter estimates from ggs data frames.
#' Extract parameter point estimates
#'
#' @param D Data frame with the simulations or list of data frame with simulations. If a list of data frames with simulations is passed, the names of the models are the names of the objects in the list.
#' @param family Name of the family of parameters to plot, as given by a character vector or a regular expression. A family of parameters is considered to be any group of parameters with the same name but different numerical value between square brackets (as beta[1], beta[2], etc).
#' @param thick_ci Vector of length 2 with the quantiles of the thick band for the credible interval.
#' @param thin_ci Vector of length 2 with the quantiles of the thin band for the credible interval.
#' @param param_label_from Vector of strings (can be regular expressions) for the original paramater labels that you would like to replace in the plot lables using \code{param_label_to}.
#' @param param_label_to Vector of strings for paramater labels you would like to use. Must be both the sam
@christophergandrud
christophergandrud / svTransform.R
Created July 22, 2014 10:29
Simple function to transform a dependent variable that in [0,1] rather than (0, 1) to beta regression. Suggested by Smithson & Verkuilen (2006).
#' Simple function to transform a dependent variable that in [0,1] rather than
#' (0, 1) to beta regression. Suggested by Smithson & Verkuilen (2006).
#'
#' @param y vector of the dependent variable that is in [0, 1].
svTransform <- function(y)
{
n <- length(y)
transformed <- (y * (n-1) + 0.5)/n
return(transformed)
@christophergandrud
christophergandrud / utf8_convert.R
Created October 13, 2014 14:23
Convert text files in a directory to UTF-8 encoding (http://en.wikipedia.org/wiki/UTF-8)
#' Convert text files in a directory to UTF-8 encoding
#'
#' @param dir a character string naming the directory where the files are located.
#' @param new_dir a character string naming the directory where you would like the
#' converted files to be placed.
#'
#' @importFrom dplyr %>%
utf8_convert <- function(dir, new_dir){
all_files <- list.files(dir)
@christophergandrud
christophergandrud / github_user_counts.R
Created October 20, 2014 13:40
Downloads Counts of GitHub users with more than 100 followers for three major locations
########################################
# Download GitHub users with more than 100 followers for Berlin, Brooklyn,
# and London
# Christopher Gandrud
# 20 October 2014
########################################
library(httr)
library(dplyr)
library(rjson)
@christophergandrud
christophergandrud / WorldBankData_noNA.csv
Last active August 29, 2015 14:12
Work in progress example of a D3 line chart
country iso2c year credit
Austria AT Jan 2007 100
Austria AT Jan 2008 102.9
Austria AT Jan 2009 109.8
Austria AT Jan 2010 108.6
Austria AT Jan 2011 107.1
Austria AT Jan 2012 106.1
Austria AT Jan 2013 103.8
Belgium BE Jan 2007 100
Belgium BE Jan 2008 101.5
@christophergandrud
christophergandrud / set_valid_wd.R
Created June 2, 2015 13:48
Sets valid working directory from a vector of possible directories. Useful if you run a script on multiple computers
#' Sets valid working directory from vector of possible directories
#'
#' @param character vector of possible working directores
set_valid_wd <- function(possible) {
for (i in possible) {
if (file.exists(i)) {
setwd(i)
message(sprintf('Working directory set as: %s', i))
}
@christophergandrud
christophergandrud / range01.R
Last active August 29, 2015 14:23
Rescale a vector to be within 0 and 1
#' Rescale a vector to be within 0 and 1
#'
#' @param x a numeric vector to rescale
#' @param na.rm logical. Whether or not to remove NA (missing) values
range01 <- function(x, na.rm = T){
(x - min(x, na.rm = na.rm)) /
(max(x, na.rm = na.rm) - min(x, na.rm = na.rm))
}
#' Convert a vector into a matrix with two columns
#'
#' @param x a vector
equal_columns <- function(x) {
round2 = function(x, digits) {
posneg = sign(x)
z = abs(x)*10 ^ digits
z = z + 0.5
z = trunc(z)
@christophergandrud
christophergandrud / gist:1284498
Last active September 27, 2015 14:28
Simple Web Crawler for Text
# Load RCurl package
library(RCurl)
addresses <- read.csv("~/links.csv") # Create a .csv file with all of the links you want to crawl
for (i in addresses) full.text <- getURL(i)
text.sub <- gsub("<.+?>", "", full.text) # Removes HTML tags
text <- data.frame(text.sub)