Skip to content

Instantly share code, notes, and snippets.

View rmaia's full-sized avatar

Rafael Maia rmaia

View GitHub Profile
ubercoldist <- function(Qi, ni=c(1,2,2,4), w=0.2){
Qi <- Qi[, 1:length(ni)]
Qi <- log(Qi)
e <- setNames(w/sqrt(ni), names(Qi))
# prepare output
pairsid <- t(combn(nrow(Qi),2))
require(XML)
scrapeBirdlife <- function(searchterm){
webaddress <- paste("http://www.birdlife.org/datazone/sowbsearchresults.php?a=ns&SearchTerms", searchterm, sep='=')
aa <- readLines(webaddress)
# scrap only web address, common name, species name, IUCN status
aa <- aa[grep('species/factsheet', aa)]
aa <- gsub('<li><a href=\"','',aa)
# Plotting eigenvectors from PCA onto scatterplots
sigmat <- matrix(c(1,-.9,-.9,1), nrow=2)
cent <- c(8,3)
dat <- MASS::mvrnorm(n=100, mu=cent, Sigma=sigmat)
eig <- eigen(cov(dat))
plot(dat, xlim=cent[1]+c(-3,3)*sigmat[1,1],
@rmaia
rmaia / plot_phylowbars.R
Created May 4, 2016 15:51
Plot a phylogeny with bars, as separate plots, combined
require(ape)
require(geiger)
# use Geospiza data for example
data(geospiza)
phylo <- drop.tip(geospiza$geospiza.tree, 'olivacea')
bardata <- setNames(geospiza$geospiza.data[,'beakD'], rownames(geospiza$geospiza.data))
@rmaia
rmaia / mcmcglmm_lambda.R
Last active January 1, 2016 03:28
Does MCMCglmm heritability correspond to lambda estimates?
# load required packages
require(phytools)
require(geiger)
require(MCMCglmm)
data(bird.families)
# CASE 1: very high lambda
@rmaia
rmaia / phylostan_Ktrees.R
Last active May 23, 2017 12:55
Bayesian phylogenetic regression incorporating phylogenetic uncertainty by sampling from multiple trees (work in progress)
require(rstan)
require(geiger)
require(MCMCglmm)
# load data
data(geospiza)
dat <- geospiza$geospiza.data
# create fake sample of trees
tr <- drop.tip(geospiza$geospiza.tree, 'olivacea')
@rmaia
rmaia / phylostan_1tree.R
Created July 5, 2013 18:55
Bayesian phylogenetic regression
require(rstan)
require(geiger)
require(MCMCglmm)
# load data
data(geospiza)
# get the inverse of the phylogenetic variance-covariance matrix
tr <- drop.tip(geospiza$geospiza.tree, 'olivacea')
invA <- solve(vcv.phylo(tr))
@rmaia
rmaia / tcs_s3d.R
Last active December 16, 2015 13:09
Use scatterplot3d to plot a tetrahedral color space
# (very convoluted) function to plot tetrahedral colorspace
require(pavo)
require(scatterplot3d)
require(RColorBrewer)
# get the data
data(sicalis)
tcsdat <- tcs(vismodel(sicalis))
@rmaia
rmaia / lmer_pbmc.R
Last active December 16, 2015 11:48
Hypothesis testing in mixed-effects linear models based on parametric bootstrap and monte carlo simulations
# PARAMETRIC BOOTSTRAP NULL HYPOTHESIS TESTING OF FIXED-EFFECT IN A MIXED MODEL
# Same rationale can be used to test random effects
# adapted from Faraway 2006
require(lme4)
# build the models to be tested
# note that to test fixed effects, one should use ML and not REML estimation
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy, REML=F)
@rmaia
rmaia / radial_phylo_bars.R
Last active December 15, 2015 23:49
plot a radial ("fan") tree with a phenotypic trait as a bar plot
require(ape)
require(geiger)
# use Geospiza data for example
data(geospiza)
phylo <- drop.tip(geospiza$geospiza.tree, 'olivacea')
bardata <- setNames(geospiza$geospiza.data$beakD, rownames(geospiza$geospiza.data))