Skip to content

Instantly share code, notes, and snippets.

View davharris's full-sized avatar

David J. Harris davharris

View GitHub Profile
library(MASS)
n = 1000
x = mvrnorm(n, c(0, 0), Sigma = matrix(c(1, .9, .9, 1), nrow = 2))
cor(x) # correlation of about 0.9 before thresholding
thresholded.values = x > 0
cor(thresholded.values) # Correlation drops to about 0.7, depending on random seed
library(ggplot2)
library(mgcv)
# Something bad happens at t=400 and lambda drops
lambdas = c(runif(4E2, 6, 8), runif(1E2, 2, 4))
obs = rpois(length(lambdas), lambdas)
qplot(x = 1:length(obs), y = obs) +
stat_smooth(method = "gam", family = poisson, formula = y ~ s(x), size = 3)
@davharris
davharris / boot.R
Created June 12, 2014 20:17
code for plotting bootstrap predictions from a nonlinear model
set.seed(1)
# Create fake data
x = runif(100, 0, 5)
y = .25 * x^3 - x^2 + rnorm(length(x))
data = data.frame(x = x, y = y)
# Identify 500 points to include in the plots
x.sequence = seq(0, 5, length = 500)
@davharris
davharris / mistnet-ordination.R
Created July 3, 2014 04:41
mistnet-ordination
# Install the most recent version of mistnet (if needed) using the devtools package
# devtools::install_github("davharris/mistnet")
library(mistnet)
# Grab a small data set from another package
library(ade4)
data(aviurba)
# Reduce the fauna table to ones and zeros
y = as.matrix(aviurba$fau > 0)
### Keybase proof
I hereby claim:
* I am davharris on github.
* I am harris (https://keybase.io/harris) on keybase.
* I have a public key whose fingerprint is 0867 7213 63D1 967B 2B0B E9E6 942D 033F 15ED 4389
To claim this, I am signing this object:
gnr_resolve("American Crow")
# $results
# submitted_name matched_name data_source_title score
# 1 American Crow American Painted-snipe EOL 0.25
#
# $preferred
# NULL
n = 7 # Number of elements to permute
k = 1E5 # Number of permutations allowed by RNG
# Declare a character vector to store permutations
permutations = character(k)
for(i in 1:k){
# save k permutations as strings
permutations[i] = paste(sample.int(n), collapse = "-")
}
#Based on https://github.com/robotlolita/fuck-you
# and https://en.wikipedia.org/wiki/Transformation_of_text
screw_you = function(x){
chars = strsplit(paste0(c(" ", rev(letters), rev(LETTERS), 0, 9:1, "&_?!\".';'"), collapse = ""), "")[[1]]
flipped = strsplit(
" zʎxʍʌnʇsɹbdouɯlʞɾıɥɓɟǝpɔqɐZ⅄XMΛ∩⊥SᴚΌԀONW⅂⋊ſIH⅁ℲƎᗡƆ𐐒∀068ㄥ9ގㄣƐᘔ⇂⅋‾¿¡„,˙'؛",
"")[[1]]
split = strsplit(as.character(substitute(x)), "")[[1]]
@davharris
davharris / geiger.R
Created January 30, 2015 00:52
geiger versus multivariate normal
# Confirming that I understand how geiger's variance-covariance matrices work
library(geiger)
library(mvtnorm)
geo = get(data("geospiza"))
# Simulate one trait using geiger::sim.char
sim = t(
f = function(x){
x
}
f() # Throws error: x is missing
g = function(x){
letters[x]
}