Skip to content

Instantly share code, notes, and snippets.

View FlukeAndFeather's full-sized avatar

Max Czapanskiy FlukeAndFeather

View GitHub Profile
@FlukeAndFeather
FlukeAndFeather / movebankstudies.R
Created January 2, 2025 22:52
Query Movebank for taxonomic coverage
library(move2)
library(ritis)
library(sf)
library(tidyverse)
# Download movebank study-level information
movebank_studies <- movebank_download_study_info(
# Filter to studies where data are *visible* (not necessarily downloadable)
i_can_see_data = TRUE,
# Specify the list of attributes we want to query
@FlukeAndFeather
FlukeAndFeather / sharkexploitation.R
Created December 13, 2024 17:29
What order are shark fisheries exploited in? Brainstorm for Leo's dissertation.
library(tidyverse)
set.seed(123)
ex_countries <- tibble(
country = c("Canada", "USA", "Mexico", "Brazil", "Chile"),
ruleoflaw = c(0.8, 0.7, 0.41, 0.5, 0.66),
ruleoflawZ = (ruleoflaw - mean(ruleoflaw)) / sd(ruleoflaw)
)
sharks <- tibble(
commonname = c("Tiger", "Bull", "Blacktip", "Nurse"),
# Make the whole analysis
# The prequisite for all is the end of your pipeline (in this case, your model predictions)
.PHONY: all
all: models/predictions.pkl
# You can preview the sequence of commands in the pipeline with: make all -B --recon
# I recommend putting that in your README
# 1. Download raw data
### replace species.csv, species2.csv, etc with names of your raw data files
### the ampersand-colon &: tells make that this rule has multiple grouped targets
# Based on tutorial: https://gganimate.com/articles/gganimate.html
# Accessed 2021-10-01
library(gganimate)
# We'll start with a static plot
p <- ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) +
geom_point()
plot(p)
@FlukeAndFeather
FlukeAndFeather / datasaurus_example.R
Created August 3, 2021 17:43
The datasaurus dozen is a collection of datasets that demonstrate how summary statistics can be misleading. This gist shows how to access the data and visualize it.
# If you don't have devtools, install that first. If you have Windows and R version 4.0 (or greater), see: https://cran.r-project.org/bin/windows/Rtools/
devtools::install_github("lockedata/datasauRus")
# Load the datasaurus dozen and our favorite suite of data processing tools
library(datasauRus)
library(tidyverse)
# Quick look at the contents of the datasaurus_dozen data frame
summary(datasaurus_dozen)
unique(datasaurus_dozen$dataset)
@FlukeAndFeather
FlukeAndFeather / relative_wind.R
Created August 3, 2021 02:18
Given a flight trajectory [(t, x, y)] and co-located wind vectors [(u, v)], calculate the relative angle between flight and wind along the trajectory
library(dplyr)
library(geosphere)
library(glue)
library(ggplot2)
library(mapproj)
# Example data frame
set.seed(1)
tracks <- tibble(
time = 1:5,
@FlukeAndFeather
FlukeAndFeather / pred_mr.R
Last active February 24, 2025 23:27
Predict mammalian metabolic rates from various scaling equations.
pred_mr <- function(
mass_kg,
mr_method = c("kleiber", "kolokotronesetal", "nagy", "savageetal", "whiteseymour"),
unit = c("kcal_day", "kJ_day", "mlO2_hr", "W"),
multiplier = 1
) {
#' Predicted metabolic rate
#'
#' Predict mammalian metabolic rates from various scaling equations.
#'
@FlukeAndFeather
FlukeAndFeather / pointsinmussels.R
Created June 30, 2021 18:04
How unlikely was it to randomly generate 24/25 points in mussels in a quadrat photo?
library(ggrepel)
library(tidyverse)
# These were my measurements for mussel area and quadrat area respectively
musselfrac <- 0.043 / 0.097
# dbinom() is the probability of n successes out of x trials with probability p
# So dbinom(0:25, 25, musselfrac) is the chance of: [0 points in mussels, 1
# point in mussels, 2 points in mussels, ..., 25 points in mussels]
musselprobs <- tibble(n = 0:25,
p = dbinom(0:25, 25, musselfrac))
# This is the probability of 24/25 points in mussels
@FlukeAndFeather
FlukeAndFeather / partial_italic.R
Created February 22, 2021 03:49
Partially italicize ggplot tick labels
library(tidyverse)
# Start with some dummy data
whale_data <- tribble(
~species, ~prey, ~order,
"B. musculus", "krill", 5,
"B. physalus", "fish", 3,
"B. physalus", "krill", 4,
"M. novaeangliae", "fish", 1,
"M. novaeangliae", "krill", 2
@FlukeAndFeather
FlukeAndFeather / prey_field.R
Created April 2, 2020 21:27
Simulate prey fields with a spectral power law of spatial frequency to the –1.5
library(raster)
library(scales)
library(tidyverse)
powpow <- function(n, a, b) {
# based on answers at: https://dsp.stackexchange.com/questions/47640/generating-a-timeseries-with-an-arbitrary-power-spectrum
# Thank you Sam!
if (n %% 2 != 0)