Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / leg_violence_predict.R
Created April 26, 2012 21:29
Predict probability graphs with zelig and ggplot2
####################
# Create relogit predicted probabilities using Zelig and ggplot2
# Two Sword Lengths: Losers' Consent and Violence in National Legislatures (Working Paper 2012)
# Christopher Gandrud
# Updated 26 April 2012
###################
## Load required packages
library(RCurl)
library(Zelig)
@christophergandrud
christophergandrud / relativeNPLGraph.R
Created October 4, 2012 04:28
Relative Non-proportional Hazard Graph in R
#################
# Use R to recreate part of QMV the relative non-proportional hazard estimation graph (fig 2) from Licht (2011)
# Christopher Gandrud (http://christophergandrud.blogspot.com/)
# 4 October 2012
#################
#### Set Up ####
# Load required libraries
@christophergandrud
christophergandrud / CompareRelativeHazards.R
Created October 4, 2012 09:08
Compare Relative Non-Proportional Hazards
##################
# Compare relative non-proportional hazards
# Christopher Gandrud
# 4 October 2012
##################
# Load required libraries
library(survival)
library(MSBVAR)
library(reshape)
###############
# NPL Relative Hazard 2
# Christopher Gandrud
# 6 October 2012
###############
# Load required libraries
library(foreign)
library(survival)
library(MSBVAR)
@christophergandrud
christophergandrud / Middle95HR.R
Created December 30, 2012 07:27
Update to example of graphing non-proportional hazard ratios in R. The update keeps only the middle 95% of the simulated hazard ratios. The original example is [HERE](http://christophergandrud.blogspot.com/2012/10/graphing-non-proportional-hazards-in-r.html)
#################
# Use R to recreate part of QMV the relative non-proportional hazard estimation graph (fig 2) from Licht (2011)
# Updated to keep only the middle 95% of simulations
# Christopher Gandrud (http://christophergandrud.blogspot.com/)
# 30 December 2012
#################
#### Set Up ####
@christophergandrud
christophergandrud / source_GitHubData.R
Last active March 31, 2020 04:58
A function for downloading data stored on GitHub in a plain-text format (e.g. CSV, TSV) into R. The function loads the data as a data frame. For more details see: http://christophergandrud.blogspot.com/2013/01/sourcegithubdata-simple-function-for.html.
#####################
# R function for downloading plain-text data from GitHub
# Christopher Gandrud
# 7 January 2013
#####################
# source_GitHubData is directly based on source_url from the Hadley Wickham's devtools package
source_GitHubData <-function(url, sep = ",", header = TRUE)
{
@christophergandrud
christophergandrud / LoadandCite.R
Created January 8, 2013 09:56
LoadandCite: a function for installing, loading, and creating a BibTeX bibliography of R packages.
###############
# LoadandCite: a function for installing, loading, and creating a BibTeX bibliography of R packages.
# Christopher Gandrud
# 9 January 2013
##############
# This function is partially based on: https://gist.github.com/3710171
LoadandCite <- function(packages, file = "", install = FALSE)
{
@christophergandrud
christophergandrud / FillIn.R
Last active June 20, 2022 11:59
FillIn: an R function for filling in missing values of a variable from one data frame with the values from another variable. The function also prints the correlation coefficient between the two variables. You will need to have the data.table package installed to run FillIn.
#' An R function for filling in missing values of a variable from one data frame with the values from another variable.
#'
#' \code{FillIn} uses values of a variable from one data set to fill in missing values in another.
#'
#' @param D1 the data frame with the variable you would like to fill in.
#' @param D2 the data frame with the variable you would like to use to fill in \code{D1}.
#' @param Var1 a character string of the name of the variable in \code{D1} you want to fill in.
#' @param Var2 an optional character string of variable name in \code{D2} that you would like to use to fill in.
#' @param KeyVar a character vector of variable names that are shared by \code{D1} and \code{D2} that can be used to join the data frames.
#'
@christophergandrud
christophergandrud / stataDo.R
Created April 18, 2013 01:07
the `stataDo` function does not currently work. More of an aspiration than a reality.
############
# stataDo: A function for running Stata do files in batch mode from R
# Christopher Gandrud
# 22 January 2013
############
# Note: Stata must be in your PATH for this to work.
## For Mac Users see: http://www.stata.com/support/faqs/mac/advanced-topics/
stataDo <- function(dofile, options = "-e"){
@christophergandrud
christophergandrud / quarter_year.R
Created May 2, 2013 07:40
Find the quarter or year + quarter of a date in R. The function is a modified version of quarter from lubridate.
quarter_year <- function(x, with_year = FALSE) {
require(lubridate)
m <- month(x)
quarters <- c("1" = 1,
"2" = 1,
"3" = 1,
"4" = 2,
"5" = 2,
"6" = 2,
"7" = 3,