Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / R_Jags_AmazonEc2_Setup.sh
Last active October 28, 2017 20:26
Demonstration script for setting up R, Git, and Jags on Ubuntu instance of Amazon EC2 Server
# Set up RStudio and JAGS on an Amazon EC2 instance
# Using Ubuntu 64-bit
# Partially from http://blog.yhathq.com/posts/r-in-the-cloud-part-1.html
# See yhat for EC2 instance set up
# Navigate to key pair
# ssh -i YOUR_KEYPAIR.pem ubuntu@PUBLIC_DNS
# Add a user/password.
# This will become your RStudio username and password.
@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 / 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 / beta_plot_basic.R
Created June 13, 2017 17:28
Simple function for simulating/plotting beta distributions for Dataverse 2017 Package Development Workshop
#' @import ggplot2
#' @export
beta_plot <- function(n = 10000, a = 1, b = 3) {
# draw distributions
sims <- rbeta(n = n, shape1 = a, shape2 = b)
# convert to data frame for ggplot2 compatability
sims <- data.frame(x = sims)
---
title: "The zeligverse and beyond"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
[for Zelig version 5.1.1.9000, [setdirect branch](https://github.com/IQSS/Zelig/tree/setxdirect)]
@christophergandrud
christophergandrud / rrst_to_rmd.R
Last active May 8, 2017 17:32
Convert an R reStructuredText document (Rrst) to R Markdown (Rmd)
#' Converts an R reStructuredText document (Rrst) to R Markdown (Rmd)
#'
#' @param input character string with the path of the file to convert
#' @param output optional character string with the path for the output file
#' @param out_dir optional character string with the output directory. Not used
#' if \code{output} is supplied.
#' @param output_file logical whether to return the output to a file or not.
#' If \code{FALSE}, then \code{output} and \code{out_dir} are ignored.
#'
#' @importFrom readr read_file write_file
@christophergandrud
christophergandrud / parse.formula.R
Last active March 22, 2017 14:03
parse.formula function from Zelig 3.5
parse.formula<-function( formula, model,data=NULL){
if(class(formula)[[1]]=="multiple")
return(formula)
nrUserOpt<-nrUserReq<-nrUserFixed<-nrUserSubreq<-0
userOpt<-userReq<-userFixed<-userSubreq<-list()
fc <- paste("describe.", model, sep = "")
if (!exists(fc))
stop("describe.",model," does not exsist")
modelReq<-do.call(fc,list())
@christophergandrud
christophergandrud / d3SimpleNetwork.R
Last active March 7, 2017 07:28
An R function for creating simple D3 javascript directed network graphs.
#' An R function for creating simple D3 javascript directed network graphs.
#'
#' d3SimpleNetwork creates simple D3 javascript network graphs.
#'
#' @param data a data frame object with three columns. The first two are the names of the linked units. The third records an edge value. (Currently the third column doesn't affect the graph.)
#' @param Source character string naming the network source variable in the data frame. If \code{Source = NULL} then the first column of the data frame is treated as the source.
#' @param Target character string naming the network target variable in the data frame. If \code{Target = NULL} then the second column of the data frame is treated as the target.
#' @param height numeric height for the network graph's frame area.
#' @param width numeric width for the network graph's frame area.
#' @param file a character string of the file name to save the resulting graph. If a file name is given a standalone webpage is created, i.e. with a header and footer. If \code{file = NULL} then
@christophergandrud
christophergandrud / gist:1287427
Created October 14, 2011 15:26
Google Motion Chart of World Bank External Debt Data
# Create Google Motion Chart from World Bank Finance Data
# Inspired by http://lamages.blogspot.com/2011/09/accessing-and-plotting-world-bank-data.html
# Written by Christopher Gandrud
# 15 October 2011
library(WDI)
library(gregmisc)
library(googleVis)
# Initialize Zelig5 least squares object
z5 <- zls$new()
# Estimate ls model
z5$zelig(Fertility ~ Education, data = swiss)
# Simulate quantities of interest
z5$sim()
# Plot quantities of interest