Skip to content

Instantly share code, notes, and snippets.

View mikmart's full-sized avatar

Mikko Marttila mikmart

  • Orion Pharma
  • United Kingdom
View GitHub Profile
@mikmart
mikmart / merge-overlaps.md
Last active February 5, 2019 10:19
57th take at merging overlapping intervals in a data frame
library(data.table)

merge_overlaps <- function(tbl, start, end, max_gap = 0) {
  grps <- dplyr::group_vars(tbl)
  
  s <- rlang::enexpr(start)
  e <- rlang::enexpr(end)
  
  s_nm <- rlang::as_label(s)
library(rstan)
library(bayesplot)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
# https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
stan_code <- '
data {
int<lower=0> J; // number of schools
@mikmart
mikmart / uk-driver-deaths.R
Last active November 23, 2018 11:34
Match point and line transitions
library(tidyverse)
library(lubridate)
library(gganimate)
library(ggfortify)
deaths <- fortify(UKDriverDeaths) %>%
rename(date = Index, deaths = Data) %>%
mutate(year = year(date), month = month(date))
p <- ggplot(deaths, aes(month, deaths)) +
@mikmart
mikmart / splines.md
Created November 22, 2018 13:19
Natural vs. B-splines
library(tidyverse)
library(modelr)
library(splines)

m_ns <- lm(mpg ~ ns(wt, df = 2), data = mtcars)
m_bs <- lm(mpg ~ bs(wt, df = 4), data = mtcars)

pred <- mtcars %>% 
 data_grid(wt = seq(0, 7, len = 256)) %&gt;% 
@mikmart
mikmart / theme-grid-flip.md
Last active September 21, 2018 13:07
Flipping gridlines in ggplot2 themes
library(tidyverse)
library(fbcutils)

# Produce a grob to be used as for panel backgrounds
guide_grid2 <- function(theme, x.minor, x.major, y.minor, y.major) {

  x.minor <- setdiff(x.minor, x.major)
  y.minor <- setdiff(y.minor, y.major)
@mikmart
mikmart / cut-axis.md
Last active September 13, 2018 07:24
library(ggplot2)
library(magick)
#> Linking to ImageMagick 6.9.9.14
#> Enabled features: cairo, freetype, fftw, ghostscript, lcms, pango, rsvg, webp
#> Disabled features: fontconfig, x11

library(grid)
library(gtable)
@mikmart
mikmart / geom-bar-fail.md
Created September 10, 2018 13:26
Something weird with geom_bar()
library(tidyverse)

df <- structure(list(foo = structure(c(30L, 15L, 18L, 14L, 50L, 5L, 
20L, 22L, 19L, 9L), .Label = c("01", "02", "03", "04", "05", 
"06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", 
"17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", 
"28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", 
"39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 
"50", "51"), class = "factor"), bar = structure(c(10L, 8L, 9L, 
@mikmart
mikmart / add-tdc.R
Last active August 28, 2018 06:24
Function for adding time-dependent covariates and events to interval data
library(tidyverse)
add_tdc <- function(df, int, tdc = vars(), event = vars()) {
nm <- names(df)
original_cols <- nm
int <- select_vars(nm, !!!int)
tdc <- select_vars(nm, !!!tdc)
stopifnot(length(int) == 2)
@mikmart
mikmart / matsums.R
Last active August 13, 2018 10:06
Efficiency of rowSums() vs colSums() in the R C API
library(microbenchmark)
C_colSums <- function(x) .Call("C_colSums", x)
C_rowSums <- function(x) .Call("C_rowSums", x)
set.seed(1234)
x <- tcrossprod(rnorm(5000))
dyn.load("matsums.dll")
microbenchmark(
@mikmart
mikmart / matsums.cpp
Last active February 12, 2020 14:52
Different ways to calculate row/col sums in C++
#define RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// Cols ----------------
// [[Rcpp::export]]