Skip to content

Instantly share code, notes, and snippets.

@ha0ye
ha0ye / zs_dist.R
Created October 20, 2021 23:13
distribution of zodiac signs in US population
library(tidyverse)
library(lubridate)
# modified from https://stackoverflow.com/questions/48026462/writing-r-function-using-lubridate-package-that-takes-dateex-august-14-and
zodiac_sign <- function(input)
{
zodiac_sign_df <- data.frame(Month = c("March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February"),
Zodiac_Separation_Dt = c(21, 20, 21, 21, 23, 23, 23, 23, 22, 22, 20, 19),
LowerSideZS = c("Pisces", "Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius","Capricorn","Aquarius"),
UpperSideZS = c("Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius","Capricorn","Aquarius", "Pisces"),
@ha0ye
ha0ye / generate_sequences.R
Last active August 31, 2021 21:07
generate random sequences without consecutive repeats
f <- function(x)
{
n <- sum(x)
v <- numeric(n)
prev <- NULL
for (i in 1:n)
{
idx <- x > floor(sum(x)/2) # how many counts are > n/2
if (sum(idx) >= 2) # error if more than 2 (impossible?)
{
@ha0ye
ha0ye / main.R
Last active June 26, 2020 14:59
EDM simplex calculations test (E = 1)
library(rEDM)
# generate time series
set.seed(42)
ts_length <- 50
x <- rnorm(ts_length)
# pre-compute distance matrix
x_dists <- as.matrix(dist(x))
x_dists <- x_dists[, -ts_length] # last point can't be a nearest neighbor
@ha0ye
ha0ye / gist:197de5797df1d4e2b14a8bff4fb99c95
Created March 28, 2020 01:15
pairwise overlap calculations
calc3 <- function(sets)
{
sets <- check_sets(sets)
set_lengths <- vapply(sets, length, 0)
set_order <- order(set_lengths)
sets <- sets[set_order]
set_lengths <- set_lengths[set_order]
n_sets <- length(sets)
set_names <- names(sets)
@ha0ye
ha0ye / gist:d62b33b1d735b09610852a7113f6bda3
Created March 12, 2020 16:35
Forward forecasts using rEDM
library(rEDM)
#### Example 1: a single forward prediction
# Assume we have already done the analysis, finding that the best embedding
# dimension is 2. Our objective is to make multiple 1-step ahead forecasts
# using simplex and E = 2.
dat <- data.frame(yr = as.numeric(time(sunspot.year)),
sunspot_count = as.numeric(sunspot.year))
E <- 3
@ha0ye
ha0ye / script.R
Created February 18, 2020 16:13
read in param output from text file
full_text <- readLines(file, warn = FALSE)
start_par_line <- grep("^\\$par", full_text)
start_objective_line <- grep("^\\$objective", full_text)
param_lines <- seq(from = start_par_line,
to = start_objective_line - 3,
by = 2)
params <- purrr::map_dfc(param_lines, function(line_idx)
{
readr::read_delim(file, delim = " ",
library(dplyr)
library(microbenchmark)
set.seed(42)
n <- 10000
df <- data.frame(a = rep(seq(n), 2),
x = rnorm(2 * n))
group_summarize_mutate <- function(df)
{
library(likert)
data(pisaitems)
##### Item 24: Reading Attitudes
items24 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST24Q']
head(items24); ncol(items24)
names(items24) <- c(
ST24Q01="I read only if I have to.",
ST24Q02="Reading is one of my favorite hobbies.",
@ha0ye
ha0ye / drake-parallel.R
Created June 21, 2019 16:29
parallelization within drake targets
library(drake)
library(furrr)
future::plan(future::multiprocess)
tictoc::tic()
params = data.frame(t = c(1, 2, 3, 1, 2, 3))
out = purrr::pmap(params, Sys.sleep)
tictoc::toc()
library(lavaan)
library(visNetwork)
model <- '
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60