Skip to content

Instantly share code, notes, and snippets.

View tmasjc's full-sized avatar
Strange Loop

Thomas Jc tmasjc

Strange Loop
  • Dumpling City
  • 09:38 (UTC +08:00)
View GitHub Profile
@tmasjc
tmasjc / .R
Created October 2, 2020 15:12
tidyeval using parse expressions
library(tidyverse)
library(rlang)
set.seed(123)
df <- tibble(id = rep(c(1:2), 10),
grade = sample(c("A", "B", "C"), 20, replace = TRUE))
df %>%
group_by(id) %>%
summarise(
@tmasjc
tmasjc / .R
Last active September 9, 2020 04:48
test for differences between AB groups - hetaojr
library(tidyverse)
library(janitor)
library(EnvStats)
library(infer)
library(ggthemes)
set.seed(123)
old = theme_set(theme_minimal(base_family = "Noto Sans CJK SC") +
theme(legend.position = "top"))
# DID ----
@tmasjc
tmasjc / script.R
Last active August 13, 2020 10:11
parallel extract info from user-agent string
library(feather)
library(dplyr)
library(purrr)
library(furrr)
library(uaparserjs)
raw <- feather::read_feather("raw.feather") %>%
`[[`('ua') %>%
purrr::discard(is.na) %>%
unique()
@tmasjc
tmasjc / tutors_performance_by_city_and_year.R
Last active July 27, 2020 09:10
Demonstrate alternative to boxplot
library(tidyverse)
library(ggthemes)
library(lubridate)
library(gganimate)
# ggplot theme
old <- theme_set(
theme_minimal() +
theme(
text = element_text(family = 'Menlo'),
@tmasjc
tmasjc / ebeta.R
Created July 14, 2020 07:17
Estimate beta distribution using EnvStats package
set.seed(1234)
vec = c(13.1, 7.5, 4.4, 10.0, 7.8, 3.4, 3.5) / 100
rb = EnvStats::ebeta(vec)
alpha = rb$parameters['shape1']
beta = rb$parameters['shape2']
x <- rbeta(1000, shape1 = alpha, shape2 = beta)
hist(
x,
@tmasjc
tmasjc / equilibrium.R
Last active July 3, 2020 06:32
another dynamic programming
cuts = 0.8 # cutoff level
maxN = 100 # number of trials
run_game <- function(vec, cutoff, N) {
# browser()
if (length(vec) > N) {
return(vec)
}
pass = ifelse(mean(vec) >= cutoff, 0, 1)
vec = c(vec, pass)
@tmasjc
tmasjc / script.R
Created June 27, 2020 02:52
A spurious example
## the game goes like this,
## head +1 point and tail -1 point
## we flip 3 coins and count the score
## is there a correlation between them?
library(tidyverse)
library(ggthemes)
set.seed(1234)
coins = 4
@tmasjc
tmasjc / curve_fitting.R
Last active June 11, 2020 15:11
Demostration of curve fitting
library(tidyverse)
# some dummy data
df <- read_csv("dummy.csv")
# loess fitting via ggplot2
df %>%
mutate(batch = factor(batch,
ordered = TRUE,
levels = paste0("X00", 1:12))) %>%
@tmasjc
tmasjc / merge_same_cells.vba
Created June 7, 2020 09:12
Quickly merge cells of same value
Sub MergeSimilarCells()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Specify column here
Set myRange = Range("B2:B223")
CheckAgain:
For Each cell In myRange
If cell.Value = cell.Offset(1, 0).Value And Not IsEmpty(cell) Then
Range(cell, cell.Offset(1, 0)).Merge
@tmasjc
tmasjc / backtest.R
Last active June 9, 2020 11:19
Capacity and utilization by cohort
library(furrr)
# for parallel computing
future::plan(future::multiprocess)
options(future.globals.maxSize = 900*1024^2)
# remove significant jump in May 2020
max_date <- as.Date("2020-03-02")
# sequence of backtest
date_seq <- purrr::map(0:36, ~ max_date - months(1 * .x))