Skip to content

Instantly share code, notes, and snippets.

View yjunechoe's full-sized avatar
🐣

June Choe yjunechoe

🐣
View GitHub Profile
@yjunechoe
yjunechoe / twosum.R
Created February 15, 2025 18:07
Leetcode twosum problem in R
twosum <- function(nums, target) {
cur_i <- 1
nxt_i <- cur_i + 1
while(cur_i != length(nums)) {
thesum <- nums[cur_i] + nums[cur_i + 1]
if (thesum == target) {
return(c(cur_i, nxt_i))
}
if (nxt_i == length(nums)) {
cur_i <- cur_i + 1
@yjunechoe
yjunechoe / zscore-subject-intercept.R
Last active February 10, 2025 02:32
If you z-score the response variable by group, will by-group intercept always be zero? (no - depends on meaning of intercept - ex: in dummy coding)
library(tidyverse)
set.seed(123)
df <- crossing(
subject = 1:10,
prime = c("yes", "no")
) %>%
mutate(
intercept = 500,
y x1 x2 g
-1.84 a1 b2 s1
-0.66 a1 b2 s1
-0.83 a1 b2 s1
0.87 a2 b2 s1
0.12 a1 b2 s1
-0.67 a1 b2 s1
0.4 a2 b2 s1
-1.12 a1 b2 s1
-0.05 a1 b2 s1
@yjunechoe
yjunechoe / get-lazydata.R
Last active April 22, 2025 15:02
Difference between `get()` vs. `getFromNamespace()` and function vs. lazydata
# Without dplyr loaded ----
isNamespaceLoaded("dplyr")
#> [1] FALSE
## FUNCTION: Both work for getting `dplyr::mutate`
get("mutate", envir = asNamespace("dplyr"))
#> function (.data, ...)
#> {
#> UseMethod("mutate")
#> }
# https://fosstodon.org/@josi/113391735804937128
cumsum_cut_rlang <- function(x, cuts) {
x[cuts-1] <- 0
res <- lapply(split(x, cumsum(x == 0)), \(.x) {
cumsum(.x)
}) |>
unlist() |>
unname()
n <- length(res)
to_fill <- numeric(n)
@yjunechoe
yjunechoe / convergence-diagnostics.R
Last active October 21, 2024 13:23
Examples of diagnostics for convergence failures using data from Choe, Yoshida, & Cole (2022)
library(arrow)
CYC_2022 <- read_feather("https://raw.githubusercontent.com/yjunechoe/Semantic-Persistence/refs/heads/master/CYC_2022.arrow")
# Diagnosing warnings of type: "Model failed to converge with max|grad| ..."
library(lme4)
#> Loading required package: Matrix
fm <- Accuracy ~ Condition * SemanticFit * Transitivity +
(1 | Item) +
(1 + Condition | Subject)
# Top-level
thefuck <- function(e = rlang::last_error()) {
stopifnot(rlang::is_call_simple(e$call))
if (grep("^unused argument", e$message)) {
fix_unused_arg(e)
}
# ... more cases
}
# Internal methods
@yjunechoe
yjunechoe / convert_to_zerocorr.R
Created May 6, 2024 16:15
Convert lme4 double-bar syntax to MixedModels.jl zerocorr()
library(rrapply)
expr <- y ~ x + (1 || z)
lobstr::ast(!!expr)
#> █─`~`
#> ├─y
#> └─█─`+`
#> ├─x
#> └─█─`(`
#> └─█─`||`
#> ├─1
@yjunechoe
yjunechoe / highlight_color_by.R
Created May 6, 2024 02:07
highlight_color_by()
# The reprex
library(ggplot2)
data <- tibble::tribble(
~x, ~group, ~y, ~measure, ~base,
"FY18", "aaa", 0.5603448, 260, 464,
"FY15", "aaa", 0.5081269, 1313, 2584,
"FY19", "aaa", 0.5799373, 185, 319,
"FY16", "aaa", 0.5225225, 580, 1110,
"FY13", "aaa", 0.4779116, 595, 1245,
"FY17", "aaa", 0.5502471, 334, 607,
@yjunechoe
yjunechoe / ternary.R
Created March 26, 2024 14:30
ternary op in R
lobstr::ast(T ? F : 1)
`%?%` <- function(e1, e2) {
e2_expr <- substitute(e2)
if (e1) {
return(eval.parent(e2_expr[[2]]))
} else {
return(eval.parent(e2_expr[[3]]))
}
}