Skip to content

Instantly share code, notes, and snippets.

View dmolitor's full-sized avatar
🍂

Daniel Molitor dmolitor

🍂
View GitHub Profile
@dmolitor
dmolitor / ols_sparse_ols.R
Last active July 29, 2021 20:19
OLS vs. sparse OLS
# Set seed
set.seed(123)
test_lm <- lm(mtcars$mpg ~ as.matrix(mtcars[c("wt", "drat")]))
test_glm <- glm(mtcars$mpg ~ as.matrix(mtcars[c("wt", "drat")]),
family = gaussian())
# Are they the same? (Hint: yes)
identical(coef(test_lm), coef(test_glm))
@dmolitor
dmolitor / lm_fit_sparse.R
Created July 29, 2021 20:37
Fit sparse OLS
lm.fit.sparse <- function(x,
y,
w = NULL,
offset = NULL,
method = c("qr", "cholesky"),
tol = 1e-07,
singular.ok = TRUE,
order = NULL,
transpose = FALSE) {
cld <- getClass(class(x))
@dmolitor
dmolitor / crrri_dump_dom.R
Last active October 8, 2021 14:04
Scrape dynamic web page content using the crrri package.
library(crrri)
# Function that uses headless Chrome to extract webpage html
headless_ch_get <- function(url,
user.agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
headless = TRUE,
wait.for.page = 0,
pause.before.closing = NULL,
ip = NULL,
port = NULL,
@dmolitor
dmolitor / tidy-tuesday-2021-09-14
Last active September 14, 2021 19:34
Tidy Tuesday 2021-09-14
library(dplyr)
library(dtplyr)
library(ggplot2)
library(gt)
library(lubridate)
library(readr)
library(stringr)
library(tibble)
library(tidyr)
library(viridis)
@dmolitor
dmolitor / stop_and_stem.R
Created September 16, 2021 14:28
Remove stop words and create word stems.
library(SnowballC)
library(tidyverse)
library(tidytext)
test <- tibble("id" = 1:5,
"message" = c("I went to play tennis",
"We made almond pound cake and played ball",
"All the cakes in the bakery were good",
"We had a hearty lunch",
"He had open heart surgery"))
@dmolitor
dmolitor / new_r_initialize.R
Last active November 24, 2021 22:32
Initialize new R installation.
if (!"Hmisc" %in% rownames(installed.packages())) install.packages("Hmisc")
packages <- Hmisc::Cs(
conflicted,
getPass,
assertthat,
testthat,
here,
magrittr,
data.table,
@dmolitor
dmolitor / venv_initialize.ps1
Last active November 12, 2021 18:53
Initialize Python virtual environment on Windows and install pip, setuptools, and wheel.
function New-VirtualEnv {
param
(
$VirtualEnvDir,
$EnvName,
$Deactivate
)
# Create virtual env
cd $VirtualEnvDir
@dmolitor
dmolitor / any_add_up.py
Last active November 3, 2021 16:03
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
def any_adds_up(num_list, k):
if not isinstance(k, int) or isinstance(k, float):
raise TypeError("'k' must be numeric")
if not isinstance(num_list, list):
raise TypeError("'num_list' must be a list")
return any([-(x - k) in num_list for x in num_list])
@dmolitor
dmolitor / multiply_all_but_i.py
Created November 3, 2021 16:45
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
def mult_reduce(ls):
out = ls[0]
for i in range(1, len(ls)):
out = out * ls[i]
return out
def mult_all_but_i(num_list):
if len(num_list) < 2:
raise ValueError("Undefined operation")
return [mult_reduce(num_list[:i] + num_list[i+1 :]) for i
@dmolitor
dmolitor / first_missing_positive.py
Created November 3, 2021 18:17
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
def first_missing_positive(A):
if not A or max(A) <= 0:
return 1
target_list = list(range(1, max(A) + 2))
return min([x for x in target_list if x not in A])