Skip to content

Instantly share code, notes, and snippets.

View JoFrhwld's full-sized avatar

Josef Fruehwald JoFrhwld

View GitHub Profile
@JoFrhwld
JoFrhwld / phila.schools.R
Created March 7, 2012 20:33
Mapping Philadelphia Highschools
library(rgdal) #This may be a pain to get installed. The OGR functions come from it.
## install gdal, available here: http://www.gdal.org/
## install proj.4, available here: http://trac.osgeo.org/proj/
## For some reason, R wasn't expecting for libproj to be installed where it was,
## so I had to tell it to look at /usr/local/bin
library(ggplot2)
library(reshape2)
library(plyr)
gpclibPermit()
@JoFrhwld
JoFrhwld / phila.schools.R
Created March 7, 2012 20:33
Mapping Philadelphia Highschools
library(rgdal) #This may be a pain to get installed. The OGR functions come from it.
## install gdal, available here: http://www.gdal.org/
## install proj.4, available here: http://trac.osgeo.org/proj/
## For some reason, R wasn't expecting for libproj to be installed where it was,
## so I had to tell it to look at /usr/local/bin
library(ggplot2)
library(reshape2)
library(plyr)
gpclibPermit()
@JoFrhwld
JoFrhwld / ling-plot.Rnw
Created April 14, 2012 20:49
A knitr source file for including linguistic notation as graphical elements in ggplot2
\documentclass{beamer}
\usetheme{Singapore}
\usepackage{tipa}
%% Make the r-code small
\ifdefined\knitrout
\renewenvironment{knitrout}{\begin{footnotesize}}{\end{footnotesize}}
\else
\fi
@JoFrhwld
JoFrhwld / pvalues.R
Created May 16, 2012 14:47
Blog post on the decline effect
library(ggplot2)
library(plyr)
## Set your effect size, and desired p-value threshold here
effect = 0.1
thresh = 0.05
## Sets up the simulation parameters
pars = list(mean1 = 1, mean2 = 1+effect, sd1 = 1, sd2 = 1)
nsim = 1000
@JoFrhwld
JoFrhwld / exponents_model1.R
Created May 17, 2012 15:56
For a blog post on calculating probabilities
## You need to model the "failures"
psm$Del <- 1-psm$td
mod <- glmer(Del ~ Gram2 + (Gram2|Speaker) + (1|Word), data = psm, family = binomial(link = cloglog))
j3 <- exp(fixef(mod)["Gram2semiweak"])
k3 <- exp(fixef(mod)["Gram2mono"])
c(j1, k1)
## semiweak mono
@JoFrhwld
JoFrhwld / bw_default.R
Created June 18, 2012 21:04
Overplotting for black and white graphics
ggplot(Vy, aes(F2, F1, shape = Vowel))+
geom_point()+
theme_bw()+
scale_x_continuous(name = "F2", trans = revlog_trans())+
scale_y_reverse(name = "F1")+
stat_ellipse(level = 0.95)+
opts(legend.position = "none")+
annotate(geom="text", x=2900, y = 460, label = "iyC", size = 5)+
annotate(geom="text", x=2400, y = 750, label = "eyC", size = 5)+
annotate(geom="text", x=1850, y = 970, label = "ay", size = 5)
list2fd <- function(list, basis){
if(class(list[[1]]) == "fdSmooth"){
coef_list <- lapply(list, function(x)x$fd$coefs)
}else if(class(list[[1]]) == "fd"){
coef_list <- lapply(list, function(x)x$coefs)
}
n_coefs <- unlist(lapply(coef_list, length))
if(!all(n_coefs == max(n_coefs))) stop()
#' Find zero crossings in an fd object
#'
#' @import fda
#' @import magrittr
#'
#' @param fd an fd object
#' @param Lfdobj the derivative (0, 1, 2)
#' @param slope The slope of interest at the zero crossing
#' @param eps The prediction granularity
#' @param min Localize the zero crossing search to be greater than min
@JoFrhwld
JoFrhwld / Rmd_example.rmd
Created September 27, 2012 19:03
Code for my UseR_Sept2012 talk
#### Values created by statistics
Statistical layers added to plots actually create new pieces of data, like the y-coordinates of the smoother. Some statistical layers create a few different values, and you can choose which one you want to plot. For example, here is a density plot, where the kernel density estimate is represented by a colored line.
```{r tidy = F, fig.width = 8/1.2, fig.height=5/1.2}
ggplot(I_jean, aes(Dur_msec, color = Word))+
geom_density()
```
You have to understand the densities represented in this plot as being conditional on selecting a specific word. That is, given that we have decided to think about the lexical item "I've", what is the probability it will be found in a specific range of durations?
# http://www.cdc.gov/nchs/data/nhsr/nhsr010.pdf
n_fem = 604
h_fem = 162.2
se_fem = 0.34
sd_fem = se_fem * sqrt(n_fem)
n_mal = 591
h_mal = 176.6
se_mal = 0.38