This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(plyr) | |
n_games <- readRDS("n_games.Rdata") | |
n_positions <- readRDS("n_positions.Rdata") | |
fullmoves_white <- readRDS("fullmoves_white.Rdata") | |
fits <- readRDS("chess_fits.Rdata") | |
fit <- fits$fit | |
fit <- fit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From https://aschinchon.wordpress.com/2015/06/23/the-2d-harmonograph-in-shiny/ | |
# All code by Antonio S. Chinchón (@aschinchon), just rearranged so that you can directly | |
# run it by copy and pasting it into an R console. | |
library(shiny) | |
CreateDS = function () { | |
f=jitter(sample(c(2,3),4, replace = TRUE)) | |
d=runif(4,0,1e-02) | |
p=runif(4,0,pi) | |
xt = function(t) exp(-d[1]*t)*sin(t*f[1]+p[1])+exp(-d[2]*t)*sin(t*f[2]+p[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prior to the tutorial make sure that the script below runs without error on your python installation. | |
# What you need is a working installation of Stan: http://mc-stan.org/ . | |
# For installation instructions, see here: | |
# http://mc-stan.org/interfaces/pystan.html | |
# After installation you should be able to run this script which should output | |
# some summary statistics and some pretty plots, :) | |
# Fitting a simple binomial model using Stan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Bayesian model that calculates a probability that a couple is fertile | |
# and pregnant. Please use this for fun only, not for any serious purpose | |
# like *actually* trying to figure out whether you are pregnant. | |
# Enter your own period onsets here: | |
period_onset <- as.Date(c("2014-07-02", "2014-08-02", "2014-08-29", "2014-09-25", | |
"2014-10-24", "2014-11-20", "2014-12-22", "2015-01-19")) | |
# If you have no dates you can just set days_between_periods to c() instead like: | |
# days_between_periods <- c() | |
days_between_periods <- as.numeric(diff(period_onset)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "A Bayesian Model to Calculate Whether My Wife is Pregnant or Not" | |
author: "Rasmus Bååth" | |
output: html_document | |
--- | |
```{r echo = FALSE} | |
library(knitr) | |
options(digits=2) | |
``` |
OlderNewer