Skip to content

Instantly share code, notes, and snippets.

View rasmusab's full-sized avatar

Rasmus Bååth rasmusab

View GitHub Profile
@rasmusab
rasmusab / significance_test.R
Last active June 10, 2020 21:01
A Significantly Improved Significance Test! Not! (More context in this blogpost: http://www.sumsar.net/blog/2014/02/a-significantly-improved-test/)
# Test of Significance, takes the same arguments as t.test() .
signif.test <- function(x, ...) {
p <- t.test(x, ...)$p.value
# List of p excuses retrieved from http://mchankins.wordpress.com/2013/04/21/still-not-significant-2/
p_excuses <- c(
"(barely) not statistically significant <p>",
"a barely detectable statistically significant difference <p>",
"a borderline significant trend <p>",
"a certain trend toward significance <p>",
@rasmusab
rasmusab / r_markdown_example.Rmd
Created March 25, 2014 22:19
R markdown example
Inlämningsuppgifter SM 1 & 2
========================================================
Rasmus Bååth, Sumsar Htååb
Detta är ett exempeldokumen skrivet med R markdown.
R markdown-dokumentet måste vara fristående från din nuvarande R session.
T.ex. så måste du i R markdown dokumentet alltid ladda in de paket du vill
använda även om de redan är inladdade i din nuvarande R-session:
```{r message=FALSE}
@rasmusab
rasmusab / corr_jags.R
Created April 6, 2014 21:18
Correlation in rJAGS
library(rjags)
library(mvtnorm) # to generate correlated data with rmvnorm.
library(car) # To plot the estimated bivariate normal distribution.
set.seed(31415)
mu <- c(10, 30)
sigma <- c(20, 40)
rho <- -0.7
cov_mat <- rbind(c( sigma[1]^2 , sigma[1]*sigma[2]*rho ),
c( sigma[1]*sigma[2]*rho, sigma[2]^2 ))
@rasmusab
rasmusab / mario_beep
Last active August 29, 2015 14:04
Defines a function that plays the Mario theme while an experssion is executing (Linux only)
# The code below will probably only work on Linux and requires VLC on the path.
library(beepr)
library(stringr)
# Plays a file with vlc and returns the vlc instance's PID
play_vlc_pid <- function(fname) {
system(paste("vlc -Idummy --no-loop --no-repeat --playlist-autostart --no-media-library --play-and-exit", fname),
ignore.stdout = TRUE, ignore.stderr=TRUE,wait = FALSE)
ps_out <- system("ps -eo pid,comm,etime| grep vlc", intern=TRUE)
@rasmusab
rasmusab / introduction_to_bayes_test_script.R
Last active May 2, 2023 17:16
Test script for "Introduction to Bayesian data analysis with R"
# Prior to the tutorial make sure that the script below runs without error on your R installation.
# What you need is a working installation of JAGS: http://mcmc-jags.sourceforge.net/
# and the rjags R package that can be installed from R by running
# install.packages("rjags")
# Generating some fake data
set.seed(123)
y <- rbinom(30, size = 1, prob = 0.2015)
# Fitting a simple binomial model using JAGS
### Function for generating the posterior of Robo's position ###
landscape <- c(rep("plain", 50), rep("mountain", 25), rep("forest", 45))
landscape_color <- c(mountain = "black", forest = "green", plain = "yellow")
cover_cost <- c(mountain = 10, forest = 5, plain = 1)
posterior_sample <- function(n) {
dist_i <- sample(3, n, replace = TRUE, c(18, 17, 65))
mu <- c(15, 40, 90)
@rasmusab
rasmusab / lin_reg_speed_comparison.R
Last active August 29, 2015 14:17
A Speed Comparison Between Flexible Linear Regression Alternatives in R.
library(microbenchmark)
library(arm)
library(rstan)
library(bbmle)
log_post <- function(par, y, x) {
sigma <- exp(par[1])
intercept <- par[2]
beta <- as.matrix(par[-c(1,2)])
mu <- intercept + x %*% beta
@rasmusab
rasmusab / millionbase_to_json.py
Last active May 8, 2019 13:58
Converts the millionbase chess PGN database (http://www.top-5000.nl/pgn.htm) to json
# Converts the millionbase chess PGN database (http://www.top-5000.nl/pgn.htm) to json
# with one json dictionary per row. (That is, the resulting file is contain multiple json objects,
# not just one large).
import json
import chess.pgn # From python-chess https://github.com/niklasf/python-chess
pgn = open("millionbase-2.22.pgn") # Or where you have put it
fout = open('milionbase-2.22.json', 'w') # Or where you want it
@rasmusab
rasmusab / chess_json_to_matrix.R
Created May 28, 2015 22:32
Takes a json file describing chess games and produces a matrix with one row per turn showing how many pieces are left, one column per piece.
# Takes a json file describing chess games and produces a matrix with one row
# per turn showing how many pieces are left, one column per piece.
### Don't run this in R studio because it will take up twice the RAM
### as R will make copies instead of references.
library(jsonlite)
library(stringi)
# Path to your json file as produced by this script: https://gist.github.com/rasmusab/07f1823cb4bd0bc7352d
@rasmusab
rasmusab / chess_pre_analysis.R
Created June 2, 2015 00:21
Takes the matrix created by this script: https://gist.github.com/rasmusab/fb98cced046d4c675d74 and calculates some statistics and fits some models to it.
# Takes the matrix created by this script: https://gist.github.com/rasmusab/fb98cced046d4c675d74
# and calculates some statistics and fits some models to it. All this is pretty memory heavy so saving
# the interesting parts using saveRDS so the script only need to be run once.
set.seed(123)
games <- as.data.frame(readRDS("milionbase_matrix.rds"))
# Saving some info
n_games <- max(games[,"game_id"])
fullmoves_white <- games[ games[, "fullmoves"] < 100 & games[,"active_player"] == 1, "fullmoves"]