Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
@timcdlucas
timcdlucas / pubmedCites.R
Created July 25, 2015 10:40
Scrape the number of citations on pubmed
scrapePub <- function(sp){
Sys.sleep(2)
spString <- tolower(gsub(' ', '+', sp))
url <- paste0('http://www.ncbi.nlm.nih.gov/pubmed/?term=%22', spString, '%22')
page <- html(url)
data(shorebird)
shorebird <- comparative.data(shorebird.tree, shorebird.data, Species, vcv=TRUE, vcv.dim=3)
mod1 <- pgls(log(Egg.Mass) ~ log(M.Mass) * log(F.Mass), shorebird)
print(mod1)
mod1.sum <- summary(mod1)
print(mod1.sum)
mod1.aov <- anova(mod1)
print(mod1.aov)
library(ggplot2)
x <- data.frame(y = rnorm(100), g = rep(letters[1:4], 25))
ggplot(x, aes(y = y, x = g, fill = g)) +
geom_boxplot() +
ylim(c(-15, 15)) +
scale_fill_manual(values = grey(c(0, 0.5, 0.9, 1))) +
@timcdlucas
timcdlucas / scrapeCitations.R
Last active October 27, 2015 21:04
Scrape pubmed or scholar
library(dplyr)
# Web scraping.
library(rvest)
# For synonym list
library(taxize)
@timcdlucas
timcdlucas / cloglog.R
Created January 20, 2016 11:37
equations for predicting cloglog models
d <- data.frame(x = rnorm(100) + c(1, 2), y = c(0, 1))
m <- glm(y ~ x, data = d, family = binomial(link = cloglog))
newx <- seq(-5, 5, length.out = 100)
predict.line <- predict(m, newdata = data.frame(x = newx), type = 'response')
plot(y ~ x, d)
@timcdlucas
timcdlucas / r2math.R
Last active January 26, 2016 15:23
R to Mathematica
# Print an R matrix in a form that can be copied straight into mathematica
MathematicaPaste.matrix <- function(m){
charmat <- apply(m, 2, as.numeric)
charmat[, -NCOL(charmat)] <- paste0(charmat[, -NCOL(charmat)], ',')
charmat <- rbind('{', t(charmat), '},')
charmat[dim(charmat)[1], dim(charmat)[2]] <- '}'
@timcdlucas
timcdlucas / Mathematica2R.R
Created February 4, 2016 14:58
Convert Mathematica array text to R array
#' Turn a string copied from Mathematica into an R array
#'
#'@param text Text representation of a mathematica array.
#'@param drop Logical. If TRUE, 1 and 2 dimensional arrays will be returned as vectors and matrices.
#' If FALSE, 1 and 2 dimensional arrays will be returned.
#'
#'@return An array
#'@export
@timcdlucas
timcdlucas / gist:4c44c0b4fe59592d385a
Created February 15, 2016 17:48
Compare slopes bewteen two datasets.
# Make up some data
data1 <- data.frame(length = rnorm(20, 8), weight = 1:20)
data2 <- data.frame(length = rnorm(30, 8), weight = 1:30)
# add a dummy variable and combine the data
data1$dummy <- 'data1'
@timcdlucas
timcdlucas / DFT.R
Last active February 17, 2016 19:14
Discrete Fourier transform
# Simulate timeseries with period 10
time <- 1:200
y <- sin(time * 2 * pi / 10)
FT <- rep(NA, length(y) - 1)
for(k in 1:(length(y) - 1)){
FT[k] <- sum(y * (cos(-2 * pi * k * 0:(length(y) - 1) / length(y)) + 1i * sin(-2 * pi * k * 0:(length(y) - 1) / length(y))))
@timcdlucas
timcdlucas / gist:781a9c2d03eb144748fa
Created March 22, 2016 15:00
colourstrips in ggtree
t <- read.nexus('data/Chapter3/fritz2009geographical.tre')
tr1 <- t[[1]]
mat <- data.frame(name = tr1$tip.label)
mat$col1 = 'a'
mat$col1[1:300] <- 'b'