Skip to content

Instantly share code, notes, and snippets.

View dantonnoriega's full-sized avatar

Danton Noriega-Goodwin dantonnoriega

View GitHub Profile
@dantonnoriega
dantonnoriega / Main.sublime-menu
Last active November 15, 2017 21:35
SublimeREPL ipy_repl.py hack to work with Anaconda + IPython 6.1.0 + Python 3.6 for macOS
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[
@dantonnoriega
dantonnoriega / tmux-vim-repl.md
Last active October 28, 2021 06:58
Step by step instruction to set up a VIM REPL with TMUX on a linux box. I found most instruction onlin convoluted and confusing.

Set up tmux with a vim REPL on a linux box

  1. install https://github.com/tpope/vim-pathogen into ~/.vim/
  2. install https://github.com/jpalardy/vim-slime into ~/.vim/bundle
  3. copied default vimrc to a user copy: cp /etc/vimrc ~/.vim/vimrc
  4. added the following to the end of ~/.vim/vimrc
" vim slime
let g:slime_python_ipython = 1

@dantonnoriega
dantonnoriega / sublime-r-section-move.py
Created December 20, 2017 22:36
sublime text plugin to move forward or backward to a given pattern. in this case, i search for "headers" defined as strings "####" or "# TEXT ---------". mostly designed for R.
import sublime, sublime_plugin
class MoveForwardToSectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
s = v.sel()
pattern = "^[ ]*# [#]{4,}|^[ ]*[#]{4,}|^[ ]*#+ [\w]+.+[-]+$|^`{3}"
# get current point and then look forward
@dantonnoriega
dantonnoriega / sublime-section-expands.py
Last active December 21, 2017 21:31
sublime text script to create two new expand commands: to end of file and expand to section. a section is defined by a header using the Rstudio syntax '# SECTION TITLE ----------' or anything with 4 hashes '####'. flexible with comment sections as well.
import sublime, sublime_plugin
class ExpandSelectionToEofCommand(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
s = v.sel()
eof = v.size()
first_point = v.line(s[0]).a
region_to_eof = sublime.Region(first_point, eof)
@dantonnoriega
dantonnoriega / major_holidays_2000_2025.csv
Last active January 26, 2018 01:03
list of some major holidays from 2000 - 2025. not expansive but hypothesis is that these dates correlate with high technology use.
year date holiday
2000 2000-01-01 New Year's Day
2000 2000-02-05 Chinese New Year
2000 2000-02-14 Valentine's Day
2000 2000-04-23 Easter Sunday
2000 2000-05-14 Mother's Day
2000 2000-06-18 Father's Day
2000 2000-07-04 Independence Day
2000 2000-10-31 Halloween
2000 2000-11-23 Thanksgiving Day
# a running list of really useful data.table tricks
# TOP 1,2 ... last row by some group id
## source: https://stackoverflow.com/questions/16325641/is-it-possible-to-extract-the-first-2-rows-for-each-date#comment23381259_16325932
## comment by @eddi
id <- c('date', 'userid')
dt[dt[, .I[1:2], by = id]$V1] # first 2 rows by id
dt[dt[, .I[.N], by = id]$V1] # last row by id (.N = length of group, .I = row index)
@dantonnoriega
dantonnoriega / tweedie-simulations-with-dispersion-estimate.R
Created April 16, 2018 07:25
I wanted to understand how to simulate counts from a tweedie distribution using fitted mu after using gam but didn't get how to estimate the dispersion parameter, phi. had to dig through code (stats::summary.glm) and through some papers to verify. looks good!
# inspired by https://stats.stackexchange.com/questions/174121/can-a-model-for-non-negative-data-with-clumping-at-zeros-tweedie-glm-zero-infl
# additions by Danton Noriega
library(statmod)
library(tweedie)
library(mgcv)
# generate fake mu (poisson count rates)
set.seed(1789)
x <- seq(1,100, by = .1)
mutrue <- exp(-1+x/25)
@dantonnoriega
dantonnoriega / deep_loops_in_stan.R
Last active August 16, 2018 19:03 — forked from khakieconomics/deep_loops_in_stan.R
Write your deep loops in Stan, not R. added a quick intro into how the simulations fill into the matrix
library(tidyverse)
library(rstan)
## HOW THE SIMULATION LOOP BELOW WORKS -- ignore the shocks for now --------------
# quick example
I = 3 # individuals
M = 5 # "months"
S = 7 # sims
mat <- matrix(NA, I*M, S) # M rows (months) will be filled in at a time for each individual i across all S columns (simulations); records for individual i will populate rows ((i - 1)*M + 1):(i*M)
# stan code from https://arxiv.org/pdf/1808.06399.pdf
library("DirichletReg")
Bld <- BloodSamples
Bld <- na.omit(Bld)
Bld$Smp <- DR_data(Bld[, 1:4])
stan_code <- '
data {
int<lower=1> N; // total number of observations
int<lower=2> ncolY; // number of categories
# recreate the code from https://arxiv.org/pdf/1808.06399.pdf using greta (https://greta-dev.github.io/greta/)
library("DirichletReg")
Bld <- BloodSamples
Bld <- na.omit(Bld)
Bld$Smp <- DR_data(Bld[, 1:4])
# simplex function. applies simplex to each row of a matrix.
# HUGE speed gains using matrix mat vs for loop!
simplex_mat <- function(x){
exp_x <- exp(x)