Skip to content

Instantly share code, notes, and snippets.

View aammd's full-sized avatar

Andrew MacDonald aammd

  • Université de Sherbrooke
  • Montreal, Canada
View GitHub Profile
library(brms)
library(tidyverse)
# we're going to have 5 animals
nspp <- 5
# in 100 sites
nsites <- 100
#abilities are between -1 and 1, from a normal distribution:
thetas <- rnorm(nspp, 0, 1.2)
@aammd
aammd / mkrproj.sh
Created January 3, 2018 14:59 — forked from bearloga/mkrproj.sh
A bash shell script that can be used to turn the current directory into an RStudio project, opening the project in RStudio after creating it.
#!/bin/bash
# Usage: mkproj [projectname]
# projectname defaults to name of current directory
template="Version: 1.0\nRestoreWorkspace: Default\nSaveWorkspace: Default\nAlwaysSaveHistory: Default\n\nEnableCodeIndexing: Yes\nUseSpacesForTab: Yes\nNumSpacesForTab: 4\nEncoding: UTF-8\n\nRnwWeave: knitr\nLaTeX: pdfLaTeX"
wd=$(basename `pwd`)
if [ -z $1 ]; then
library(pageviews)
require(lubridate)
today <- pageview_timestamps()
earlysept <- pageview_timestamps(lubridate::ymd("2017-09-01"))
korsmit <- pageviews::article_pageviews("en.wikipedia", "Roy_Kortsmit", start = earlysept, end = today)
korsmit %>%
ggplot(aes(x = date, y = views)) + theme_minimal() + geom_line() + geom_point() +
labs(title = "Roy Kortsmit wikipedia pageviews", subtitle = "Before and after he made 4 saves in 5s.")

Keybase proof

I hereby claim:

  • I am aammd on github.
  • I am aammd (https://keybase.io/aammd) on keybase.
  • I have a public key ASAjkSpXgWkiID1aVIJksW_plP1S60NKHq3lnNrdFeLD8wo

To claim this, I am signing this object:

@aammd
aammd / gist:f566a3eafc6ee293d234b32812f3b2bb
Created March 10, 2017 09:30
plotting with a function factory
rmax <- function(rm, baserate){
force(rm)
force(baserate)
function(x) {
rm * x / (
(rm / baserate) + x
)
}
}
@aammd
aammd / hosts.awk
Last active March 8, 2017 18:02
no longer needful as a script to clean the data
BEGIN {
fam = 0
parasite_genus = 0
host_genus = 0
parasite_species = 0
print "Host_fam\tHost_genus\tHost_sp\tParasite_genus\tParasite_sp\tk_i\tLocation\tParasite_fam"
}
/^[A-Z]/ {fam = $1}
/^\t[A-Z]/ {
@aammd
aammd / reshaping.R
Created February 24, 2017 16:26
if you have species names in one column
library(dplyr)
library(tidyr)
library(stringr)
too_wide <- frame_data(
~common_name, ~plant_genera,
"Abagrotis apposita", "Amelanchier Arbutus Cenanothus",
"Abagrotis brunipennis", "Prunus Vaccinium"
)
@aammd
aammd / fake.R
Created January 26, 2017 15:34
fake exponential growth with error
library(dplyr)
library(ggplot2)
data_frame(x = seq(2001, 2016, by = 2),
y = exp(rnorm(length(x), mean = 0.3, sd = 0.03)*(x - 2000))) %>%
ggplot(aes(x = x, y = y)) + geom_point() + geom_line() +
labs(x = "Year",
y = "Cumulative number of papers",
title = "Number of papers with a 'Number of papers' figure") +
theme_bw()
@aammd
aammd / simple_tree_graph.R
Last active January 25, 2017 12:26
i just want a simple tree tho
library(dplyr)
library(igraph)
library(ggplot2)
library(ggraph)
ed <- frame_data(
~from, ~to,
"a", "b",
"a", "c",
"c", "e"
microbenchmark::microbenchmark({lm(ys~xs, data = testdf)$residuals},
{fastLm(ys~xs, data = testdf)$residuals},
{homemade_residuals(indeps, deps)}, times = 500)
identical(homemade_residuals(indeps, deps), homemade_residuals2(indeps, deps))
# all.equal()
testdf <- data_frame(xs = runif(200, 0, 15),
ys = xs * 5 + 15 + rnorm(200, sd = 3))