Skip to content

Instantly share code, notes, and snippets.

View EoinTravers's full-sized avatar

Eoin Travers EoinTravers

  • Institute of Cognitive Neuroscience, University College London
  • London
View GitHub Profile
@EoinTravers
EoinTravers / long_string.txt
Created January 26, 2024 11:26
Need a string of a specific length for debugging purposes? For a 100-charachter string, select up to the end of "100", etc. '\n' newlines count as one charachter.
........10........20........30........40........50
.......60........70........80........90.......100
......110.......120.......130.......140.......150
......160.......170.......180.......190.......200
......210.......220.......230.......240.......250
......260.......270.......280.......290.......300
......310.......320.......330.......340.......350
......360.......370.......380.......390.......400
......410.......420.......430.......440.......450
......460.......470.......480.......490.......500
---
title: "MVN + Regression Interactions"
author: "Eoin Travers"
date: "`r Sys.Date()`"
output: html_document
---
https://twitter.com/realHollanders/status/1605462357697667072?s=20&t=sfGV7iiPavt6DwCrMdks-w
```{r}
@EoinTravers
EoinTravers / better_lrt.py
Last active August 4, 2022 14:12
Likelhood ratio tests for python statsmodels GLMs
# This version is more complicated, but can handle > 2 models and throws an error on non-nested models.
import numpy as np
from scipy import stats
import statsmodels.formula.api as smf
import pandas as pd
def likelihood_ratio_test(*models):
'''Model comparison via likelihood ratio chi-square test
See https://en.wikipedia.org/wiki/Likelihood-ratio_test
generate_spaced_dates = function(n_samples, gap = 21,
total_period = 365,
n_iter=1e12){
x = runif(n_samples, 0, total_period) %>% sort() %>% round()
for(i in 1:1e12){
is_acceptable = min(diff(x)) >= 21
if(is_acceptable) {
break
} else {
x = runif(n_samples, 0, total_period) %>% sort() %>% round()
plot_measurement_invariance = function(model){
factor_loadings = parameterEstimates(model) %>%
rename(estimate = est, low = ci.lower, high = ci.upper) %>%
mutate(rhs = factor(rhs, levels = unique(rhs)),
lhs = factor(lhs, levels = unique(lhs)),
label = paste(lhs, op, rhs),
type = case_when(
op == '=~' ~ 'Loading',
op == '~~' ~ 'Covariance',
op == '~1' ~ 'Mean',
---
title: "glm_prevelance"
author: "Eoin Travers"
output: html_document
---
```{r}
library(tidyverse)
binom_smooth = function (link = "probit", ...) {
geom_smooth(method = "glm",
@EoinTravers
EoinTravers / plot_matrix.R
Last active June 1, 2021 13:08
Plot a (correlation/covariance) matrix in R
#' Plot matrix as heatmap
#' @description
#' `plot_covariance_matrix()` adds an appropriate `fill_label`
#' `plot_correlation_matrix()` also sets limits to ±1
#'
#' Make sure to import dplyr and ggplot (or tidyverse)!
#' @param mat Matrix to plot
#' @param labeller Function or list used to rename rows/cols
#' @param digits Digits to round values to (default 2)
#' @param limit Limit (±) of colour scale. Defaults to `abs(max(value))`
# Coin Flip MAP Bootstrap
library(tidyverse)
theme_set(theme_bw(base_size=18))
# Data
outcomes = c(rep(0, 5), rep(1, 15)) # True probability = 0.75
n_trials = length(outcomes)
# Beta Prior
library(tidyverse)
# Simulate data under 1-cluster solution:
# Two Gaussians, with different means, same SD.
m1 = 10
m2 = 20
sd = 10
n1 = 50
n2 = 50
x1 = rnorm(n1, m1, sd)
@EoinTravers
EoinTravers / plot_top.py
Last active May 15, 2020 13:30
matplotlib: Plot the top N values, then squeeze in the rest
import numpy as np
import matplotlib.pyplot as plt
def plot_top(names, values, n_top=10,
order=None,
alpha=.25,
ax=None, figsize=None):
'''Plot the top N values, then squeeze in the rest