Skip to content

Instantly share code, notes, and snippets.

View vikjam's full-sized avatar
💭
👨🏾‍💻

vikjam

💭
👨🏾‍💻
View GitHub Profile
@vikjam
vikjam / treas.r
Created June 30, 2017 11:48
Plot of yields
library(rvest)
library(ggplot2)
library(ggthemes)
library(MASS)
library(splines)
treas.html <- html("http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield")
yield.data <- treas.html %>%
html_nodes("table") %>%
.[[67]] %>%
@vikjam
vikjam / prison.r
Created June 30, 2017 11:46
Quick plot of prison rates versus murder rate.
library(rvest)
library(ggvis)
wiki.prison <- html("http://en.wikipedia.org/wiki/List_of_countries_by_incarceration_rate")
wiki.prison.data <- wiki.prison %>%
html_nodes("table") %>%
html_nodes("table") %>%
.[[2]] %>%
html_table(fill = TRUE)
@vikjam
vikjam / locus.r
Created June 30, 2017 11:45
Quick comparison of locus of control by debt.
library(foreign)
library(dplyr)
library(ggplot2)
success <- read.dta("ALP_MS118_2016_03_13_06_51_42.dta")
scpc2010 <- read.dta("SCPC2010.dta")
scpc2010.adopt <- scpc2010[ , c("prim_key", "cc_adopt", "cc_sh")]
success.locus <- success[ , c("ms118_prim_key", "ms118_lc1")]
@vikjam
vikjam / select.sh
Created March 18, 2017 14:33
Use awk to subset a CSV
# http://stackoverflow.com/questions/6491532/how-to-subset-a-file-select-a-numbers-of-rows-or-columns
cat largefile | awk 'NR >= 10000 && NR <= 100000 { print }'
@vikjam
vikjam / split.sh
Created March 12, 2017 02:27
Split file into equal parts
awk 'NR%20==1 { file = FILENAME "_" sprintf("%04d", NR+19) } { print > file }' domains.xml
@vikjam
vikjam / sampsi-vs-power.do
Created January 22, 2017 15:57
sampsi versus power in Stata
* Two-sample comparison of mean1 to mean2. Compute sample sizes with n2/n1 = 2:
sampsi 132.86 127.44, p(0.8) r(2) sd1(15.34) sd2(18.23)
power twomeans 132.86 127.44, p(0.8) nratio(2) sd1(15.34) sd2(18.23)
@vikjam
vikjam / equal.with.NA.r
Last active January 11, 2017 01:01
Include NA in test
'%==%' <- function(a, b) {
equality.test <- (a == b)
equality.test[is.na(a) & is.na(b)] <- TRUE
equality.test[is.na(equality.test)] <- FALSE
return(equality.test)
}
@vikjam
vikjam / Preferences.sublime-settings
Created September 4, 2016 13:16
Preferences.sublime-settings
{
"always_show_minimap_viewport": true,
"bold_folder_labels": true,
"caret_extra_bottom": 1,
"caret_extra_top": 1,
"caret_extra_width": 1,
"caret_style": "blink",
"color_scheme": "Packages/Material Theme/schemes/Material-Theme-Darker.tmTheme",
"fade_fold_buttons": false,
"font_face": "Source Code Pro",
@vikjam
vikjam / regress-vs-randomforest.r
Created August 9, 2016 15:27
Example of regression versus random forest predictions from StackExchange
# http://stats.stackexchange.com/questions/66757/random-forest-vs-regression
beta <- runif(5)
x <- matrix(rnorm(500), nc=5)
y <- drop(x %*% beta)
dat <- data.frame(y=y, x1=x[,1], x2=x[,2], x3=x[,3], x4=x[,4], x5=x[,5])
model1 <- lm(y~., data=dat)
model2 <- randomForest(y ~., data=dat)
pred1 <- predict(model1 ,dat)
pred2 <- predict(model2 ,dat)
@vikjam
vikjam / flac2aac.sh
Created May 11, 2016 11:38
Convert FLAC to AAC
# Install ffmpeg with the right codec
brew install ffmpeg --with-fdk-aac
# Actual loop
for i in *flac;do of="${i/.flac/.m4a}"; ffmpeg -i "${i}" -c:a libfdk_aac -b:a 320k "${of}";done