Skip to content

Instantly share code, notes, and snippets.

View alexpghayes's full-sized avatar

alex hayes alexpghayes

View GitHub Profile
@alexpghayes
alexpghayes / glmnet_factor_ex.r
Last active April 28, 2017 22:22
glmnet factor example
library(ggplot2)
library(glmnet)
x <- model.matrix(mpg ~ factor(cyl) + factor(gear) + wt * factor(gear), mtcars)
y <- mtcars$mpg
x
fit <- cv.glmnet(x, y, alpha = 1) # lasso when alpha = 1, ridge when alpha = 0
@alexpghayes
alexpghayes / gather_mutate_spread.r
Created June 14, 2017 20:16
looking for a better way to manipulate data in a gather mutate spread pattern
I have categorical data spread across multiple columns that I would like to aggregate.
library(tidyverse)
data <- data_frame(var1 = sample(LETTERS[1:2], 50, replace = TRUE), # categorical A/B
var2 = sample(LETTERS[1:2], 50, replace = TRUE),
var3 = sample(LETTERS[1:2], 50, replace = TRUE),
var4 = sample(LETTERS[3:4], 50, replace = TRUE), # categorical C/D
var5 = sample(LETTERS[3:4], 50, replace = TRUE),
var6 = sample(LETTERS[3:4], 50, replace = TRUE)) %>%
@alexpghayes
alexpghayes / ttt_simulated_distribution.R
Last active June 21, 2017 17:28
sampling distribution simulation for alisa
library(tidyverse)
get_ts <- function(group_size, ttt_p_inf, ctrl_p_inf) {
a <- rbinom(1, size = group_size, prob = ttt_p_inf) # a
c <- rbinom(1, size= group_size, prob = ctrl_p_inf) # c
b <- group_size - a
d <- group_size - c
((a - c) / group_size) / sqrt((a * b + c * d) / group_size ^ 3)
@alexpghayes
alexpghayes / merge_chr.R
Created June 21, 2017 21:49
generalization of tidyr::unite
df <- tribble(
~ID, ~d1, ~d2, ~d3,
1, "G", "G", "C",
2, NA, "G", "T",
3, "A", NA, "G",
4, "G", "A", "A",
5, NA, NA, NA,
6, "G", "G", "G")
merge_chr <- function(df, col, ..., fun, remove = TRUE) {
@alexpghayes
alexpghayes / gather_spread_saves_the_day.r
Created June 23, 2017 01:04
tidyr::spread -- use `sep` and `convert` args for a good time
library(tidyverse)
data_frame(id = 1:50,
rel1 = sample(LETTERS[1:4], 50, replace = TRUE),
gender1 = sample(c("M", "F", "O"), 50, replace = TRUE),
score1 = rnorm(50),
rel2 = sample(LETTERS[1:4], 50, replace = TRUE),
gender2 = sample(c("M", "F", "O"), 50, replace = TRUE),
score2 = rnorm(50)) %>%
gather(field, value, -id) %>%
@alexpghayes
alexpghayes / exprs_for_mutate.R
Created July 15, 2017 20:47
#tidyeval question for Hadley
library(tidyverse)
library(rlang)
f <- function(x, c) c * x
col_names <- c("am", "gear", "carb")
exprs <- purrr::map(col_names, ~quo(!!paste0("c_", .x) := f(!!sym(.x), 3)))
mutate(mtcars, !!!exprs)
# Error in mutate_impl(.data, dots) : Column ``:=`("c_am", f(am, 3))` is of unsupported type quoted call
@alexpghayes
alexpghayes / marc_bot.py
Last active August 3, 2017 04:04
sophisticated bi-directional LTSM to simulate something marc would say
import numpy as np
phrases = [ \
"I don't like Guerra", \
"Roberto is excellent. I like Roberto.", \
"Dobelman is a great teacher! How could you not like Dobelman!", \
"I don't like Devika. She's not a very good teacher.", \
"Noon is too early to go to class.", \
"I like Ms. Poon :)", \
"!"]
@alexpghayes
alexpghayes / call_python_from_r.R
Created August 27, 2017 01:05
call a python function f: numpy array -> numpy array from R
library(reticulate)
library(tidyverse)
X <- rbind(
c(1, 2),
c(3, 4)
)
y <- cbind(
c(3, 5)
library(tidyverse)
beans <- c("Caturra", "Grusti", "Double roasted")
coffees <- c("Garuda", "Blend 101", "Blend 201", "Exxxtra special blend")
location <- c("Rwanda", "Columbia", "Peru")
people <- c("Dan Wallach", "Chris Jermaine", "Scott Rikner", "Luay")
has_bean <- tibble(
coffee = sample(coffees, 10, replace = TRUE),
bean_name = sample(beans, 10, replace = TRUE)
# open a SQL terminal. note that my netid is aph3
runas /user:adrice\aph3 /netonly "sqlcmd -S classdb.ad.rice.edu"
# in the SQL terminal, make sure things work
CREATE DATABASE apgha1db
USE apgha1db;
GO
# populate the database we just created. note that C:\Users\alex\Desktop\comp330\a1\ is the path to where I kept my A1 files