Skip to content

Instantly share code, notes, and snippets.

@marutter
marutter / sort.R
Created September 25, 2018 01:06 — forked from tjmahr/sort.R
sorting photos
dir <- "F:/Tristan/OneDrive/Pictures/Camera Roll"
ps <- list.files(dir, "*.jpg", full.names = TRUE)
head(ps)
library(exiftoolr)
library(dplyr)
exif <- exif_read(head(ps, 2500), c("SourceFile", "CreateDate" ))
exif <- exif %>%
filter(!is.na(CreateDate)) %>%
@marutter
marutter / jags_stan.R
Created September 6, 2012 01:27
Comparing JAGS and STAN
library(R2jags)
runif(1) # Fixes "Error: object '.Random.seed' not found"
Y <- as.matrix(read.csv("rats.csv",header=F))
N <- 30
T <- 5
x <- c(8.0, 15.0, 22.0, 29.0, 36.0)
x.bar <- mean(x)
rats.data <- list("Y", "x", "T", "N","x.bar")
rats.params <- c("tau.c", "sigma", "alpha.c","tau.alpha", "tau.beta", "beta.c", "alpha0")
@marutter
marutter / rstan_schools.R
Created September 4, 2012 00:01
RStan test code
library(rstan)
schools_code <- '
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real theta[J];
real mu;
@marutter
marutter / R-devel.sh
Created August 10, 2012 02:00
A script to launch the devel version of R
#!/bin/bash
export R_LIBS_SITE=${R_LIBS_SITE-'/usr/lib/R-devel/lib/R/library:/usr/local/lib/R/site-library:/usr/lib/R/site-library::/usr/lib/R/library'}
export PATH="/usr/local/lib/R-devel/bin:$PATH"
R "$@"
@marutter
marutter / build-R-devel
Created August 10, 2012 01:37
A bash script to build r-devel
#!/bin/sh
# This is the location of the source code
cd ~/svn/r-devel/R
# R_PAPERSIZE=letter \
# R_BATCHSAVE="--no-save --no-restore" \
# R_BROWSER=xdg-open \
# PAGER=/usr/bin/pager \
# PERL=/usr/bin/perl \
@marutter
marutter / gist:944188
Created April 27, 2011 12:48
hw_11_code.R
library(lme4)
data.3 <- read.table("CH27PR20.txt")
response <- data.3$V1
field <- factor(data.3$V2)
fert <- factor(data.3$V4)
irrigation <- factor(data.3$V3)
res3.m <- lmer(response~irrigation*fert+(1|field:irrigation))
summary(res3.m)
51.6/(51.6+1.5)
anova(res3.m)
# Bottling Plant Example
# Factor A: Machines - 3 levels
# Factor B: Operators nested withing machines - 4 each
# Response: Cases produced
data <- read.csv("nested.csv")
attach(data)
machf <- factor(machine)
operf <- factor(operator)
library(lattice)
library(lme4)
oats <- read.csv("oats.csv")
attach(oats)
nitrof <- factor(nitro)
xyplot(yield~nitrof|variety)
xyplot(yield~nitrof|variety,groups=replicate)
xyplot(yield~nitrof|variety,groups=replicate,aspect="xy",type="o")
xyplot(yield~variety|nitrof,groups=replicate,aspect="xy")
xyplot(yield~variety|nitrof,groups=replicate,aspect="xy",type="a")
# Classic repeated measures example
# Finding the results of a wine tasting
# 3 wines, 22 judges. The judges are the repeated measure
data <- read.csv("wine_taste.csv")
attach(data)
interaction.plot(Wine,Taster,Taste)
#
# Naive assumptions, no repeated measures
#
res <- lm(Taste~Wine)
#
# Generate random beer data
#
set.seed(314159)
ibv <- .5 # Inner Beer Variation
mu.i <- rnorm(8,18,4)
sodium <- rnorm(6*8,mu.i,ibv)
beer <- rep(1:8,6)
plot(sodium~beer)