Skip to content

Instantly share code, notes, and snippets.

@jarad
jarad / read_dir_with_drake.R
Last active May 31, 2020
Script to read a directory of data similar to `read_dir` but using drake to remember what has already been read.
View read_dir_with_drake.R
library("drake")
files = list.files("data", "*.csv", full.names = TRUE)
add2 = function(d) { # example function to apply to each individual data.frame
d$x = d$x+2
return(d)
}
plan = drake_plan(
@jarad
jarad / frontmatter.tex
Last active Dec 13, 2018
Beamer template for approximate Iowa State University colors and my preferred style
View frontmatter.tex
\documentclass{beamer}
\usetheme{AnnArbor}
\usecolortheme{beaver}
\usefonttheme[onlymath]{serif} % uncomment for article style math
\setlength{\unitlength}{\textwidth} % measure in textwidths
\usepackage[normalem]{ulem}
@jarad
jarad / read_my_dir.R
Last active May 31, 2020
Function to read a directory worth of data
View read_my_dir.R
library("dplyr")
library("tidyr")
library("readr")
read_my_csv = function(f, into) {
readr::read_csv(f) %>%
dplyr::mutate(file = f) %>%
tidyr::separate(file, into)
}
@jarad
jarad / Vectorize_script.R
Created Apr 5, 2016
Example usage of the Vectorize() function in R for evaluation of a likelihood surface for multiple values of the parameter
View Vectorize_script.R
y = rnorm(5)
log_like = function(theta, y) {
sum(dnorm(y,theta,log=TRUE))
}
thetas = c(1,2)
# Evaluating the theta vector one at a time works,
log_like(thetas[1],y)
@jarad
jarad / install_and_load_packages
Created Oct 14, 2014
Function to install (if necessary) and load R packages
View install_and_load_packages
# From http://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
install_and_load_packages <- function(x){
for( i in x ){
# require returns TRUE invisibly if it was able to load package
if( ! require( i , character.only = TRUE ) ){
# If package was not able to be loaded then re-install
install.packages( i , dependencies = TRUE )
# Load package after installing
require( i , character.only = TRUE )
}
@jarad
jarad / convergenceToADistribution.R
Created Dec 15, 2011
random walk Metropolis for a equal mixture of two normals
View convergenceToADistribution.R
mn = 3
f = function(x) {
n = length(x)
apply(.5*cbind(dnorm(x,-mn),dnorm(x,mn)),1,sum)
}
# Random-walk Metropolis
set.seed(1)
n.reps = 1e5
x.reps = rep(NA,n.reps)