Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / e.divGG.R
Last active March 31, 2020 04:58
Function for creating nonparamentric multiple change point plots with estimates from the ecp package.
#' Function for creating nonparamentric multiple change point plots with
#' estimates from the ecp package.
#'
#' @param data A data frame with the covariates and time variable.
#' @param Vars A character vector listing the names of the variates from
#' \code{data} to include in the nonparametric multiple change point analysis.
#' @param TimeVar A character string naming the time variable in \code{data}.
#' Must be in a format handled by POSIX, e.g. YYYY-MM-DD.
#' @param sig.lvl The level at which to sequentially test if a proposed change
#' point is statistically significant.
@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 / create_lags_diffs.R
Created March 28, 2020 17:05
Create lag and difference variables for a grouped time series (grouped by a variable called "country")
# Create lag and difference variables for a grouped time series (grouped by a variable called "country")
#
# @param df a data frame containing the variable to lag and lead
# @param x a column name in `df` to lag and lead
# @param lag integer period to lag and difference
#
# @importFrom tsibble difference
# @importFrom dplyr %>% lag
create_lags_diffs <- function(df, x, lag = 1) {
@christophergandrud
christophergandrud / simulate_power_distributed_discrete.R
Created January 12, 2020 10:56
Simulate data from discrete power law distribution in parallel
library(xfun)
pkg_attach2("tidyverse", "poweRlaw", "furrr")
plan(multiprocess)
# Simulate from a discrete power law
# @param counter numeric, to enable mapping
# @param n integer, number of draws
# @param xmin numeric, minimum value subject to power law
# @param alpha, scaling parameter
Y X Z A
1.41349020936189 Signup 3.76363446527701 0
-1.19511457323204 No Signup -3.81402465182499 1
1.87668885146666 Signup 0.712012451131932 1
1.8385401566156 No Signup -0.774724331043617 1
3.02728268507929 Signup 3.25513469702395 1
2.88424256922274 Signup -0.863068694934284 1
-0.387965283366912 No Signup -2.59568803608236 0
0.577546833602056 Signup -0.277480406497884 0
2.72670763084368 Signup 0.181542158647839 1
@christophergandrud
christophergandrud / r_stan_setup.sh
Last active September 15, 2018 19:51
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 / poisson_mle_sims_bias_comp.R
Created August 13, 2018 15:16
Compare bias in Poisson MLE and various Sim methods (10 August)
library(pacman)
p_load(MASS, tidyverse)
# set seed
set.seed(4321)
# set simulation paramters
n <- 100 # sample size
@christophergandrud
christophergandrud / transformation_bias_blog.R
Last active August 9, 2018 04:30
Replication material for
library(pacman)
p_load(MASS, tidyverse)
# set seed
set.seed(8764)
# set simulation paramters
n <- 100 # sample size
@christophergandrud
christophergandrud / quick_mod_to_intuition_plot.R
Last active August 8, 2018 07:28
Quick modification to Rainey and Kern Figure 2 to remove transformation bias https://github.com/carlislerainey/unnecessary/blob/master/R/intuition-sims.R
set.seed(19743)
library(tidyverse)
library(ggrepel)
library(forcats)
tau <- function(x) {
x^2
}
@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)