This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################## | |
# Download GitHub users with more than 100 followers for Berlin, Brooklyn, | |
# and London | |
# Christopher Gandrud | |
# 20 October 2014 | |
######################################## | |
library(httr) | |
library(dplyr) | |
library(rjson) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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])) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################## | |
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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)) | |
} |