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.

@tyleransom
tyleransom / NppVimCrosswalk.md
Last active February 29, 2024 17:10
Crosswalk between Notepad++ and Vim utilities

Crosswalk between Notepad++ and Vim utilities

Action Notepad++ shortcut Vim shortcut
Duplicate line Ctrl+D yyp
Cut line Ctrl+L dd
Move line up/down Ctrl+Shift+<up/down> dd<k/j>p
Compare files File compare plugin wizard :vsplit <filename> or vimdiff file1 file2 (from command line)
Select all (from here to top/bottom of file) Ctrl+Shift+<home/end> v<gg/G>
Select all (from here up/down one page) Ctrl+Shift+ v Ctrl+
@tyleransom
tyleransom / gcc-5.4.0-install.sh
Last active January 11, 2024 05:47 — forked from jdhao/gcc-5.4.0-install.sh
The script will install GCC 5.4.0 on your CentOS 7 system, make sure you have root right.
echo "Downloading gcc source files..."
curl https://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2 -O
echo "extracting files..."
tar xvfj gcc-5.4.0.tar.bz2
echo "Installing dependencies..."
yum install gmp-devel mpfr-devel libmpc-devel
echo "Configure and install..."

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)