Skip to content

Instantly share code, notes, and snippets.

View bnicenboim's full-sized avatar

Bruno Nicenboim bnicenboim

View GitHub Profile
@bnicenboim
bnicenboim / authors.R
Last active February 9, 2024 07:52
getting potentially duplicated authors from a bibtex
library(RefManageR)
library(tidytable)
library(stringr)
bib1 <- ReadBib("./BayesCogSci.bib")
bib2 <- ReadBib("./packages.bib")
map(c(bib1, bib2), function(entry) {
map2(entry$author, entry$key, function(person, key) {
tidytable(key =key,
@bnicenboim
bnicenboim / scrolldiff.sh
Last active January 17, 2024 07:45
compare pdfs
#!/bin/bash
#
# Compare two PDF files.
# Dependencies:
# - pdfinfo (xpdf)
# - pdfjam (texlive-extra-utils)
# - diffpdf
#
@bnicenboim
bnicenboim / SBC.R
Last active October 28, 2020 14:18
sbc for cmdstanr
library(dplyr)
sbc_cmd <- function (cmdstanrmodel, data, M, ...)
{
stan_code <- cmdstanrmodel$code()
stan_code <- scan(what = character(), sep = "\n", quiet = TRUE,
text = stan_code)
pars_lines <- grep("[[:space:]]*(pars_)|(pars_\\[.*\\])[[:space:]]*=",
stan_code, value = TRUE)
if(length(pars_lines)==1){
@bnicenboim
bnicenboim / addresses.R
Last active November 1, 2019 16:17
get a table with locations in memory and objects names
oo <- ls(envir=.GlobalEnv)
purrr::map_chr(oo, ~ do.call(pryr::address,list(rlang::sym(.x))) ) %>%
setNames(oo) %>%
tibble::enframe()
@bnicenboim
bnicenboim / gist:d613d4a215cdf7aaf3b48a3e385ae94f
Created June 29, 2018 14:54
quantile probability (QP) plot
library(dplyr)
library(purrr)
library(ggplot2)
data <- tibble(b= rnorm(1000), RT= rlnorm(1000,-.3+b,.3), correct= rbinom(1000, 1, plogis(b)))
pdata <- data %>% mutate(quantiles=list(quantile(RT,c(.1,.3,.5,.7,.9,1))), q = map_dbl(RT, ~ which(.x <= quantiles[[1]] ) %>% min )) %>%
select(-quantiles) %>% group_by(q) %>% summarize(RT=mean(RT),p= mean(correct))
ggplot(pdata, aes(x=p,y=RT)) + geom_point() + geom_line()
@bnicenboim
bnicenboim / process_dat.R
Created April 20, 2018 15:38
loading dat files generated by BrainVision using eegUtils
library(eegUtils)
library(tidyverse)
library(magrittr)
### Import dat needs a lot of information, since dat files don't contain any meta data
import_dat <- function(file_name, data_points, chan_info,
srate, event_table = NULL, orientation = "vectorized",
domain = "time") {
@bnicenboim
bnicenboim / softmax.R
Created March 30, 2018 13:07
softmax function in R for matrices, based on Stan reference manual 2.17, page 478
softmax <- function(probs){
if(is.null(dim(probs))) probs <- matrix(probs,ncol= length(probs))
exp(probs)/apply(probs,1, function(x) sum(exp(x)))
}
@bnicenboim
bnicenboim / helmert.stan
Created March 30, 2018 12:55
function for creating a Helmert matrix in Stan
functions {
matrix helmert(int levels) {
matrix[levels, levels - 1] m = rep_matrix(-1, levels, levels - 1);
real k = 0;
for(j in 1:(levels - 1))
for(i in 1:levels)
if(j <= i - 2)
m[i, j] = 0;
else if(j == i - 1) {
k = k + 1;
# Regular expressions I've used to convert a tex file using Biblatex to apacite:
(?s)cites{([^{}]*?)}{
cites{\1,
(?s)cites{([^{}]*?)}\n{
cites{\1,