Skip to content

Instantly share code, notes, and snippets.

View pbaylis's full-sized avatar

Patrick Baylis pbaylis

View GitHub Profile
@pbaylis
pbaylis / makebins.ado
Last active September 14, 2015 22:32
Stata program to generate dummy variable bins of a variable. Stata program to generate dummy variable bins of a variable. Bottom and top bins run from edge to negative infinity and infinity, respectively. Also generates good-looking custom labels for regression tables.
program define makebins, rclass
syntax varname, [PREfix(name) min(real 0) max(real 100) step(real 5) SEQuential DROPbin(real 70)]
local var `varlist'
if mod(`max' - `min',`step') != 0 {
di as error "Warning: Step size `step' does not divide evenly into range. Top bin may extend beyond `max'."
}
* Declare prefix
if "`prefix'" != "" {
@pbaylis
pbaylis / gen_heat_index.ado
Last active September 11, 2015 12:39
Stata program to get heat index for given dry bulb temperature and relative humidity
program define gen_heat_index
* Calculat heat index
* See https://en.wikipedia.org/wiki/Heat_index. Note that there is no exact heat index formula
* since it is defined by the table. There are approximations, but this code uses the table itself.
* The only modification is that I cap at 140.
syntax, DRYbulbtemp(varname) RELativehumidity(varname) [GENerate(string)]
if "`generate'" == "" {
local generate "heat_index"
}
@pbaylis
pbaylis / gen_relative_humidity.ado
Created September 11, 2015 12:23
Stata program to get relative humidity given temperature and dew point
program define gen_relative_humidity
* Calculate relative humidity given dry bulb temperature and dew point
syntax, DRYbulbtemp(varname) DEWpoint(varname) [GENerate(string)]
* Inputs in F
local T `drybulbtemp'
local TD `dewpoint'
if "`generate'" == "" {
local generate "rel_humidity"
}
@pbaylis
pbaylis / 00_spatial_interpolation.R
Last active February 27, 2022 02:16
Sample R spatial interpolation code. First fills in missing obs using CDF method, then interpolates to a grid. Useful for converting weather station to gridded data. Uses IDW but that could easily be changed.
library(foreign)
library(data.table)
library(zoo)
library(Hmisc)
library(gstat)
library(sp)
rm(list=ls())
# Fill in missing data ----
sm.thresh <- 0.9 # Min proportion of obs that must be in a station-month
@pbaylis
pbaylis / bear_morning.sh
Created March 31, 2017 06:56
Shell script to open Bear with a daily log template
#!/bin/bash
DATE=`date "+%A, %b %d %Y"`
YESTERDAY=`date -v-1d "+%A, %b %d %Y"`
open "bear://x-callback-url/create?title=$DATE&text=## Beginning of day thoughts
## Goals
-
## End of day thoughts
[[$YESTERDAY]]&tags=dailylog"
@pbaylis
pbaylis / update-beamer-packages.R
Created May 16, 2019 15:11
Install missing style files (R, tinytex)
library(tinytex)
tlmgr_search('/times.sty') # search for times.sty
tlmgr_install('psnfss') # install the psnfss package
tlmgr_update() # update everything
@pbaylis
pbaylis / execute-tmux.sh
Last active May 16, 2019 15:35
Use tmux to run commands through ssh (that won't close when you log out)
## Logging into server
ssh <server-address>
# Login
tmux
# CTRL-b d to detach tmux
exit
## When returning to server
tmux attach
@pbaylis
pbaylis / pad-with-zeroes.R
Created May 27, 2019 18:30
Pad integers with zeroes
x <- c(5, 15, 2015)
sprintf(sprintf("%03d", x))
@pbaylis
pbaylis / mult-fxn-mult-col.R
Created May 27, 2019 18:31
Apply multiple functions to multiple columns (with data.table)
my.summary = function(x) list(mean = mean(x), median = median(x))
DT[, as.list(unlist(lapply(.SD, my.summary))), .SDcols = c('a', 'b')]
@pbaylis
pbaylis / points-in-polys.R
Created May 27, 2019 18:33
Points in polygons with sf
points.shp <- points.shp[apply(sf::st_intersects(points.shp, polys), 1, any),]