This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
# Web scraping. | |
library(rvest) | |
# For synonym list | |
library(taxize) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]] <- '}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |