Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / stan_caterpillar.R
Last active October 24, 2017 22:25
Simple function to create caterpillar plots from rstan's stanfit objects
#' A function to create caterpillar plots from rstan's stanfit objects
#'
#' @param obj a \code{stanfit} object
#' @param pars character string, vector, or regular expression of paramater
#' labels that you would like to plot as declared in \code{model_code} from the
#' \code{\link{stan}} call.
#' @param pars_labels vector of parameter labels. Important: they must be in
#' the same order as in the \code{stanfit} object when \code{as.data.frame(obj)}
#' is called.
#' @param hpd logical. If \code{TRUE} then the 90% and 95% highest probability
@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 / source_lines.R
Created December 1, 2014 14:08
Source specific lines from a file in R
#' Source specific lines in an R file
#'
#' @param file character string with the path to the file to source.
#' @param lines numeric vector of lines to source in \code{file}.
source_lines <- function(file, lines){
source(textConnection(readLines(file)[lines]))
}
@christophergandrud
christophergandrud / r_stan_setup.sh
Last active October 31, 2024 18:29
Setup R, RStudio,and Stan on Ubuntu (for Amazon EC2)
########################################################
# Set up RStudio and JAGS on an Amazon EC2 instance
# Using Ubuntu 64-bit
# Christopher Gandrud
# 16 December 2014
# Partially from http://blog.yhathq.com/posts/r-in-the-cloud-part-1.html
# See yhat for EC2 instance set up
########################################################
# In your terminal navigate to key pair
@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 / ihs.R
Last active July 12, 2017 11:03
Inverse hyperbolic sine transformation
#' Inverse hyperbolic sine transformation
#'
#' @param x a numeric vector
#'
#' @details A useful transformation for skewed data that includes 0's and
#' negative values
ihs <- function(x) {
transformed <- log(x + sqrt(x ^ x + 1))
return(transformed)
@christophergandrud
christophergandrud / topicmodels_json_ldavis.R
Last active March 13, 2021 19:00
Convert the output of a topicmodels Latent Dirichlet Allocation model to JSON for use with LDAvis
#' Convert the output of a topicmodels Latent Dirichlet Allocation to JSON
#' for use with LDAvis
#'
#' @param fitted Output from a topicmodels \code{LDA} model.
#' @param corpus Corpus object used to create the document term
#' matrix for the \code{LDA} model. This should have been create with
#' the tm package's \code{Corpus} function.
#' @param doc_term The document term matrix used in the \code{LDA}
#' model. This should have been created with the tm package's
#' \code{DocumentTermMatrix} function.
@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))
}