Skip to content

Instantly share code, notes, and snippets.

@JoFAM
JoFAM / VariantAnalysis.R
Last active February 22, 2021 20:56
Analysis of proportion of variants 501Y.V1 and 501Y.V2 in Belgium
# remotes::install_github("rvlenth/emmeans")
# use the latest version of emmeans, as the current cran version 1.5.4 containsa bug
library(dplyr)
library(tidyr)
library(ggplot2)
library(lme4)
library(emmeans)
# create fits function:
create_fit <- function(x, variant = "nv1",n = 100){
@JoFAM
JoFAM / PvalueSimulation.R
Last active January 15, 2020 15:30
Small simulation on resampling inspired by the work of Daniel Lakens. Renders a gif animation.
library(gganimate)
# needs packages gifski & transformr
# If they are not installed, please also run
# install.packages(c("gifski", "transformr"))
# Function that does the simulation
create_pvals <- function(n){
x <- rnorm(n)
y <- rnorm(n)
last <- seq(10,n,by=10)
@JoFAM
JoFAM / UAHtemps.R
Created November 19, 2019 13:31
Plotting monthly global temperatures from UAH dataset
# Plot UAH monthly temperatures
# Joris Meys
# Date last modified: 2019-11-18
# Create a temporary filename
fname <- tempfile()
# Download data into the temporary file
download.file("https://www.nsstc.uah.edu/data/msu/v6.0/tlt/uahncdc_lt_6.0.txt",destfile = fname)
@JoFAM
JoFAM / server.R
Created September 12, 2019 16:19
An example of a shiny App that can be run from a Gist
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
mysamples <- reactive({
fdist <- switch(input$dist,
Normal = rnorm,
Exponential = rexp,
Poisson = function(n) rpois(n,4))
@JoFAM
JoFAM / PottmpOcean.R
Created March 31, 2019 18:04
Calculate departure from average ocean temperature for 1980-2019
## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
# Needed so the code runs later on.
datadir <- "pottmp_data"
fnames <- paste("pottmp",1980:2019,"nc", sep = ".")
## ----Download information, eval=FALSE------------------------------------
## # ONLY RUN THIS ONCE TO DOWNLOAD THE DATA
## # This downloads 39 files of appx 144 Mb each!
## # Create a directory to store the data.
@JoFAM
JoFAM / HADCRUTMEDIAN.R
Created March 11, 2019 16:54
Quick plot of the hadcrut data.
# Download median of regional timeseries
temp <- readLines("https://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.6.0.0.monthly_ns_avg.txt")
temp <- data.frame(do.call(rbind, strsplit(temp," ")),
stringsAsFactors = FALSE)
temp[-1] <- lapply(temp[-1], as.numeric)
temp$year <- as.numeric(gsub("/.*","",temp[,1]))
temp$month <- as.numeric(gsub(".*/","",temp[,1]))
temp$time <- temp$year + temp$month/12.5
@JoFAM
JoFAM / SeaLevelSimulation.R
Created February 12, 2019 11:01
Small script to create a simulation of linear sealevel rise under different speeds.
library(tidyr)
library(ggplot2)
# I look at 4 different rates here. The rate for the period 2000-2018 was approximately 3 mm / year.
rates <- c(0.15,0.2,0.3, 0.4)
# This creates a tibble (dataset) with the sea level rise until 2100 depending on the rate
mydata <- tibble(
years = seq(2018,2100, by=1),
slow = seq_along(years)*rates[1],
@JoFAM
JoFAM / lift_dlvsrbind.txt
Last active December 19, 2018 12:00
lift_dl() versus rbind()
``` r
library(magrittr)
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#>
#> set_names
library(microbenchmark)
@JoFAM
JoFAM / InstallRoxygen601.R
Last active September 11, 2018 18:33
Install roxygen 6.0.1 for Windows
# Currently roxygen 6.1.0 doesn't read the collate field correctly, so once you use @include anywhere
# in your package, you run into problems. The error you get, is a 'Permission denied' error. If that
# occurs, best thing to do for now is to install the previous version of roxygen until
# the problem is solved.
#
# I've made a binary build for Windows available. You can download it manually from following link:
# https://jorismeys.stackstorage.com/s/co8cKzyRemVHZef
# Alas the filesharing I use doesn't allow automatic downloading through download.file(), so you
# have to go to that link and click on Download to get the actual zip file.
#
@JoFAM
JoFAM / GenderWageGap.R
Last active April 10, 2018 17:23
Code to produce comparison of wage gap based on OECD data.
library(OECD)
library(dplyr)
library(ggplot2)
## Get the latest OECD data on decile ratios of gross earnings.
## This is the dataset that contains information on wage gaps.
oecdData <- get_dataset("DEC_I")
## Prepare the plot data
gwg <-