Skip to content

Instantly share code, notes, and snippets.

@vankesteren
vankesteren / waves.R
Created September 13, 2018 07:57
totally normal animation
# waveform
library(animation)
library(firatheme) # devtools::install_github(vankesteren/firatheme)
n <- 20
mu <- seq(-5, 5, len = n)
sg <- rgamma(20, 1.5, 0.5)
xx <- seq(-10, 10, len = 1000)
fills <- paste0(firaPalette(n), "88")
@vankesteren
vankesteren / svdreg.R
Last active September 24, 2018 12:29
svd hidim regression
# Try-out of Hastie, Tibshirani, & Friedman (ESL, 2009) p.659
# Generate high-dimensional dataset
beta <- -100:100/100
X <- matrix(rnorm(100*201), 100)
y <- X%*%beta + rnorm(100, 0, sqrt(crossprod(beta)))
# Ridge estimates
bhat_ridge <- solve(crossprod(X) + diag(rep(.1, 201)), crossprod(X, y))
@vankesteren
vankesteren / analysis_skeleton.R
Created October 17, 2018 14:12
Skeleton for new R analyses
#
# Copyright (C) 2018 University of Amsterdam
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@vankesteren
vankesteren / view_html.R
Created November 1, 2018 10:42
View local html files in viewer of Rstudio
view_html <- function(path, height = "maximize") {
tf <- tempfile(fileext = ".html")
file.copy(path, tf)
rstudioapi::viewer(tf, height = height)
}
@vankesteren
vankesteren / check_packages.R
Last active January 18, 2019 10:26
Check packages for DAV
pcks <- c("ISLR", "tidyverse", "haven", "readxl", "MASS", "glmnet", "splines",
"class", "pROC", "rpart", "rpart.plot", "randomForest", "ca",
"igraph")
ip <- rownames(installed.packages())
if (!all(pcks %in% ip)) {
to_install <- pcks[!pcks %in% ip]
message("Package(s) missing. \n",
"Please install the following packages: ",
@vankesteren
vankesteren / lantaarnpaal_utrecht.R
Last active June 29, 2019 05:24
Creating a map with all the lampposts in Utrecht
library(tidyverse)
lights_dat <- read_csv("https://ckan.dataplatform.nl/dataset/83402c68-1c05-4aa5-ab28-2e99d2bc2261/resource/dc10e0ac-351a-49b6-b3db-d0152c29dc02/download/paal-20180906.csv")
pp <-
lights_dat %>%
filter(latitude > 50) %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_point(alpha = 0.03, fill = "#FAFAAB", stroke = 0, pch = 21, size = 1.6) +
geom_point(alpha = 0.8, fill = "#FAFAAB", stroke = 0, pch = 21, size = 0.2) +
@vankesteren
vankesteren / water_NL.R
Last active January 28, 2019 22:53
Plotting 3d maps of the Netherlands with different water levels
# Use the great rayshader package (www.rayshader.com)
library(rayshader)
# Download data
zip_loc <- tempfile()
download.file("http://geodata.nationaalgeoregister.nl/ahn1/extract/ahn1_100m/ahn1_100.tif.zip",
zip_loc)
local_tif <- raster::raster(unzip(zip_loc, "ahn_100.tif"))
unlink(zip_loc)
@vankesteren
vankesteren / regression.R
Last active March 11, 2021 08:28
All the interesting regression quantities
# Randomly generate some data
S <- rWishart(1, 10, diag(10))[,,1] / 10
X <- MASS::mvrnorm(100, rep(0, 10), S)
b <- runif(10, -1, 1)
y <- X %*% b + rnorm(100, sd = sqrt(b %*% S %*% b))
# all the interesting regression quantities!
n <- nrow(X) # sample size
p <- ncol(X) # parameters in model matrix
@vankesteren
vankesteren / convolutions.R
Created March 19, 2019 10:57
Time series convolutions
# convolutions
convolve <- function(x, kernel) {
size <- length(kernel)
x_pad <- c(rep(0, size - 1), x)
out <- rep(0, length(x))
for (i in 1:length(x)) {
out[i] <- x_pad[i:(i + (size - 1))] %*% kernel
}
return(out)
}
@vankesteren
vankesteren / ad_linreg.jl
Last active September 24, 2022 13:22
Gradient descent for linear regression using Zygote AutoDiff
# Gradient descent with autodiff for linear regression
using Zygote
# Data
X = randn(1000, 10)
b = (1:10)
y = X * b + randn(1000)
# MSE for linear model
function mse(bhat)