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 / ols.py
Created January 19, 2016 17:08
Simple OLS with Python
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
Simple OLS with Python
Fitting a simple OLS regression model using linear algebra with Python,
following the first Matlab programming excercise for Machine Learning.
Author:
@mikmart
mikmart / complete_count.R
Last active February 19, 2018 10:28
Counting and completing, with special behaviour for `grouped_df`
library(tidyverse)
library(rlang)
complete_count <- function(.data, ...) {
UseMethod("complete_count")
}
complete_count.grouped_df <- function(.data, ...) {
grps <- groups(.data)
@mikmart
mikmart / applying-arrays.R
Last active March 10, 2018 21:13
Calculating summaries from arrays without loops
set.seed(1)
n <- 1000
matrices <- replicate(n, matrix(runif(24 ^ 2), nrow = 24))
str(matrices)
#> num [1:24, 1:24, 1:1000] 0.266 0.372 0.573 0.908 0.202 ...
f <- function(x) {
sum_x <- sum(x)
if (sum_x == 0)
@mikmart
mikmart / reverse-time.md
Last active February 10, 2024 09:01
Reversing a time axis in ggplot2

An answer to this closed StackOverflow question about reversing a time axis in ggplot2.

date = c("2011-11-15", "2011-11-16", "2011-11-17", "2011-11-19")
start = c("12:01:27", "12:01:25", "12:01:02", "12:01:12")
end = c("12:30:15", "12:32:15", "12:39:12", "12:30:18")

df = data.frame(date = as.POSIXct(date),
     ystart = as.POSIXct(start, format="%H:%M:%S"), 
     yend = as.POSIXct(end, format="%H:%M:%S"),
@mikmart
mikmart / substitute-stack.md
Last active July 20, 2018 08:49
Substitute expression in the call stack
# Substitute a bare expression in all call stack envs
substitute_stack <- function(expr) {
  substitute_stack_q(substitute(expr))
}

# Substitute a quoted expression in all call stack envs
substitute_stack_q <- function(expr) {
  envs <- sys.frames() # call stack
 for (e in rev(envs)) {
@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]]
@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 / 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 / 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 / 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)