Skip to content

Instantly share code, notes, and snippets.

View EoinTravers's full-sized avatar

Eoin Travers EoinTravers

View GitHub Profile
@EoinTravers
EoinTravers / psychopy_compound.py
Last active January 31, 2020 11:59
Treat a list of psychopy visual components as a single object.
from psychopy import visual
from psychopy.tools.colorspacetools import hsv2rgb
import numpy as np
win = visual.Window(
size=[800, 600], fullscr=False, screen=0,
gammaErrorPolicy='ignore',
color=(1, 1, 1), colorSpace='rgb', units='height')
class CompoundStim():
@EoinTravers
EoinTravers / permutation_test.R
Created April 14, 2020 21:43
What happens if we permute after fitting?
library(tidyverse)
# Arguments: Sigma of old predictor, Sigma of new predictor, N cases
compare_methods = function(s0, s1, n){
print(c(s0, s1, n))
y = rnorm(n, 0, 1)
x0 = y + rnorm(n, 0, s0) # Old predictor
x1 = y + rnorm(n, 0, s1) # New predictor to evaluate
m0 = lm(y ~ x0)
@EoinTravers
EoinTravers / dag.R
Last active April 30, 2020 13:05
Testing DAG in brms/rstan
library(brms)
library(rstan)
## Simulate data
bxy = 2
bxz = 3
byz = 4
e = .1
n = 1000
@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
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)
# 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
@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))`
---
title: "glm_prevelance"
author: "Eoin Travers"
output: html_document
---
```{r}
library(tidyverse)
binom_smooth = function (link = "probit", ...) {
geom_smooth(method = "glm",
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',
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()