Skip to content

Instantly share code, notes, and snippets.

View tyleransom's full-sized avatar

Tyler Ransom tyleransom

View GitHub Profile
@tyleransom
tyleransom / mfxExample.do
Last active December 22, 2016 21:07
How to compute counterfactual predictions in Stata
sysuse auto, clear
logit foreign i.rep78 c.price c.mpg
predict base if e(sample), pr
preserve
clonevar oldrep78 = rep78
replace rep78 = 4
predict new if e(sample), pr
generat mfx = new-base
replace rep78 = oldrep78
tab rep78 , sum(mfx)
@tyleransom
tyleransom / JuMPexampleProfiler.jl
Created March 6, 2017 21:27
Evaluate memory usage in Julia's JuMP optimizing language
using JuMP
using Ipopt
function datagen(N::Int64,T::Int64)
## Generate data for a linear model to test optimization
srand(1234)
# N = convert(Int64,N) #inputs to functions such as -ones- need to be integers!
# T = convert(Int64,T) #inputs to functions such as -ones- need to be integers!
n = N*T
@tyleransom
tyleransom / compile.linux
Created August 25, 2017 13:52
FORTRAN open-source version of sort_qq
#!/bin/bash
# GNU Fortran (gfortran) compiler, OpenBLAS BLAS and LAPACK libraries
gfortran -Wall -ffree-form -ffree-line-length-256 -o ~/ado/plus/r/rcr rcrlib_gnu.f90 rcrutil_gnu.f90 rcr.f90 -L/~/lib/OpenBLAS -lopenblas
rm *.mod
@tyleransom
tyleransom / nlf.R
Created December 30, 2017 21:34 — forked from paulgp/nlf.R
code for NFL analysis
pbp_2016 <- read_csv("~/Downloads/pbp_2016.csv")
a <- pbp_2016 %>% group_by(GameID, posteam) %>% summarize(top = sum(PlayTimeDiff), end_score = max(PosTeamScore, na.rm=TRUE), numplays = n()) %>% filter(posteam != "NA") %>% group_by(GameID) %>% mutate(teamNum = row_number()) %>% select(GameID, top, numplays, teamNum, end_score)
score <- a %>% select(GameID, teamNum, end_score) %>% spread(teamNum, end_score, sep="_")
numplays <- a %>% select(GameID, teamNum, numplays) %>% spread(teamNum, numplays, sep="_")
top <- a %>% select(GameID, teamNum, top) %>% spread(teamNum, top, sep="_")
colnames(top) <- c("GameID", "top_1", "top_2")
colnames(score) <- c("GameID", "score_1", "score_2")
colnames(numplays) <- c("GameID", "numplays_1", "numplays_2")
final <- inner_join(numplays,score, by="GameID") %>% inner_join(top, by="GameID") %>% mutate(winner_1 = as.integer(score_1 > score_2))
@tyleransom
tyleransom / vim_crash_course.md
Created December 30, 2017 21:44 — forked from dmsul/vim_crash_course.md
Vim Crash Course

NOTE: Specific examples given for options, flags, commands variations, etc., are not comprehensive.

NORMAL MODE

Vim has 2 main "modes", that chance the behavior of all your keys. The default mode of Vim is Normal Mode and is mostly used for moving the cursor and navigating the current file.

Some important (or longer) commands begin with ":" and you will see the text you enter next at the bottom left of the screen.

:q[uit] - quit (the current window of) Vim. ("Window" here is internal to Vim, not if you have multiple OS-level windows of Vim open at once.)
:q! - force quit (if the current buffer has been changed since the last save)
:e[dit] {filename} - read file {filename} into a new buffer.

Rosetta Stone

This consists of a "Rosetta Stone" of code syntax between R, Python, and Julia. The aim is to help those who are proficient in one language easily translate their code/understanding to another.

Basic actions (REPL, package ecosystem, etc.)

Action R code Python code Julia code
execute script from REPL source('script.R') execfile('script.py') include("script.jl")
install a package install.packages("pkg") [from OS terminal]: pip install pkg Pkg.add("pkg")
load a package library(pkg) or require(pkg) import pkg using pkg
@tyleransom
tyleransom / optimexample.jl
Last active June 22, 2018 14:13
Optimize memory usage in Julia
using Optim, NLSolversBase
function datagen(n=5500,k=13)
b = rand(k)
l = rand(2)
x = randn(n,k)
abil = randn(n,1)
vabil = randn(n,1)
sector = 1.0.*(rand(n,1).>0.8)
weight = rand(n,1)
using BenchmarkTools
using Optim, NLSolversBase
function datagen(n=5500,k=13)
b = rand(k)
l = rand(2)
x = randn(n,k)
abil = randn(n,1)
vabil = randn(n,1)
sector = 1.0.*(rand(n,1).>0.8)
@tyleransom
tyleransom / normalMLE.jl
Last active August 3, 2018 16:43
Compare run time of Julia and Matlab optimization for simple Maximum Likelihood problem (normal linear model)
function normalMLE(b::Vector,Y::Array,X::Array,w::Array=ones(size(Y)),d::Array=ones(size(Y)),J::Int64=1)
@assert size(Y,1)==size(X,2) "X and Y must be the same length"
T = promote_type(eltype(b), eltype(X))
like = zero(T)
@inbounds for i=1:size(Y,1)
xb = zero(T)
for j in 1:size(X,1)
xb += X[j, i] * b[j]
end
@tyleransom
tyleransom / jump_mwe.jl
Last active December 9, 2018 03:51
MWE for linear programming problem
using DataFrames
using HTTP
using uCSV
using JuMP
using GLPK
using Cbc
# read in the data
fname = "https://gist.githubusercontent.com/tyleransom/3738fa6cd1b3b5602e0621b08d068380/raw/5c4b139639d7b3c16716289b0232b8528d1bf494/mwe.csv"
data = DataFrame(uCSV.read(IOBuffer(HTTP.get(fname).body), quotes='"', header=1))